在部署nfs-client-provisioner之前,需要先部署nfs服务。
因为,nfs-client-provisioner创建的pv都是要在nfs服务器中搭建的。
本示例中的nfs server的地址如下:
yum install nfs-utils -y
# 启动服务
# 注意先后顺序,先启动rpcbind,再启动nfs-server
systemctl start rpcbind
systemctl start nfs
# 开机启动
systemctl enable rpcbind
systemctl enable nfs
# 创建共享目录
mkdir -p /data/nfs
# 修改共享目录权限
chmod -R 777 /data/nfs
# 修改配置文件
vim /etc/exports
# 添加共享目录
#/data/nfs/rw *(rw,sync,no_root_squash,no_all_squash)
/data/nfs/rw 192.168.20.0/24(rw,sync,no_subtree_check,no_root_squash)
# 重启服务
systemctl restart rpcbind
systemctl restart nfs
# 查看服务状态
systemctl status rpcbind
systemctl status nfs
# 查看共享目录
showmount -e nfs_server_ip
# 查看rpc服务
rpcinfo -p nfs_server_ip
[root@node1 /]# showmount -e
Export list for node1:
/data/nfs/rw 192.168.20.0/24
执行以下的命令,在每个节点部署nfs工具,后面pod启动,挂载nfs的存储会使用
yum install nfs-utils -y
通过以下yaml文件,创建需要的service account
创建 rbac.yaml
apiVersion: v1
kind: ServiceAccount
metadata:
name: nfs-client-provisioner
# replace with namespace where provisioner is deployed
namespace: default
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: nfs-client-provisioner-runner
rules:
- apiGroups: [""]
resources: ["persistentvolumes"]
verbs: ["get", "list", "watch", "create", "delete"]
- apiGroups: [""]
resources: ["persistentvolumeclaims"]
verbs: ["get", "list", "watch", "update"]
- apiGroups: ["storage.k8s.io"]
resources: ["storageclasses"]
verbs: ["get", "list", "watch"]
- apiGroups: [""]
resources: ["events"]
verbs: ["create", "update", "patch"]
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: run-nfs-client-provisioner
subjects:
- kind: ServiceAccount
name: nfs-client-provisioner
# replace with namespace where provisioner is deployed
namespace: default
roleRef:
kind: ClusterRole
name: nfs-client-provisioner-runner
apiGroup: rbac.authorization.k8s.io
---
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: leader-locking-nfs-client-provisioner
# replace with namespace where provisioner is deployed
namespace: default
rules:
- apiGroups: [""]
resources: ["endpoints"]
verbs: ["get", "list", "watch", "create", "update", "patch"]
---
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: leader-locking-nfs-client-provisioner
# replace with namespace where provisioner is deployed
namespace: default
subjects:
- kind: ServiceAccount
name: nfs-client-provisioner
# replace with namespace where provisioner is deployed
namespace: default
roleRef:
kind: Role
name: leader-locking-nfs-client-provisioner
apiGroup: rbac.authorization.k8s.io
kubectl apply -f rbac.yaml
创建nfs_client_provisioner.yaml
kind: Deployment
apiVersion: apps/v1
metadata:
name: nfs-client-provisioner
spec:
replicas: 1
selector:
matchLabels:
app: nfs-client-provisioner
strategy:
type: Recreate
template:
metadata:
labels:
app: nfs-client-provisioner
spec:
serviceAccountName: nfs-client-provisioner
containers:
- name: nfs-client-provisioner
image: quay.io/vbouchaud/nfs-client-provisioner:latest
volumeMounts:
- name: nfs-client-root
mountPath: /persistentvolumes
env:
- name: PROVISIONER_NAME
value: fuseim.pri/ifs
- name: NFS_SERVER
value: 192.168.20.11
- name: NFS_PATH
value: /data/nfs/rw
volumes:
- name: nfs-client-root
nfs:
server: 192.168.20.11
path: /data/nfs/rw
创建: kubectl apply -f nfs_client_provisioner.yaml 查看nfs-client pod的状态:kubectl get pod | grep nfs
动态存储的关联关系,都是通过stroageclass来完成的。通过下面的yaml文件,创建storageclass
创建nfs-storage-class.yaml
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: managed-nfs-storage
provisioner: fuseim.pri/ifs
parameters:
archiveOnDelete: "false"
创建: kubectl apply -f nfs-storage-class.yaml
查询: kubectl get sc
创建nfs-pvc.yaml
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: test-claim
annotations:
volume.beta.kubernetes.io/storage-class: "managed-nfs-storage"
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 10Mi
执行: kubectl apply -f nfs-pvc.yaml
查询:kubectl get pvc
这个时候会在nfs的目录创建出一个目录。这个目录就是pv的目录
pvc已经创建好了,创建测试pod,使用pvc
创建test-pod.yaml 文件
kind: Pod
apiVersion: v1
metadata:
name: test-pod
spec:
containers:
- name: test-pod
image: busybox:1.36
command:
- "/bin/sh"
args:
- "-c"
- "touch /mnt/SUCCESS && exit 0 || exit 1"
volumeMounts:
- name: nfs-pvc
mountPath: "/mnt"
restartPolicy: "Never"
volumes:
- name: nfs-pvc
persistentVolumeClaim:
claimName: test-claim
kubectl apply -f test-pod.yaml
进入nfs server的目录里,已经创建好了SUCCESS文件
OK,到这里nfs-client-provisioner工具部署成功。
部署完成nfs-client-provisioner之后,创建好pvc,就可以动态的创建nfs的pv了,不需要手动的进行创建。
基本的工作原理就是,nfs-client-provisioner持续的监控api server,查看创建pvc的请求,如果发现请求的pvc的存储类stroageclass,存储类的提供者和自己的是一致的就会根据创建的pvc,动态的在nfs中创建pv。
关于nfs-client-provisioner工作流程的描述:
通过这个流程,nfs-client-provisioner 实现了动态创建和管理基于 NFS 的 PV,减轻了管理员的工作负担,同时使开发人员能够更轻松地使用动态的存储资源。需要注意的是,确保您在 PVC 和 StorageClass 的定义中提供正确的配置信息,以确保 nfs-client-provisioner 能够与 NFS 服务器正确交互。