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.