1 ) 概述
.spec.volumes
字 段 ).spec.containers.volumeMounts
字段)2 ) 关于 emptyDir
3 )关于 hostPath
hostPath卷能将主机节点文件系统上的文件或目录挂载到Pod中
虽然这不是大多数Pod需要的
但是它为一些应用程序提供了强大的持久化能力
现在写一个应用 $ vi vol-hostpath.yaml
apiVersion: v1
kind: Pod
metadata:
name: hostpath-pod
spec:
containers:
- name: test-container
image: nginx
volumeMounts:
- mountPath: /test-nginx
name: myhostpath
volumes:
- name: myhostpath
hostPath:
path: /tmp/nginx
type: DirectoryOrCreate # 不存在目录则创建
$ kubectl apply -f vol-hostpath.yaml
pod/hostpath-pod created
$ kubectl get pod
NAME READY STATUS RESTARTS AGE
hostpath-pod 1/1 Running 0 18m
$ kubectl exec -it hostpath-pod -- sh
进入 pod
$ ls | grep test
test-nginx
$ cd test-nginx
$ touch a.txt
$ echo 1 > a.txt
这时候在pod内 /test-nginx/a.txt 中写入了 1
现在,退出 pod, $ exit
查看pod被部署到了哪台node节点上,$ kubectl describe pod hostpath-pod | grep Node
Node: node1.k8s/10.211.55.11
Node-Selectors:
好,定位到这个node机器上,执行 $ cat /tmp/nginx/a.txt
1
可以看到,数据是同步的了,当然 hostPath 仍然是有局限的
1 )概述
2 ) 安装 nfs 服务
在 Master 和 所有Worker Node 安装 nfs 服务
yum install -y nfs-utils rpcbind
centos 8 可以用 yum, centos 9已经使用dnf来sudo dnf install nfs-utils rpcbind
centos 9 安装在 Master 节点配置 $ vi /etc/exports
修改配置
/nfsdata *(rw,sync,no_root_squash)
在 Master 节点 $ sudo exportfs -ra
修改了/etc/exports,需要重新激活配置
sudo exportfs -ra
在 Master 节点启动 NFS 服务和 rpcbind,并设置它们在系统启动时自动运行
sudo systemctl start rpcbind
sudo systemctl start nfs-server
sudo systemctl enable rpcbind
sudo systemctl enable nfs-server
在 Master节点检查 NFS 服务和 rpcbind 是否运行正常
sudo systemctl status rpcbind
sudo systemctl status nfs-server
在 Master 节点查看挂载效果 $ showmount -e master
Export list for master:
/nfsdata *
3 ) 在 node 节点 运行服务
vi nfs-demo1.yaml
编写yaml文件进行测试apiVersion: v1
kind: Pod
metadata:
name: nfs-pd
spec:
containers:
- name: test-container
image: nginx
volumeMounts: # 上面的容器内挂载目录为
- mountPath: /usr/share/nginx/html
name: test-volume
volumes: # 物理机
- name: test-volume
nfs:
server: master.k8s # 这里配置的是 master 节点的 hostname
path: /nfsdata
kubectl get po -w
监控 nfs pod 的状态NAME READY STATUS RESTARTS AGE
nfs-pd 1/1 Running 0 36s
kubectl exec -it nfs-pd -- sh
进入podmount
执行 mount 看容器起来后,挂载哪些卷,这里是全部的overlay on / type overlay (rw,relatime,lowerdir=/var/lib/docker/overlay2/l/TGJN2AK5DUCHXGQSXLTICMH6CV:/var/lib/docker/overlay2/l/4EYRGAW5MFRGLIJKCOJFKWNRDT:/var/lib/docker/overlay2/l/Y5QO2NJ6REXYEVRYLCHKIWZOLG:/var/lib/docker/overlay2/l/T5HUWMG4LKHET6APDBVJ25QIPE:/var/lib/docker/overlay2/l/PCJEGNJOYLSPYYP5TJBFNNAPF6:/var/lib/docker/overlay2/l/4EUOTLMHJJFA2AXOALXRHNAIOA:/var/lib/docker/overlay2/l/7WCTJFFWY4TZNAN3GM4JWTIBSV:/var/lib/docker/overlay2/l/UI7F22MIRB2NZSHHGF6U6IJU2U,upperdir=/var/lib/docker/overlay2/f3010a94dda626c9b3f8249c61f03b3e6caf6557b1193d7f9979308f7bf8d4e5/diff,workdir=/var/lib/docker/overlay2/f3010a94dda626c9b3f8249c61f03b3e6caf6557b1193d7f9979308f7bf8d4e5/work)
proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)
tmpfs on /dev type tmpfs (rw,nosuid,size=65536k,mode=755,inode64)
devpts on /dev/pts type devpts (rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=666)
sysfs on /sys type sysfs (ro,nosuid,nodev,noexec,relatime)
cgroup on /sys/fs/cgroup type cgroup2 (ro,nosuid,nodev,noexec,relatime,nsdelegate,memory_recursiveprot)
mqueue on /dev/mqueue type mqueue (rw,nosuid,nodev,noexec,relatime)
shm on /dev/shm type tmpfs (rw,nosuid,nodev,noexec,relatime,size=65536k,inode64)
/dev/mapper/cs-root on /dev/termination-log type xfs (rw,relatime,attr2,inode64,logbufs=8,logbsize=32k,noquota)
/dev/mapper/cs-root on /etc/resolv.conf type xfs (rw,relatime,attr2,inode64,logbufs=8,logbsize=32k,noquota)
/dev/mapper/cs-root on /etc/hostname type xfs (rw,relatime,attr2,inode64,logbufs=8,logbsize=32k,noquota)
/dev/mapper/cs-root on /etc/hosts type xfs (rw,relatime,attr2,inode64,logbufs=8,logbsize=32k,noquota)
master.k8s:/nfsdata on /usr/share/nginx/html type nfs4 (rw,relatime,vers=4.2,rsize=262144,wsize=262144,namlen=255,hard,proto=tcp,timeo=600,retrans=2,sec=sys,clientaddr=10.211.55.12,local_lock=none,addr=10.211.55.13)
tmpfs on /run/secrets/kubernetes.io/serviceaccount type tmpfs (ro,relatime,size=1704516k,inode64)
proc on /proc/asound type proc (ro,nosuid,nodev,noexec,relatime)
proc on /proc/bus type proc (ro,nosuid,nodev,noexec,relatime)
proc on /proc/fs type proc (ro,nosuid,nodev,noexec,relatime)
proc on /proc/irq type proc (ro,nosuid,nodev,noexec,relatime)
proc on /proc/sys type proc (ro,nosuid,nodev,noexec,relatime)
proc on /proc/sysrq-trigger type proc (ro,nosuid,nodev,noexec,relatime)
tmpfs on /proc/acpi type tmpfs (ro,relatime,inode64)
tmpfs on /proc/kcore type tmpfs (rw,nosuid,size=65536k,mode=755,inode64)
tmpfs on /proc/keys type tmpfs (rw,nosuid,size=65536k,mode=755,inode64)
tmpfs on /proc/timer_list type tmpfs (rw,nosuid,size=65536k,mode=755,inode64)
tmpfs on /proc/scsi type tmpfs (ro,relatime,inode64)
tmpfs on /sys/firmware type tmpfs (ro,relatime,inode64)
mount | grep nfs
查看 nfs 挂载效果master.k8s:/nfsdata on /usr/share/nginx/html type nfs4 (rw,relatime,vers=4.2,rsize=262144,wsize=262144,namlen=255,hard,proto=tcp,timeo=600,retrans=2,sec=sys,clientaddr=10.211.55.12,local_lock=none,addr=10.211.55.13)
cd /usr/share/nginx/html
touch x.txt
echo nfs > x.txt
cat x.txt
nfs
4 )回到 master 节点上检查
现在回到 master.k8s 这个 master 节点上查看
cat /nfsdata/x.txt
nfs
现在,可以看到,nfs 服务正常工作了
5 ) 总结