Error: failed to prepare subPath for volumeMount "postgres-storage" of container "postgres"

Error: failed to prepare subPath for volumeMount "postgres-storage" of container "postgres"

我正在尝试使用持久卷声明并面临这个问题

这是我的postgres-deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: postgres-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      component: postgres
  template:
    metadata:
      labels:
        component: postgres
    spec:
      volumes:
        - name: postgres-storage
          persistentVolumeClaim:
            claimName: database-persistent-volume-claim
      containers:
      - name: postgres
        image: postgres
        ports:
        - containerPort: 5432
        volumeMounts:
          - mountPath: /var/lib/postgresql/data
            name: postgres-storage
            subPath: postgres

当我使用 describe

调试 pod 时
kubectl describe pod postgres-deployment-8576df7bfc-8mp5t

Events:
  Type     Reason     Age                  From                     Message
  ----     ------     ----                 ----                     -------
  Normal   Scheduled  3m4s                 default-scheduler        Successfully assigned default/postgres-deployment-8576df7bfc-8mp5t to docker-desktop
  Normal   Pulled     67s (x8 over 2m58s)  kubelet, docker-desktop  Successfully pulled image "postgres"
  Warning  Failed     67s (x8 over 2m58s)  kubelet, docker-desktop  Error: failed to prepare subPath for volumeMount "postgres-storage" of container "postgres"
  Normal   Pulling    53s (x9 over 3m3s)   kubelet, docker-desktop  Pulling image "postgres"

我的 pod 向我显示此错误

$ kubectl get pods
NAME                                   READY   STATUS                       RESTARTS   AGE
postgres-deployment-8576df7bfc-8mp5t   0/1     CreateContainerConfigError   0          5m5

我不确定配置中的问题在哪里。但我确定这与卷有关,因为在添加卷后出现此问题

删除子路径。你能试试下面的 yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: postgres-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      component: postgres
  template:
    metadata:
      labels:
        component: postgres
    spec:
      volumes:
        - name: postgres-storage
          persistentVolumeClaim:
            claimName: database-persistent-volume-claim
      containers:
      - name: postgres
        image: postgres
        ports:
        - containerPort: 5432
        volumeMounts:
          - mountPath: /var/lib/postgresql/data
            name: postgres-storage

我刚刚部署并且有效

master $ kubectl get deploy
NAME                  READY   UP-TO-DATE   AVAILABLE   AGE
postgres-deployment   1/1     1            1           4m13s
master $ kubectl get po
NAME                                   READY   STATUS    RESTARTS   AGE
postgres-deployment-6b66bdd748-5q76h   1/1     Running   0          4m13s