(1)所有k8s节点安装nfs相关软件包
[root@master231 pod]# yum -y install nfs-utils
[root@worker232 ~]# yum -y install nfs-utils
[root@worker233 ~]# yum -y install nfs-utils
(2)nfs服务端设置共享目录
[root@master231 pod]# mkdir -pv /oldboyedu/data/kubernetes
[root@master231 pod]# cat > /etc/exports <<'EOF'
/oldboyedu/data/kubernetes *(rw,no_root_squash)
EOF
(3)配置nfs服务开机自启动
[root@master231 pod]# systemctl enable --now nfs
(4)服务端检查NFS挂载信息,如上图所示。
[root@master231 pod]# exportfs
(5)客户端节点手动挂载测试
[root@worker233 ~]# mount -t nfs master231:/oldboyedu/data/kubernetes /mnt/
[root@worker233 ~]#
[root@worker233 ~]# cp /etc/os-release /mnt/
[root@worker233 ~]#
[root@worker233 ~]#
[root@worker233 ~]# ll /mnt/
total 4
-rw-r--r-- 1 root root 393 Jun 16 16:23 os-release
[root@worker233 ~]#
[root@worker233 ~]# umount /mnt
[root@worker233 ~]#
[root@worker233 ~]# ll /mnt/
total 0
[root@worker233 ~]#
(6)编写资源清单
[root@master231 pod]# cat 13-pods-volumes-nfs.yaml
apiVersion: v1
kind: Pod
metadata:
name: linux86-volumes-nfs-003
spec:
# 定义存储卷
volumes:
# 声明存储卷的名称
- name: data
# 声明存储卷类型为hostPath
# hostPath:
# # 指定宿主机的路径
# path: /data
# emptyDir: {}
# 声明nfs类型
nfs:
server: master231
path: /oldboyedu/data/kubernetes
nodeName: worker232
containers:
- name: web
image: harbor.oldboyedu.com/oldboyedu-web/nginx:1.25.1-alpine
# 挂载存储卷
volumeMounts:
# 要挂载的存储卷名称
- name: data
# 指定容器的挂载点
mountPath: /usr/share/nginx/html
---
apiVersion: v1
kind: Pod
metadata:
name: linux86-volumes-nfs-004
spec:
volumes:
- name: data
# hostPath:
# path: /data
# emptyDir: {}
nfs:
server: master231
path: /oldboyedu/data/kubernetes
nodeName: worker233
containers:
- name: linux
image: harbor.oldboyedu.com/oldboyedu-linux/alpine:latest
stdin: true
volumeMounts:
- name: data
mountPath: /oldboyedu-linux86