目前我正在尝试创建一个基于postgres:latest
镜像的PostgreSQL复制部署。
配置文件默认位于数据目录/var/lib/postgresql/data
中。为了使复制正常工作,我需要让数据目录为空,但这意味着我必须将配置文件保留在其他地方。
参考PostgreSQL文档:
如果您希望将配置文件保留在数据目录之外,则
postgres -D
命令行选项或PGDATA
环境变量必须指向包含配置文件的目录,并且必须在postgresql.conf
(或命令行上)中设置参数以显示实际位于何处的数据目录。请注意,覆盖了-D和PGDATA的数据目录位置,但没有覆盖配置文件的位置。
在物理机器设置中,我们可以手动移动文件并在postgresql.conf
文件中设置数据目录的位置。但是在Kubernetes中并不那么简单。
我尝试使用volumeMount
和subPath
来挂载配置文件到其他位置,然后使用command
更改postgresql.conf
的新位置。
示例.yaml文件:
apiVersion: v1
kind: ConfigMap
metadata:
name: pg-replica
labels:
app: postgres
name: pg-replica
data:
POSTGRES_DB: postgres
POSTGRES_USER: postgres
POSTGRES_PASSWORD: mypassword
pg_hba.conf: |
# Contents
postgresql.conf: |
data_directory = /var/lib/postgresql/data/data-directory
recovery.conf: |
# Contents
---
apiVersion: v1
kind: Service
metadata:
name: pg-replica
labels:
app: postgres
name: pg-replica
spec:
type: NodePort
ports:
- nodePort: 31000
port: 5432
selector:
app: postgres
name: pg-replica
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: pg-replica
spec:
selector:
matchLabels:
app: postgres
name: pg-replica
replicas: 1
template:
metadata:
labels:
app: postgres
name: pg-replica
spec:
containers:
- name: pg-replica
image: postgres:latest
imagePullPolicy: "IfNotPresent"
ports:
- containerPort: 5432
envFrom:
- configMapRef:
name: pg-replica
volumeMounts:
- name: pg-replica
mountPath: /var/lib/postgresql/data
- name: replica-config
mountPath: /var/lib/postgresql/postgresql.conf
subPath: postgresql.conf
- name: replica-config
mountPath: /var/lib/postgresql/pg_hba.conf
subPath: pg_hba.conf
- name: replica-config
mountPath: /var/lib/postgresql/recovery.conf
subPath: recovery.conf
command:
- "/bin/bash"
- "postgres -c config_file=/var/lib/postgresql/postgresql.conf"
volumes:
- name: pg-replica
persistentVolumeClaim:
claimName: pv-replica-claim
- name: replica-config
configMap:
name: pg-replica
返回结果:
返回的消息如下:
/bin/bash: postgres -c config_file=/var/lib/postgresql/postgresql.conf: 没有那个文件或目录
这个配置有什么问题?我需要采取哪些步骤使其正常工作?
编辑:
使用volumeMount
字段时,尽管我用subPath
指定了要挂载的确切文件,但该目录被覆盖(所有其他文件都被删除)。这可能是什么原因?