are VOLUME in Dockerfile persistent in kubernetes

那年仲夏 提交于 2021-02-17 06:01:18

问题


Some Dockerfile have a VOLUME command.

What happens when such containers are deployed in Kubernetes, but no kubernetes volume are provided: no persistent volume (PV), nor persistent volume claim (PVC) ?

Where are the file stored ?

Is the volume persistent ?


For exemple, Dockerfile image for Docker's library/postgreSQL container image has:

    VOLUME /var/lib/postgresql/data

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 }}

回答1:


(answer based on what I observed with Rancher 2.4, kubernetes 1.17 using Docker)

Short answer:

a Docker volume is created in /var/lib/docker/volumes/... and removed when the pods is stopped or redeployed.

Long answer:

Kubernetes doesn't seems to have any knowledge of the volume the containers / in the Dockerfile. No Kubernetes objects seems to be created.

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).

Where are the file stored ?

The file is a regular Docker volume (see docker volume below)

Is the volume persistent ?

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/


来源:https://stackoverflow.com/questions/65036374/are-volume-in-dockerfile-persistent-in-kubernetes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!