我有以下部署:
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
如何解决这个问题?
这个错误实际上有两种可能:
-
您的文件
/var/lib/postgresql/data/postgresql.conf
实际上并不存在。在本例中,需要在挂载它之前创建它。 -
正如评论中提到的,你使用了错误的
subPath
。您的文件/var/lib/postgresql/data/postgresql.conf
存在,但是您试图基于subPath
挂载错误的文件。看mountPath
和subPath
的区别的解释:
mountPath
显示了所引用的卷应该挂载在容器中的位置。例如,如果将卷挂载到mountPath: /a/b/c
,则该卷将可用于目录/a/b/c
下的容器。挂载卷将使mountPath
下的所有卷可用。如果您只需要挂载卷的一部分,例如卷中的单个文件,您可以使用subPath
指定必须挂载的部分。例如,mountPath: /a/b/c
、subPath: d
将在目录/a/b/c
下的挂载卷中创建d
注意,当subPath
是一个文件夹时,文件夹的内容将被挂载到mountPath