是 Dockerfile 中的 VOLUME 在 kubernetes 中持久化 [英] are VOLUME in Dockerfile persistent in kubernetes
问题描述
一些 Dockerfile 有一个 VOLUME 命令.
Some Dockerfile have a VOLUME command.
当这些容器部署在 Kubernetes 中,但没有提供 Kubernetes 卷时会发生什么:没有持久卷 (PV),也没有持久卷声明 (PVC)?
What happens when such containers are deployed in Kubernetes, but no kubernetes volume are provided: no persistent volume (PV), nor persistent volume claim (PVC) ?
文件存储在哪里?
音量是否持久?
例如,Docker 的 library/postgreSQL 容器图像有:
For exemple, Dockerfile image for Docker's library/postgreSQL container image has:
VOLUME /var/lib/postgresql/data
stable/postgresql helm图表不会总是创建 PV:
The stable/postgresql helm charts won't always create a PV:
kind: StatefulSet
### SNIP SNIP ###
containers:
- name: {{ template "postgresql.fullname" . }}
image: {{ template "postgresql.image" . }}
### SNIP SNIP ###
volumeMounts:
{{ if .Values.persistence.enabled }}
- name: data
mountPath: {{ .Values.persistence.mountPath }}
subPath: {{ .Values.persistence.subPath }}
{{- end }}
### SNIP SNIP ###
{{- if and .Values.persistence.enabled .Values.persistence.existingClaim }}
- name: data
persistentVolumeClaim:
{{- with .Values.persistence.existingClaim }}
claimName: {{ tpl . $ }}
{{- end }}
{{- else if not .Values.persistence.enabled }}
- name: data
emptyDir: {}
{{- else if and .Values.persistence.enabled (not .Values.persistence.existingClaim) }}
volumeClaimTemplates:
- metadata:
name: data
{{- with .Values.persistence.annotations }}
annotations:
{{- range $key, $value := . }}
{{ $key }}: {{ $value }}
{{- end }}
{{- end }}
spec:
accessModes:
{{- range .Values.persistence.accessModes }}
- {{ . | quote }}
{{- end }}
resources:
requests:
storage: {{ .Values.persistence.size | quote }}
{{ include "postgresql.storageClass" . }}
{{- end }}
推荐答案
(根据我在 Rancher 2.4 中观察到的答案,kubernetes 1.17 使用 Docker)
(answer based on what I observed with Rancher 2.4, kubernetes 1.17 using Docker)
简答:
一个 Docker 卷在 /var/lib/docker/volumes/...
中创建,并在 Pod 停止或重新部署时删除.
a Docker volume is created in /var/lib/docker/volumes/...
and removed when the pods is stopped or redeployed.
长答案:
Kubernetes 似乎对 Dockerfile 中容器/的体积一无所知.似乎没有创建任何 Kubernetes 对象.
Kubernetes doesn't seems to have any knowledge of the volume the containers / in the Dockerfile. No Kubernetes objects seems to be created.
当 Kubernetes 告诉 Docker 守护进程启动一个容器时,Docker 创建一个卷(如 docker volume create
)并附加创建的卷(除非 kubernete 提供了一个卷来挂载).
When Kubernetes tell Docker daemon to start a container, Docker creates a volume (like docker volume create
) and attach the created volume (unless kubernete's provides a volume to mount).
文件存储在哪里?
该文件是一个常规的 Docker 卷(参见下面的 docker 卷
)
The file is a regular Docker volume (see docker volume
below)
音量是否持久?
不,当 pod 被移除时,docker 卷也会被移除(就像你运行 docker rm $mycontainer --volumes
)
No, the docker volume is removed when the pod is removed (like if you ran docker rm $mycontainer --volumes
)
docker inspect 6ce5f52186d4 | grep '"Driver": "local"' -A5 -B5
{
"Type": "volume",
"Name": "679135a23430ceea6adb8d89e04c6baf9da33239a83ecb4c4ec3263e2c925d39",
"Source": "/var/lib/docker/volumes/679135a23430ceea6adb8d89e04c6baf9da33239a83ecb4c4ec3263e2c925d39/_data",
"Destination": "/var/lib/postgresql/data",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
}
$ docker volume ls
DRIVER VOLUME NAME
local 679135a23430ceea6adb8d89e04c6baf9da33239a83ecb4c4ec3263e2c925d39
$ du -hs /var/lib/docker/volumes/679135a23430ceea6adb8d89e04c6baf9da33239a83ecb4c4ec3263e2c925d39/
51.4M /var/lib/docker/volumes/679135a23430ceea6adb8d89e04c6baf9da33239a83ecb4c4ec3263e2c925d39/
这篇关于是 Dockerfile 中的 VOLUME 在 kubernetes 中持久化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!