使用卷挂载时找不到 postgresql.conf



我有以下部署:

apiVersion: apps/v1
kind: Deployment
metadata:
name: postgres-deployment
spec:
replicas: 1
selector:
matchLabels:
component: postgres
template:
metadata:
labels:
component: postgres
spec:
volumes:
- name: postgres-storage
persistentVolumeClaim:
claimName: postgres-persistent-volume-claim
containers:
- name: postgres
image: prikshet/postgres
ports:
- containerPort: 5432
volumeMounts:
- name: postgres-storage
mountPath: /var/lib/postgresql/data
subPath: postgres

当我包括行23-26和做kubectl apply, pod给出一个错误,不运行,但当我删除行23-36 pod运行。我想用第23-26行创建一个卷挂载。查看此pod的日志错误为:

postgres: could not access the server configuration file "/var/lib/postgresql/data/postgresql.conf": No such file or directory

postgres persistent volume claim is:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: postgres-persistent-volume-claim
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi

如何解决这个问题?

这个错误实际上有两种可能:

  1. 您的文件/var/lib/postgresql/data/postgresql.conf实际上并不存在。在本例中,需要在挂载它之前创建它。

  2. 正如评论中提到的,你使用了错误的subPath。您的文件/var/lib/postgresql/data/postgresql.conf存在,但是您试图基于subPath挂载错误的文件。看mountPathsubPath的区别的解释:

mountPath显示了所引用的卷应该挂载在容器中的位置。例如,如果将卷挂载到mountPath: /a/b/c,则该卷将可用于目录/a/b/c下的容器。挂载卷将使mountPath下的所有卷可用。如果您只需要挂载卷的一部分,例如卷中的单个文件,您可以使用subPath指定必须挂载的部分。例如,mountPath: /a/b/csubPath: d将在目录/a/b/c下的挂载卷中创建d注意,当subPath是一个文件夹时,文件夹的内容将被挂载到mountPath

最新更新