Score:0

Error running PostgreSQL Deployment in Kubernetes: "No such file or directory" for data directory

mf flag

I am trying to deploy a PostgreSQL database using Kubernetes and encountered an error related to the data directory. Below is my YAML configuration for the Deployment:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: db
spec:
  replicas: 1
  selector:
    matchLabels:
      app: db
  template:
    metadata:
      labels:
        type: deployment
        app: db
    spec:
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
              - matchExpressions:
                  - key: affinity-type
                    operator: In
                    values:
                      - postgres
      tolerations:
        - key: "key"
          value: "database"
          operator: "Equal"
          effect: "NoSchedule"
      volumes:
        - name: db-nfs-pvc
          persistentVolumeClaim:
            claimName: db-nfs-pvc
        - name: init-volume
          configMap:
            name: db-init
        - name: db-hba-conf
          configMap:
            name: hba-init
        - name: db-postgresql-conf
          configMap:
            name: postgresql-conf-init
      containers:
        - name: db
          image: postgres:14-bullseye
          imagePullPolicy: IfNotPresent
          ports:
            - containerPort: 5432
          envFrom:
            - secretRef:
                name: db
          volumeMounts:
            - name: db-nfs-pvc
              mountPath: /var/lib/postgresql/data
              subPath: pgdata
            - name: init-volume
              mountPath: /docker-entrypoint-initdb.d/
            - name: db-postgresql-conf
              mountPath: /var/lib/postgresql/data/postgresql.conf
              subPath: postgresql.conf
            - name: db-hba-conf
              mountPath: /var/lib/postgresql/data/pg_hba.conf
              subPath: pg_hba.conf
          command:
            - "postgres"
            - "-c"
            - "config_file=/var/lib/postgresql/data/postgresql.conf"
            - "-c"
            - "hba_file=/var/lib/postgresql/data/pg_hba.conf"

POSTGRESQL_ENV:

POSTGRES_USER=admin
POSTGRES_PASSWORD=password
PGDATA=/var/lib/postgresql/data/pgdata

Issue: Upon deploying the PostgreSQL database using the above configuration, the pod fails to start, and the logs show the following error:

postgres: could not access directory "/var/lib/postgresql/data/pgdata": No such file or directory
Run initdb or pg_basebackup to initialize a PostgreSQL data directory.

kubectl describe pod:

0/11 nodes are available: pod has unbound immediate PersistentVolumeClaims. preemption: 0/11 nodes are available: 11 No preemption victims found for incoming pod.

Additional Information:

  • I have verified that the PersistentVolumeClaim db-nfs-pvc is bound and available.
  • I have included the environment variables POSTGRES_USER, POSTGRES_PASSWORD, and PGDATA to specify the PostgreSQL user, password, and data directory path.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.