系统:centos7
kubernetes:v1.19
1、准备nfs-server
选择一台服务器安装nfs-server
$ yum -y install nfs-utils rpcbind
修改nfs-server的配置
$ mkdir -p /data/nfs$ chmod 0755 /data/nfs$ 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,设置目录的权限
然后使配置生效
$ exportfs -r$ systemctl enable rpcbind$ systemctl enable nfs-server
查看挂载情况
$ showmount -e localhostExport list for localhost:/data/nfs 172.30.32.0/22
2、在kubernetes上安装nfs-client
在所有节点安装nfs客户端
$ yum -y install nfs-utils$ showmount -e 172.30.33.193Export list for 172.30.33.193:/data/nfs 172.30.32.0/22
此时在另一台服务器上已经可以查看到nfs挂载的目录。
helm安装nfs-client
$ helm repo add nfs-subdir-external-provisioner https://kubernetes-sigs.github.io/nfs-subdir-external-provisioner/$ helm install -n kube-system nfs-client nfs-subdir-external-provisioner/nfs-subdir-external-provisioner –set nfs.server=172.30.33.193 –set nfs.path=/data/nfs –set storageClass.defaultClass=true $ helm list -n kube-systemNAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSIONnfs-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
$ kubectl get scNAME PROVISIONER RECLAIMPOLICY VOLUMEBINDINGMODE ALLOWVOLUMEEXPANSION AGEnfs-client (default) cluster.local/nfs-client-nfs-client-provisioner Delete Immediate false 18h
已经安装完毕,申请pvc时会通过storageclass自动申请pv
$ cat pvc.yamlapiVersion: v1kind: PersistentVolumeClaimmetadata: name: pvc1spec: accessModes: – ReadWriteMany resources: requests: storage: 100Mi$ kubectl apply -f pvc.yamlpersistentvolumeclaim/pvc1 created$ kubectl get pvcNAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGEpvc1 Bound pvc-c2c45851-c843-4198-9dec-ed5f66308e93 100Mi RWX nfs-client 4s$ # kubectl get pvc pvc1 -o yamlapiVersion: v1kind: PersistentVolumeClaimspec: accessModes: – ReadWriteMany resources: requests: storage: 100Mi storageClassName: nfs-client volumeMode: Filesystem volumeName: pvc-c2c45851-c843-4198-9dec-ed5f66308e93status: accessModes: – ReadWriteMany capacity: storage: 100Mi phase: Bound
可以看到pvc已经绑定了storageClassName