(资料图片仅供参考)
Volumes
官方文档
介绍
Container 中的文件在磁盘上是临时存放的,这给 Container 中运行的较重要的应用程序带来一些问题。
- 当容器崩溃时文件丢失。 kubelet 会重新启动容器,但容器会以干净的状态重启。
- 当Pod中同时运行多个容器,容器之间需要共享文件时。
背景
Kubernetes 支持很多类型的卷。 Pod 可以同时使用任意数目的卷类型。 临时卷类型的生命周期与 Pod 相同,但持久卷可以比 Pod 的存活期长。 当 Pod 不再存在时,Kubernetes 也会销毁临时卷;不过 Kubernetes 不会销毁持久卷。 对于给定 Pod 中任何类型的卷,在容器重启期间数据都不会丢失。
卷的核心是一个目录,其中可能存有数据,Pod 中的容器可以访问该目录中的数据。 所采用的特定的卷类型将决定该目录如何形成的、使用何种介质保存数据以及目录中存放的内容。
使用卷时, 在 .spec.volumes 字段中设置为 Pod 提供的卷,并在 .spec.containers[*].volumeMounts 字段中声明卷在容器中的挂载位置。
容器中的进程看到的文件系统视图是由它们的容器镜像的初始内容以及挂载在容器中的卷(如果定义了的话)所组成的。
其中根文件系统同容器镜像的内容相吻合。 任何在该文件系统下的写入操作,如果被允许的话,都会影响接下来容器中进程访问文件系统时所看到的内容。
EmptyDir
使用emptyDir,当Pod分配到Node上时,将会创建emptyDir,并且只要Node上的Pod一直运行,Volume就会一直存。当Pod(不管任何原因)从Node上被删除时,emptyDir也同时会删除,存储的数据也将永久删除。注:删除容器不影响emptyDir。
pod_empydir.yaml
apiVersion: v1kind: Podmetadata: name: pod-emptydir namespace: defaultspec: containers: - name: busybox1 image: busybox imagePullPolicy: IfNotPresent command: ["/bin/sh", "-c", "sleep 3600"] volumeMounts: - mountPath: /cache name: cache-volume - name: busybox2 image: busybox imagePullPolicy: IfNotPresent command: ["/bin/sh", "-c", "sleep 3600"] volumeMounts: - mountPath: /test/cache name: cache-volume volumes: - name: cache-volume emptyDir: {}
创建并查看结果
[root@master01 volumes]# kubectl apply -f pod_emptydir.yamlpod/pod-emptydir created[root@master01 volumes]# kubectl describe pod pod-emptydirName: pod-emptydirNamespace: defaultPriority: 0Node: master02/192.168.44.11Start Time: Thu, 15 Dec 2022 00:14:10 +0800Labels: Annotations: Status: PendingIP: IPs: Containers: busybox1: Container ID: Image: busybox Image ID: Port: Host Port: Command: /bin/sh -c sleep 3600 State: Waiting Reason: ContainerCreating Ready: False Restart Count: 0 Environment: Mounts: /cache from cache-volume (rw) #挂载 /var/run/secrets/kubernetes.io/serviceaccount from default-token-c7jnm (ro) busybox2: Container ID: Image: busybox Image ID: Port: Host Port: Command: /bin/sh -c sleep 3600 State: Waiting Reason: ContainerCreating Ready: False Restart Count: 0 Environment: Mounts: /test/cache from cache-volume (rw) /var/run/secrets/kubernetes.io/serviceaccount from default-token-c7jnm (ro)Conditions: Type Status Initialized True Ready False ContainersReady False PodScheduled True Volumes: cache-volume: Type: EmptyDir (a temporary directory that shares a pod"s lifetime) Medium: SizeLimit: default-token-c7jnm: Type: Secret (a volume populated by a Secret) SecretName: default-token-c7jnm Optional: falseQoS Class: BestEffortNode-Selectors: Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300s node.kubernetes.io/unreachable:NoExecute op=Exists for 300sEvents: Type Reason Age From Message ---- ------ ---- ---- ------- Normal Scheduled 58s default-scheduler Successfully assigned default/pod-emptydir to master02 Normal Pulling 56s kubelet Pulling image "busybox"
测试Emptydir
[root@master01 volumes]# kubectl exec -it pod-emptydir -c busybox1 -- sh/ # lsbin cache dev etc home lib lib64 proc root sys tmp usr var/ # cd cache//cache # touch create_busybox/cache # ^C/cache # exitcommand terminated with exit code 130[root@master01 volumes]# kubectl exec -it pod-emptydir -c busybox2 -- sh/ # lsbin dev etc home lib lib64 proc root sys test tmp usr var/ # cd test/cache//test/cache # lscreate_busybox
Hostpath
hostPath允许挂载Node上的文件系统到Pod里面去。如果Pod需要使用Node上的文件,可以使用hostPath。
示例
- 运行一个需要访问 Docker 引擎内部机制的容器;请使用 hostPath 挂载 /var/lib/docker 路径。
- 在容器中运行 cAdvisor 时,以 hostPath 方式挂载 /sys。
- 允许 Pod 指定给定的 hostPath 在运行 Pod 之前是否应该存在,是否应该创建以及应该以什么方式存在
支持类型
取值 | 行为 |
---|---|
空字符串(默认)用于向后兼容,这意味着在安装 hostPath 卷之前不会执行任何检查 | |
DirectoryOrCreate | 如果指定的路径不存在,那么将根据需要创建空目录,权限设置为 0755,具有与 Kubelet 相同的组和所有权 |
Directory | 给定的路径必须存在 |
FileOrCreate | 如果给定路径的文件不存在,那么将在那里根据需要创建空文件,权限设置为 0644,具有与 Kubelet 相同的组和所有权【前提:文件所在目录必须存在;目录不存在则不能创建文件】 |
File | 给定路径上的文件必须存在 |
Socket | 在给定路径上必须存在的 UNIX 套接字 |
CharDevice | 在给定路径上必须存在的字符设备 |
BlockDevice | 在给定路径上必须存在的块设备 |
hostpath.yaml
apiVersion: v1kind: Podmetadata: name: pod-hostpath namespace: defaultspec: containers: - name: busybox3 image: busybox imagePullPolicy: IfNotPresent command: ["/bin/sh", "-c", "sleep 3600"] volumeMounts: - name: hostpath-dir-volume mountPath: /test-k8s/hostpath-dir volumes: - name: hostpath-dir-volume hostPath: path: /root/test_hostPath # 宿主机目录 type: DirectoryOrCreate # hostPath 卷指定 type,如果目录不存在则创建(可创建多层目录)
创建并查看结果
[root@master01 volumes]# kubectl apply -f hostpath.yaml pod/pod-hostpath created[root@master01 volumes]# kubectl get po -owide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATESpod-hostpath 1/1 Running 0 19s 172.20.59.237 master02 [root@master01 volumes]# kubectl describe po pod-hostpathName: pod-hostpathNamespace: defaultPriority: 0Node: master02/192.168.44.11Start Time: Thu, 15 Dec 2022 00:29:49 +0800Labels: Annotations: Status: RunningIP: 172.20.59.237IPs: IP: 172.20.59.237Containers: busybox3: Container ID: docker://b5c41f3d14da84558f6298bf7967c3bf825cce19898f5cbb308a9b8fc6c78da4 Image: busybox Image ID: docker-pullable://busybox@sha256:3b3128d9df6bbbcc92e2358e596c9fbd722a437a62bafbc51607970e9e3b8869 Port: Host Port: Command: /bin/sh -c sleep 3600 State: Running Started: Thu, 15 Dec 2022 00:29:50 +0800 Ready: True Restart Count: 0 Environment: Mounts: /test-k8s/hostpath-dir from hostpath-dir-volume (rw) /var/run/secrets/kubernetes.io/serviceaccount from default-token-c7jnm (ro)Conditions: Type Status Initialized True Ready True ContainersReady True PodScheduled True Volumes: hostpath-dir-volume: Type: HostPath (bare host directory volume) Path: /root/test_hostPath HostPathType: DirectoryOrCreate default-token-c7jnm: Type: Secret (a volume populated by a Secret) SecretName: default-token-c7jnm Optional: falseQoS Class: BestEffortNode-Selectors: Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300s node.kubernetes.io/unreachable:NoExecute op=Exists for 300sEvents: Type Reason Age From Message ---- ------ ---- ---- ------- Normal Scheduled 94s default-scheduler Successfully assigned default/pod-hostpath to master02 Normal Pulled 93s kubelet Container image "busybox" already present on machine Normal Created 93s kubelet Created container busybox3 Normal Started 93s kubelet Started container busybox3[root@master01 volumes]# ssh master02Last login: Wed Nov 23 04:15:58 2022 from 192.168.44.1[root@master02 ~]# lstest_hostPath[root@master02 ~]# cd test_hostPath/[root@master02 test_hostPath]# ls[root@master02 test_hostPath]# pwd/root/test_hostPath
测试hostpath
[root@master02 test_hostPath]# pwd/root/test_hostPath[root@master02 test_hostPath]# touch create_hostpath[root@master02 test_hostPath]# [root@master02 test_hostPath]# kubectl exec -ti pod-hostpath -- sh/ # lsbin dev etc home lib lib64 proc root sys test-k8s tmp usr var[root@master02 test_hostPath]# lscreate_hostpath[root@master02 test_hostPath]# kubectl exec -ti pod-hostpath -- sh/ # lsbin dev etc home lib lib64 proc root sys test-k8s tmp usr var/ # cd test-k8s/hostpath-dir//test-k8s/hostpath-dir # lscreate_hostpath