我正在尝试使用 Kubernetes 部署 PostgreSQL 数据库,但遇到了与数据目录相关的错误。以下是我对部署的 YAML 配置:
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_环境变量:
POSTGRES_USER=admin
POSTGRES_PASSWORD=password
PGDATA=/var/lib/postgresql/data/pgdata
问题:使用上述配置部署 PostgreSQL 数据库后,pod 无法启动,日志显示以下错误:
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 描述 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.
附加信息:
- 我已验证 PersistentVolumeClaim db-nfs-pvc 已绑定且可用。
- 我已经包含了环境变量 POSTGRES_USER、POSTGRES_PASSWORD 和 PGDATA 来指定 PostgreSQL 用户、密码和数据目录路径。