K8S volume之nfs挂载
volume的挂载有非常多种方式,比较常用的有emptyDir hostPath,但是这两种方法有一个共同的缺点:数据不能跨主机调用和存储,只能由于pod的宿主机。实验nfs挂载方式就能很好地解决这个问题。
实验环境:
master 192.168.186.10
node-1 192.168.186.11
node-2 192.168.186.12
node-3 192.168.186.13
nfs 192.168.186.161
环境准备:关闭所有机器的防火墙,selinux,配置好yum源
配置NFS服务器
[root@nfs ~]#yum -y install nfs-utils
[root@nfs ~]#mkdir /nfs_share
[root@nfs ~]#vim /etc/exports (写入如下内容)
/nfs_share 192.168.186.0/24(rw,sync,no_root_squash)
[root@nfs ~]#systemctl start nfs
[root@nfs ~]#exportfs -v (出现以下内容说明共享成功)
/nfs_share 192.168.186.0/24(sync,wdelay,hide,no_subtree_check,sec=sys,rw,secure,no_root_squash,no_all_squash)
[root@nfs ~]#echo "nfs works." > /nfs_share/index.html (准备测试网页)
配置nfs客户端(所有node都要操作)
[root@node-1 ~]#yum -y install nfs-utils
[root@node-1 ~]#systemctl start nfs
[root@node-1 ~]#showmount -e 192.168.186.161 (在所有节点执行此命令,出现以下内容则说明成功)
Export list for 192.168.186.161:
/nfs_share 192.168.186.0/24
创建Pod(在master创建)
[root@master yaml]vim nfs_nginx.yaml
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-nfs
labels:
web: nginx
spec:
replicas: 3
selector:
matchLabels:
appname: nginx
template:
metadata:
labels:
appname: nginx
spec:
containers:
- name: nginx-nfs
image: nginx
volumeMounts:
- name: nfs-test
mountPath: /usr/share/nginx/html/
volumes:
- name: nfs-test
nfs:
server: 192.168.186.161
path: /nfs_share
[root@master yaml]# kubectl apply -f nfs_nginx.yaml
deployment.apps/nginx-nfs created
[root@master yaml]# kubectl get po -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-nfs-6c45d9df5f-dplst 1/1 Running 0 2m13s 10.244.2.92 node-2 <none> <none>
nginx-nfs-6c45d9df5f-qddhz 1/1 Running 0 2m13s 10.244.3.80 node-3 <none> <none>
nginx-nfs-6c45d9df5f-zxfkt 1/1 Running 0 2m13s 10.244.1.58 node-1 <none> <none>
[root@master yaml]# curl 10.244.2.92
nfs works.
重复删除、创建Pod,都能正常访问,说明实验成功!