NFS安装
yum install nfs-utils -y # 所有k8s节点都需要安装
NFS服务端创建共享目录
[root@k8smaster5 ~]# mkdir /data/volumes -pv # pv 创建层级目录并显示详细信息
启动并查看NFS服务
[root@k8smaster5 volumes]# systemctl start nfs
[root@k8smaster5 volumes]# systemctl status nfs
修改默认配置文件
[root@k8smaster5 volumes]# cat /etc/exports
/data/volumes 192.168.0.220/24(rw,no_root_squash)
# 允许指定网段挂载目录
# no_root_squash 如果是root用户,具有root操作权限
NFS配置文件生效指令
[root@k8smaster5 volumes]# exportfs -arv
exporting *:/data/volumes # 允许任何客户端挂载此目录
重启NFS
[root@k8smaster5 volumes]# systemctl restart nfs
配置NFS开启自启动
[root@k8smaster5 volumes]# systemctl enable nfs
客户端Node配置NFS启用
[root@k8snode5 ~]# systemctl start nfs && systemctl enable nfs
Work 节点创建挂在目录
[root@k8snode4 ~]# mkdir /test
[root@k8snode4 ~]# mount 192.168.0.220:/data/volumes /test/
查看挂载结果
[root@k8snode4 ~]# df -h # 显示目前磁盘空间和使用情况
NFS卸载挂载
[root@k8snode4 ~]# umount /test/
创建Yaml 示例
[root@k8smaster4 dir]# cat nfs.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nfs-test
spec:
replicas: 3
selector:
matchLabels:
cunchu: nfs
template:
metadata:
labels:
cunchu: nfs
spec:
containers:
- name: test-nfs
image: docker.io/library/nginx
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
protocol: TCP
volumeMounts:
- name: nfs-volumes
mountPath: /usr/local/nginx/html/
volumes:
- nfs:
server: 192.168.0.220
path: /data/volumes
name: nfs-volumes
验证容器查看共享文件
[root@k8smaster4 dir]# kubectl exec -it nfs-test-58c8cd54d4-4srtw -c test-nfs -- /bin/bash
root@nfs-test-58c8cd54d4-4srtw:/# cd /usr/local/nginx/html/
root@nfs-test-58c8cd54d4-4srtw:/usr/local/nginx/html# ls
1.txt
root@nfs-test-58c8cd54d4-4srtw:/usr/local/nginx/html# exit
------------------
[root@k8smaster4 dir]# kubectl exec -it nfs-test-58c8cd54d4-xn587 -c test-nfs -- /bin/bash
root@nfs-test-58c8cd54d4-xn587:/# cd /usr/local/nginx/html/
root@nfs-test-58c8cd54d4-xn587:/usr/local/nginx/html# ls
1.txt