0

I have the following deployment:

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: postgres-persistent-volume-claim
      containers:
        - name: postgres
          image: prikshet/postgres
          ports:
            - containerPort: 5432
          volumeMounts:
            - name: postgres-storage
              mountPath: /var/lib/postgresql/data
              subPath: postgres

And when I include the lines 23-26 and do kubectl apply, the pod gives an error and doesn't run but when I remove the lines 23-36 the pod runs. I want to create a volume mount with the lines 23-26. The error when checking the logs of this pod is:

postgres: could not access the server configuration file "/var/lib/postgresql/data/postgresql.conf": No such file or directory

postgres persistent volume claim is:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: postgres-persistent-volume-claim
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 10Gi

How to fix this?

Wytrzymały Wiktor
  • 11,492
  • 5
  • 29
  • 37
zendevil.eth
  • 974
  • 2
  • 9
  • 28

1 Answers1

1

There are actually two possibilities for this error to occur:

  1. Your file /var/lib/postgresql/data/postgresql.conf does not really actually exist. In this case, you need to create it before mounting it.

  2. As meaningqo good mentioned in the comment, you are using the wrong subPath. Your file /var/lib/postgresql/data/postgresql.conf exist, but you are trying to mount wrong file based on subPath. Look at the explanation the difference between mountPath and subPath:

mountPath shows where the referenced volume should be mounted in the container. For instance, if you mount a volume to mountPath: /a/b/c, the volume will be available to the container under the directory /a/b/c. Mounting a volume will make all of the volume available under mountPath. If you need to mount only part of the volume, such as a single file in a volume, you use subPath to specify the part that must be mounted. For instance, mountPath: /a/b/c, subPath: d will make whatever d is in the mounted volume under directory /a/b/c Notice that when subPath is a folder, the content of the folder will be mounted to the mountPath

Mikołaj Głodziak
  • 4,775
  • 7
  • 28
  • 1. if /var/lib/postgresql/data/postgresql.conf didn't exist it wouldn't work even without persistent volume claim but does. 2. It doesn't work even after removing subPath – zendevil.eth Aug 09 '21 at 12:16
  • It is also possible that you have wrong permissions to this file. Please check it an attach results to the question. – Mikołaj Głodziak Aug 09 '21 at 12:45
  • What do you mean "if /var/lib/postgresql/data/postgresql.conf didn't exist it wouldn't work even without persistent volume claim but does."? It looks quite strange. – Mikołaj Głodziak Aug 09 '21 at 13:11
  • I removed volumeMounts part and then I don't see the error. It works if postgres doesn't use persistent volume claim but the container's storage. It's still accessing the same directory for the postgresql.conf file – zendevil.eth Aug 09 '21 at 14:02