1、准备nfs-server
选择一台服务器安装nfs-server

  1. $ yum -y install nfs-utils rpcbind

修改nfs-server的配置

  1. $ mkdir -p /data/nfs
  2. $ chmod 0755 /data/nfs
  3. $ echo "/data/nfs 172.30.32.0/22(rw,no_root_squash,no_all_squash,sync)" >> /etc/exports

参数有:

rw、ro:该目录分享权限是可读写(read-write)或只读(read-only)
sync、async:sync代表数据会同步写入到内存和硬盘中,async表示数据会暂存在内存,而非直接写入硬盘
no_root_squash、root_squash:客户端root的身份会由root_squash的设定压缩成nfsnobody。如果想开放客户端使用root身份来操作服务器的文件系统,需要开启no_root_squash
no_all_squash、all_squash:客户端的身份被压缩成nobody(nfsnobody),如果想开放客户端使用者身份,需要开启no_all_squash
anonuid、anongid:anno是anonymous(匿名者),uid和gid是用户id和组id,设置目录的权限

然后使配置生效

  1. $ exportfs -r
  2. $ systemctl enable rpcbind
  3. $ systemctl enable nfs-server

查看挂载情况

  1. $ showmount -e localhost
  2. Export list for localhost:
  3. /data/nfs 172.30.32.0/22

2、在kubernetes上安装nfs-client
在所有节点安装nfs客户端

  1. $ yum -y install nfs-utils
  2. $ showmount -e 172.30.33.193
  3. Export list for 172.30.33.193:
  4. /data/nfs 172.30.32.0/22

helm安装

  1. curl -fsSL -o get_helm.sh https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3
  2. chmod 700 get_helm.sh
  3. ./get_helm.sh

此时在另一台服务器上已经可以查看到nfs挂载的目录。
helm安装nfs-client

  1. $ helm repo add nfs-subdir-external-provisioner https://kubernetes-sigs.github.io/nfs-subdir-external-provisioner/
  2. helm install -n kube-system nfs-client nfs-subdir-external-provisioner/nfs-subdir-external-provisioner --set nfs.server=10.168.1.61 --set nfs.path=/data/nfs-data --set storageClass.defaultClass=true
  3. $ helm list -n kube-system
  4. NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION
  5. nfs-client kube-system 1 2022-06-13 16:56:00.304704446 +0800 CST deployed nfs-subdir-external-provisioner-4.0.11 4.0.2

查看storageclass

  1. $ kubectl get sc
  2. NAME PROVISIONER RECLAIMPOLICY VOLUMEBINDINGMODE ALLOWVOLUMEEXPANSION AGE
  3. nfs-client (default) cluster.local/nfs-client-nfs-client-provisioner Delete Immediate false 18h

已经安装完毕,申请pvc时会通过storageclass自动申请pv

  1. $ cat pvc.yaml
  2. apiVersion: v1
  3. kind: PersistentVolumeClaim
  4. metadata:
  5. name: pvc1
  6. spec:
  7. accessModes:
  8. - ReadWriteMany
  9. resources:
  10. requests:
  11. storage: 100Mi
  1. $ kubectl apply -f pvc.yaml
  2. persistentvolumeclaim/pvc1 created
  3. $ kubectl get pvc
  4. NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
  5. pvc1 Bound pvc-c2c45851-c843-4198-9dec-ed5f66308e93 100Mi RWX nfs-client 4s
  6. $ # kubectl get pvc pvc1 -o yaml
  7. apiVersion: v1
  8. kind: PersistentVolumeClaim
  9. spec:
  10. accessModes:
  11. - ReadWriteMany
  12. resources:
  13. requests:
  14. storage: 100Mi
  15. storageClassName: nfs-client
  16. volumeMode: Filesystem
  17. volumeName: pvc-c2c45851-c843-4198-9dec-ed5f66308e93
  18. status:
  19. accessModes:
  20. - ReadWriteMany
  21. capacity:
  22. storage: 100Mi
  23. phase: Bound

可以看到pvc已经绑定了storageClassName

文档更新时间: 2023-03-30 11:57   作者:admin