kubernetes-Volumes
Volumes简介
Container 中的文件在磁盘上是临时存放的,这给 Container 中运行的较重要的应用 程序带来一些问题。问题之一是当容器崩溃时文件丢失。kubelet 会重新启动容器, 但容器会以干净的状态重启。 第二个问题会在同一 Pod 中运行多个容器并共享文件时出现。 Kubernetes 卷(Volume) 这一抽象概念能够解决这两个问题。
emptyDir卷 (临时存储卷)
当 Pod 被分配给节点时,⾸先创建 emptyDir 卷,并且只要该 Pod 在该节点上运⾏,该卷就会存在。正如卷的名字所述,它最初是空的。Pod 中的容器可以读取和写⼊ emptyDir 卷中的相同⽂件,尽管该卷可以挂载到每个容器中的相同或不同路径上。当出于任何原因从节点中删除 Pod 时, emptyDir 中的数据将被永久删除。
注意:容器崩溃不会从节点中移除pod,因此emptyDir卷中的数据在容器崩溃时是安全的。
emptyDir的用法有:
暂存空间,例如用于基于磁盘的合并排序
用作长时间计算崩溃恢复时的检查点
Web服务器容器提供数据时,保存内容管理器容器提取的文件
创建emptyDir卷
[root@server2 ~]# mkdir volumes
[root@server2 ~]# cd volumes/
[root@server2 volumes]# ls
[root@server2 volumes]# vim vol1.yaml
[root@server2 volumes]# cat vol1.yaml
apiVersion: v1
kind: Pod
metadata:
name: vol1
spec:
containers:
- image: busyboxplus
name: vm1
command: ["sleep", "300"]
volumeMounts:
- mountPath: /cache
name: cache-volume
- name: vm2
image: nginx
volumeMounts:
- mountPath: /usr/share/nginx/html
name: cache-volume
volumes:
- name: cache-volume
emptyDir:
medium: Memory
sizeLimit: 100Mi
[root@server2 volumes]# kubectl apply -f vol1.yaml
pod/vol1 created
[root@server2 volumes]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
vol1 2/2 Running 0 12s 10.244.141.227 server3 <none> <none>
访问10.244.141.227
[root@server2 ~]# curl 10.244.141.227
<html>
<head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.19.2</center>
</body>
</html>
创建默认发布文件再次访问
[root@server2 ~]# kubectl exec -it vol1 -- sh
Defaulted container "vm1" out of: vm1, vm2
/ # cd /cache/
/cache # ls
/cache # echo www.westos.org > index.html
/cache # exec attach failed: error on attach stdin: read escape sequence
command terminated with exit code 126
[root@server2 ~]# curl 10.244.141.227
www.westos.org
volumes限制为100MB
创建一个200mMB的文件会发现pod崩溃
[root@server2 ~]# kubectl exec -it vol1 -- sh
Defaulted container "vm1" out of: vm1, vm2
/ # cd cache/
/cache # dd if=/dev/zero of=/cache/bigfile bs=1M count=200
200+0 records in
200+0 records out
/cache # du -sh bigfile
200.0M bigfile
/cache # exec attach failed: error on attach stdin: read escape sequence
command terminated with exit code 126
[root@server2 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
vol1 0/2 Evicted 0 15m
hostPath卷
hostPath类型的存储卷是指将工作节点上某个文件系统的目录或文件挂载于Pod中的一种存储卷,它独立于Pod资源的生命周期,因而具有持久性。但它是工作节点本地的存储空间,仅适用于特定情况下的存储卷使用要求,例如,将工作节点上的文件系统关联为Pod的存储卷,从而使得容器访问节点文件系统上的数据。这一点在运行有管理任务的系统级Pod资源需要访问节点上的文件时尤为有用。
hostPath的用途如如下:
运行需要访问Docker内部的容器;使用/var/lib/docker的hostPath
在容器中运行cAdvisor;使用/dev/cgroups的hostPath
运行pod指定给定的hostPath是否应该在pod运行之前存在,是否应该创建,以及它应该以什么形式存在
查看pod调度节点是否创建相关目录
创建一个hostPath卷
[root@server2 volumes]# vim host.yaml
[root@server2 volumes]# cat host.yaml
apiVersion: v1
kind: Pod
metadata:
name: test-pd
spec:
containers:
- image: nginx
name: test-container
volumeMounts:
- mountPath: /test-pd
name: test-volume
volumes:
- name: test-volume
hostPath:
path: /data
type: DirectoryOrCreate
[root@server2 volumes]# kubectl apply -f host.yaml
pod/test-pd created
[root@server2 volumes]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
test-pd 1/1 Running 0 11s 10.244.141.228 server3 <none> <none>
在node节点查看路径
[root@server3 ~]# cd /data
[root@server3 data]# ls
[root@server3 data]# pwd
/data
进入容器测试
[root@server2 volumes]# kubectl exec -it test-pd -- sh
# pwd
/
# ls
bin docker-entrypoint.d home media proc sbin test-pd var
boot docker-entrypoint.sh lib mnt root srv tmp
dev etc lib64 opt run sys usr
# cd test-pd
# pwd
/test-pd
# touch file1
# ls
file1
在节点查看
[root@server3 ~]# cd /data
[root@server3 data]# ls
[root@server3 data]# pwd
/data
[root@server3 data]# ls
file1
[root@server3 data]#
nfs存储卷
nfs卷能将NFS(网络文件系统)挂载到你的pod中。不像emptyDir,当删除Pod时,nfs卷的内容被保留,卷仅仅是被卸载。另外,NFS是文件系统级共享服务,它支持同时存在的多路挂载请求。
注意:要使用nfs,必须先拥有自己的NFS服务器,然后才能使用。
共享文件系统nfs,首先在所有结点上安装nfs,在仓库结点上配置nfs
[root@server1 ~]# yum install -y nfs-utils
[root@server2 ~]# yum install -y nfs-utils
[root@server3 ~]# yum install -y nfs-utils
[root@server4 ~]# yum install -y nfs-utils
server1仓库节点:
[root@server1 ~]# vim /etc/exports
[root@server1 ~]# cat /etc/exports
/mnt/nfs *(rw)
[root@server1 ~]# systemctl start nfs
[root@server1 ~]# showmount -e
Export list for server1:
/mnt/nfs *
server2:

本文详细介绍了Kubernetes中的几种存储卷类型,包括emptyDir卷的临时存储特性,hostPath卷如何连接节点文件系统,nfs卷的持久化存储优势,以及PersistentVolume和PersistentVolumeClaim在NFS上的静态和动态分配。此外,还讨论了StatefulSet如何通过Headless Service保持Pod的拓扑状态。
最低0.47元/天 解锁文章
324

被折叠的 条评论
为什么被折叠?



