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 localhost
Export 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.193
Export list for 172.30.33.193:
/data/nfs 172.30.32.0/22
helm安装
curl -fsSL -o get_helm.sh https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3
chmod 700 get_helm.sh
./get_helm.sh
此时在另一台服务器上已经可以查看到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=10.168.1.61 --set nfs.path=/data/nfs-data --set storageClass.defaultClass=true
$ helm list -n kube-system
NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION
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
$ kubectl get sc
NAME PROVISIONER RECLAIMPOLICY VOLUMEBINDINGMODE ALLOWVOLUMEEXPANSION AGE
nfs-client (default) cluster.local/nfs-client-nfs-client-provisioner Delete Immediate false 18h
已经安装完毕,申请pvc时会通过storageclass自动申请pv
$ cat pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pvc1
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 100Mi
$ kubectl apply -f pvc.yaml
persistentvolumeclaim/pvc1 created
$ kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
pvc1 Bound pvc-c2c45851-c843-4198-9dec-ed5f66308e93 100Mi RWX nfs-client 4s
$ # kubectl get pvc pvc1 -o yaml
apiVersion: v1
kind: PersistentVolumeClaim
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 100Mi
storageClassName: nfs-client
volumeMode: Filesystem
volumeName: pvc-c2c45851-c843-4198-9dec-ed5f66308e93
status:
accessModes:
- ReadWriteMany
capacity:
storage: 100Mi
phase: Bound
可以看到pvc已经绑定了storageClassName
文档更新时间: 2023-03-30 11:57 作者:admin