网络存储卷之nfs ¶
搭建 nfs 服务器 ¶
搭建 nfs 服务器
1. 安装配置 nfs
[root@k8smaster001 nfs-pvc]# yum -y install nfs-utils rpcbind -y
2. 创建 /nfs-data/nfs 为 nfs 数据目录
[root@k8smaster001 nfs-pvc]# mkdir /nfs-data/nfs -p
3. 默认配置文件 /etc/exports 下,在该文件中添加下面的配置信息
vim /etc/exports
/nfs-data/nfs *(rw,no_root_squash,sync)
4. 启动 nfs 服务
systemctl restart nfs-server && systemctl enable nfs-server
5. 所有node节点安装nfs客户端相关软件包
yum install nfs-utils -y
6. 验证 fs 可用性
[root@k8sworker001 ~]# showmount -e 192.168.3.201
Export list for 192.168.3.201:
/nfs-data/nfs *
创建应用清单使用 nfs
1. master 节点上创建 volume-nfs.yml 文件
apiVersion: apps/v1
kind: Deployment
metadata:
name: volume-nfs
spec:
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.15-alpine
imagePullPolicy: IfNotPresent
volumeMounts:
- name: documentroot
mountPath: /usr/share/nginx/html
ports:
- containerPort: 80
volumes:
- name: documentroot
nfs:
server: 192.168.3.201
path: /nfs-data/nfs/
2. 应用 yaml 创建
[root@k8smaster001 nfs]# kubectl apply -f volume-nfs.yml
deployment.apps/volume-nfs created
3. 在 nfs 服务器共享目录中创建验证文件
[root@k8smaster001 nfs]# echo "volume-nfs" > /nfs-data/nfs/index.html
4. 在 pod 中验证, 发现可以直接访问到,查看容器内文件也存在
[root@k8smaster002 ~]# kubectl get pod -owide|grep volume-nfs
volume-nfs-74cbcccff6-5b97p 1/1 Running 0 3m13s 10.244.35.43 k8sworker002 <none> <none>
volume-nfs-74cbcccff6-szwk9 1/1 Running 0 3m13s 10.244.87.206 k8sworker001 <none> <none>
[root@k8smaster002 ~]# curl 10.244.35.43
volume-nfs
[root@k8smaster002 ~]# curl 10.244.87.206
volume-nfs
[root@k8smaster002 ~]# kubectl exec -it volume-nfs-74cbcccff6-5b97p -- cat /usr/share/nginx/html/index.html
volume-nfs
5. 删除 pod 再次启动,验证文件是否存在
[root@k8smaster001 nfs]# kubectl delete -f volume-nfs.yml
deployment.apps "volume-nfs" deleted
[root@k8smaster001 nfs]# kubectl apply -f volume-nfs.yml
deployment.apps/volume-nfs created
6. 发现 容器已经改变、查看文件还存在,证明了 pod 摧毁 文件存在本地
[root@k8smaster002 ~]# kubectl get pod -owide|grep volume-nfs
volume-nfs-74cbcccff6-dctr7 1/1 Running 0 61s 10.244.35.44 k8sworker002 <none> <none>
volume-nfs-74cbcccff6-tftmh 1/1 Running 0 61s 10.244.87.193 k8sworker001 <none> <none>
[root@k8smaster002 ~]# curl 10.244.35.44
volume-nfs
[root@k8smaster002 ~]# curl 10.244.87.193
volume-nfs
[root@k8smaster002 ~]# kubectl exec -it volume-nfs-74cbcccff6-dctr7 -- cat /usr/share/nginx/html/index.html
volume-nfs