Kubernetes yaml 部署 - 无法创建符号 link

Kubernetes yaml deployment - unable to create a symbolic link

我正在尝试进行 nginx 部署,在容器创建期间,我想创建多个符号链接。但是由于某种原因,它不起作用并且容器崩溃了。

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: tcc
    component: nginx
  name: tcc-nginx-deployment
  namespace: dev2
spec:
  replicas: 1
  selector:
    matchLabels:
      app: tcc
      component: nginx
  template:
    metadata:
      labels:
        app: tcc
        component: nginx
    spec:
      containers:
      - image: nginx
        name: nginx
        command:
              - /bin/sh
              - -c
              - |
                ln -s /shared/apps/ /var/www
                rm -r /etc/nginx/conf.d
                ln -s /shared/nginx-config/ /etc/nginx/conf.d

        ports:
        - containerPort: 80
          protocol: TCP
        volumeMounts:
        - mountPath: /shared
          name: efs-pvc
      volumes:
      - name: efs-pvc
        persistentVolumeClaim:
          claimName: tcc-efs-storage-claim
  • 那是因为您要求容器仅创建符号链接。所以一旦那些
    已创建符号链接容器正在退出。

  • 为避免这种情况添加 "nginx" "-g" "daemon off;" 如下:

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: tcc
    component: nginx
  name: tcc-nginx-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: tcc
      component: nginx
  template:
    metadata:
      labels:
        app: tcc
        component: nginx
    spec:
      containers:
      - image: nginx
        name: nginx
        imagePullPolicy: IfNotPresent
        command:
              - /bin/sh
              - -c
              - |
                ln -s /shared/apps/ /var/www
                rm -r /etc/nginx/conf.d
                ln -s /shared/nginx-config/ /etc/nginx/conf.d
                "nginx" "-g" "daemon off;" ; # after creating above symbolic links it will start nginx daemon 
        ports:
        - containerPort: 80
          protocol: TCP
        volumeMounts:
        - mountPath: /shared
          name: efs-pvc
      volumes:
      - name: efs-pvc
        persistentVolumeClaim:
          claimName: tcc-efs-storage-claim

容器不是 运行,因为在执行 command 块后,容器正在退出,这是预期的行为。

与其在 yaml 模板中使用 command 中的符号链接(这不是最佳实践解决方案),为什么不使用 Kubernetes 内置解决方案并且不使用 command 块全部?

你应该使用 subPath which is designed to share directories from one volume for multiple, different directories on the single pod:

Sometimes, it is useful to share one volume for multiple uses in a single pod. The volumeMounts.subPath property specifies a sub-path inside the referenced volume instead of its root.

在您的情况下,部署 yaml 应如下所示:

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: tcc
    component: nginx
  name: tcc-nginx-deployment
  namespace: dev2
spec:
  replicas: 1
  selector:
    matchLabels:
      app: tcc
      component: nginx
  template:
    metadata:
      labels:
        app: tcc
        component: nginx
    spec:
      containers:
      - image: nginx
        name: nginx
        ports:
        - containerPort: 80
          protocol: TCP
        volumeMounts:
        - mountPath: /shared
          name: efs-pvc
        - mountPath: /etc/nginx/conf.d
          name: efs-pvc
          subPath: nginx-config
        - mountPath: /var/www
          name: efs-pvc
          subPath: apps
      volumes:
      - name: efs-pvc
        persistentVolumeClaim:
          claimName: tcc-efs-storage-claim

此外,如果您只想为 NGINX 安装配置文件,您可以使用 ConfigMap 而不是卷 - 查看 了解更多信息。