所有节点安装nfs-utils -> yum install nfs-utils
找一台VM当作NFS服务器, 姑且将k8s-node2(192.168.231.123)作为NFS服务器
1) /etc/exports内容配置如下,路径/nfstest代表会将NFS服务器的这个路径挂载到NFS客户端
[root@k8s-node2 ~]# cat /etc/exports
/nfstest *(rw,no_root_squash)
[root@k8s-node2 ~]#
2) 在NFS服务器创建该路径 mkdir /nfstest
3) 重启NFS服务使配置生效 systemctl start nfs
编辑Deployment的yaml文件,将NFS服务器的/nfstest挂载到Pod路径 /usr/share/nginx/html
[root@k8s-master ~]# cat test-nfs.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: web-testnfs
spec:
selector:
matchLabels:
app: nginx
replicas: 3
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
volumeMounts:
- name: test-nfs
mountPath: /usr/share/nginx/html
volumes:
- name: test-nfs
nfs:
server: 192.168.231.123
path: /nfstest
[root@k8s-master ~]#
创建Deployment后可以看到一个Pod调度到了k8s-node1,两个Pod调度到了k8s-node2
[root@k8s-master ~]# kubectl apply -f test-nfs.yaml
deployment.apps/web-testnfs created
[root@k8s-master ~]#
[root@k8s-master ~]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE
web-testnfs-5d9998b54c-79nqk 1/1 Running 0 35s 10.244.36.124 k8s-node1 <none> <none>
web-testnfs-5d9998b54c-bksnd 1/1 Running 0 35s 10.244.169.141 k8s-node2 <none> <none>
web-testnfs-5d9998b54c-mnxfh 1/1 Running 0 35s 10.244.169.189 k8s-node2 <none> <none>
[root@k8s-master ~]#
进入第一个Pod的容器,到路径/usr/share/nginx/html/下touch一个文件
[root@k8s-master ~]# kubectl exec -it web-testnfs-5d9998b54c-79nqk -- bash
root@web-testnfs-5d9998b54c-79nqk:/# cd /usr/share/nginx/html/
root@web-testnfs-5d9998b54c-79nqk:/usr/share/nginx/html# ls
root@web-testnfs-5d9998b54c-79nqk:/usr/share/nginx/html# touch web-testnfs-replica1Came
root@web-testnfs-5d9998b54c-79nqk:/usr/share/nginx/html# eixt
进入第二个Pod的容器,在路径/usr/share/nginx/html/下可以看到刚才创建的文件,同时再touch一个文件
[root@k8s-master ~]# kubectl exec -it web-testnfs-5d9998b54c-bksnd -- bash
root@web-testnfs-5d9998b54c-bksnd:/# cd /usr/share/nginx/html
root@web-testnfs-5d9998b54c-bksnd:/usr/share/nginx/html# ls
web-testnfs-replica1Came
root@web-testnfs-5d9998b54c-bksnd:/usr/share/nginx/html# touch web-testnfs-replica2Came
root@web-testnfs-5d9998b54c-bksnd:/usr/share/nginx/html#
在NFS服务器的路径/nfstest 也可以看到刚才创建的两个文件
[root@k8s-node2 ~]# cd /nfstest/
[root@k8s-node2 nfstest]# ls -l
total 0
-rw-r--r-- 1 root root 0 Aug 28 14:27 web-testnfs-replica1Came
-rw-r--r-- 1 root root 0 Aug 28 14:28 web-testnfs-replica2Came
[root@k8s-node2 nfstest]#