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

11/27/2019

I am trying to use persistent volume claims and facing this issue

This is my 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

when i debug pod using describe

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"

My pod is showing me this error

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

I am not sure where is the problem in config. but I am sure this is related to volumes because after adding volumes this problem appears

-- Dupinder Singh
kubernetes
kubernetes-pvc
postgresql

1 Answer

11/27/2019

remove subpath. can you try below 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

I just deployed and it works

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
-- P Ekambaram
Source: StackOverflow