1. 基础配置
首先,在所有 master 节点和 worker 节点上安装 nfs-utils.
1sudo yum -y install nfs-utils
2systemctl enable nfs-utils
2. NFS Server 配置
- 服务端安装 nfs-utils 和 rpcbind
1sudo yum -y install nfs-utils rpcbind
2systemctl enable nfs-utils
3systemctl enable rpcbind
- 创建共享目录
1mkdir /hc-data/nfs
- **创建并修改配置文件
配置文件为 /etc/exports
1sudo vim /etc/exports
2# 添加配置
3/hc-data/nfs 172.24.71.32/24(rw,no_root_squash,no_all_squash,sync)
4/hc-data/nfs 172.24.71.34/24(rw,no_root_squash,no_all_squash,sync)
5/hc-data/nfs 172.24.71.35/24(rw,no_root_squash,no_all_squash,sync)
6/hc-data/nfs 172.24.71.36/24(rw,no_root_squash,no_all_squash,sync)
7/hc-data/nfs 172.24.71.37/24(rw,no_root_squash,no_all_squash,sync)
8/hc-data/nfs 172.24.71.38/24(rw,no_root_squash,no_all_squash,sync)
9/hc-data/nfs 172.24.71.39/24(rw,no_root_squash,no_all_squash,sync)
/opt/nfs 10.101.5.111/24(rw,no_root_squash,no_all_squash,sync) /opt/nfs 10.101.5.112/24(rw,no_root_squash,no_all_squash,sync) /opt/nfs 10.101.5.113/24(rw,no_root_squash,no_all_squash,sync) /opt/nfs 10.101.5.114/24(rw,no_root_squash,no_all_squash,sync) /opt/nfs 10.101.5.115/24(rw,no_root_squash,no_all_squash,sync) /opt/nfs 10.101.5.116/24(rw,no_root_squash,no_all_squash,sync) /opt/nfs 10.101.5.117/24(rw,no_root_squash,no_all_squash,sync)
对于 /hc-data/nfs 172.24.71.32/24(rw,no_root_squash,no_all_squash,sync)
,表示服务端允许 IP 地址为 172.24.71.32 的主机进行访问 /hc-data/nfs
目录,参数的含义表示可读写、数据同步写入等。
保存后,使配置生效,并重启服务
1sudo exportfs -r
2sudo service rpcbind restart
3sudo service nfs-server restart
- 查看服务端挂载情况
1showmount -e localhost
1# showmount -e localhost
2Export list for localhost:
3/hc-data/nfs 172.24.71.39/24,172.24.71.38/24,172.24.71.37/24,172.24.71.36/24,172.24.71.35/24,172.24.71.34/24,172.24.71.32/24
3. K8s 主节点配置
3.1 K8s NFS 插件安装
仓库地址: https://github.com/kubernetes-sigs/nfs-subdir-external-provisioner
- Helm 添加仓库
1helm repo add nfs-subdir-external-provisioner https://kubernetes-sigs.github.io/nfs-subdir-external-provisioner/
- 安装 nfs-subdir-external-provisioner
- helm 安装
1helm install nfs-subdir-external-provisioner nfs-subdir-external-provisioner/nfs-subdir-external-provisioner \
2--set nfs.server=172.24.71.33 \
3--set nfs.path=/hc-data/nfs
- 本地安装
- 下载 nfs-subdir-external-provisioner 到本地目录后
1helm pull nfs-subdir-external-provisioner/nfs-subdir-external-provisioner
- 修改
values.yaml
文件
然后执行
1helm install nfs-client-provisioner .
- 配置默认 StorageClass
配置默认 StorageClass 命令如下
1kubectl patch storageclass nfs-client -p '{"metadata": {"annotations":{"storageclass.kubernetes.io/is-default-class":"true"}}}'
然后,查看当前集群 StorageClass 情况,名为 nfs-client 的 sc 已经成功创建,并已设置为默认 sc。
1kubectl get sc
2NAME PROVISIONER RECLAIMPOLICY VOLUMEBINDINGMODE ALLOWVOLUMEEXPANSION AGE
3nfs-client (default) cluster.local/nfs-client-provisioner-nfs-subdir-external-provisioner Delete Immediate true 30m
3.2 创建 PVC
PVC 的 yaml 文件如下所示:
1apiVersion: v1
2kind: PersistentVolumeClaim
3metadata:
4 name: prometheus-pvc
5 namespace: monitoring
6spec:
7 storageClassName: nfs-client
8 accessModes:
9 - ReadWriteOnce
10 resources:
11 requests:
12 storage: 50Gi # 请求与 PersistentVolume 匹配的存储容量
执行 kubectl apply 后, 查看 PVC 构建情况, prometheus-pvc 的状态 bound, 构建成功。
1kubectl get pvc -n monitoring
2NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
3prometheus-pvc Bound pvc-6003bba1-95f7-43b0-a3f8-5bdce01f2c98 50Gi RWO nfs-client 58m