我正在尝试在 Google Kubernetes Engine 中创建一个 PostgreSQL 数据库,该数据库以 PVC 为后盾来维护存储的数据库。目前我有这个定义:
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: postgres
spec:
serviceName: "postgres"
selector:
matchLabels:
db: postgres
template:
metadata:
labels:
db: postgres
spec:
containers:
- name: postgres
image: postgres:12.1
ports:
- containerPort: 5432
volumeMounts:
- name: postgres-disk
mountPath: "/var/lib/postgresql"
env:
- name: PGDATA
value: /var/lib/postgresql/data/pgdata
- name: PGUSER
value: postgres
- name: POSTGRES_DB
value: postgres
- name: POSTGRES_PASSWORD
valueFrom:
secretKeyRef:
name: db-secret
key: password
volumeClaimTemplates:
- metadata:
name: postgres-disk
spec:
accessModes: ["ReadWriteOnce"]
storageClassName: "standard"
resources:
requests:
storage: 1Gi
它按预期工作,但如果我杀死 pod 来模拟节点故障,整个数据库将从头重新启动,并丢失所有存储的数据。
答案1
我发现问题在于挂载权限,添加文件夹级别就可以解决问题
volumeMounts:
- name: data-disk
mountPath: "/var/lib/postgresql/data"
env:
- name: PGDATA
value: "/var/lib/postgresql/data/pgdata"