Kubernetes中volumeMounts.mountPath与PV的hostPath是否为相对关系?
HostPath类型PV的挂载路径疑问
场景描述
我创建了一个hostPath类型的PersistentVolume:
kind: PersistentVolume apiVersion: v1 metadata: name: mypv spec: storageClassName: normal capacity: storage: 10Gi accessModes: - ReadWriteMany hostPath: path: /etc/foo # 宿主机上的路径
接着创建对应的PersistentVolumeClaim,并在Pod中使用:
apiVersion: v1 kind: Pod metadata: labels: run: busybox name: busybox spec: containers: - image: busybox name: busybox volumeMounts: - name: myvolume mountPath: /var/log # 该路径是否相对于PV的hostPath? restartPolicy: Never volumes: - name: myvolume persistentVolumeClaim: claimName: mypvc status: {}
问题
请问实际挂载路径是否会是{hostPath}/{mountPath}(例如/etc/foo/var/log),还是必须明确指定mountPath: /etc/foo/var/log才能达到该效果?
解答
直接明确结论:
- Pod中容器的
mountPath是容器内部的路径,和PV的hostPath不存在路径拼接关系。 - 实际挂载逻辑是:PV的
hostPath: /etc/foo(宿主机目录)会被完整挂载到容器内的/var/log路径。也就是说,容器内访问/var/log时,实际读写的是宿主机的/etc/foo目录,而非/etc/foo/var/log。
如果想要容器内的/var/log对应宿主机的/etc/foo/var/log,有两种正确方式:
- 修改PV的
hostPath为/etc/foo/var/log,保持Pod的mountPath: /var/log不变; - 保持PV的
hostPath: /etc/foo,将Pod的mountPath设为比如/host-foo,然后在容器内通过/host-foo/var/log来访问宿主机的对应子目录。
内容的提问来源于stack exchange,提问作者KJ0797
相关产品推荐
相关产品推荐

