在k8s中部署nfs-client-provisioner

1、部署过程

1.1、环境依赖

在部署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

1.2、在集群的每个节点部署nfs工具

执行以下的命令,在每个节点部署nfs工具,后面pod启动,挂载nfs的存储会使用

yum install nfs-utils -y

1.3、 创建授权账户信息

通过以下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 

1.4、部署nfs-client-provisioner服务

创建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

1.5创建storageclass

动态存储的关联关系,都是通过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

1.6、创建pvc

创建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的目录

1.7、创建pod使用pvc

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了,不需要手动的进行创建。

2、工作原理简要说明

基本的工作原理就是,nfs-client-provisioner持续的监控api server,查看创建pvc的请求,如果发现请求的pvc的存储类stroageclass,存储类的提供者和自己的是一致的就会根据创建的pvc,动态的在nfs中创建pv。

关于nfs-client-provisioner工作流程的描述:

  • 部署 nfs-client-provisioner: 首先,您需要在 Kubernetes 集群中部署 nfs-client-provisioner。您可以通过使用 Kubernetes 配置文件或 Helm Chart 进行部署。这将创建一个运行在集群中的容器,用于监听 PVC 的创建。
  • 创建 StorageClass: 您需要创建一个 StorageClass,其中定义了与 nfs-client-provisioner 通信所需的配置信息,如 NFS 服务器地址、共享路径等。
  • 创建 PersistentVolumeClaim(PVC): 当您在集群中创建一个 PVC 并引用了之前定义的 StorageClass,nfs-client-provisioner 将监听 PVC 的创建。
  • nfs-client-provisioner 的处理: 一旦 PVC 创建,nfs-client-provisioner 会检测到 PVC 的存在。它会解析 PVC 中定义的存储需求和 StorageClass 的配置,然后使用这些信息来与 NFS 服务器交互。
  • 创建 PersistentVolume(PV):nfs-client-provisioner 会使用 PVC 请求中的信息,以及 StorageClass 的配置,通过 NFS 协议与 NFS 服务器进行通信,并在 NFS 服务器上创建一个目录以供存储。
  • 绑定 PV 和 PVC: 一旦 NFS 服务器上的目录创建成功,nfs-client-provisioner 将创建一个 PV,并将其与创建 PVC 绑定起来,从而使 PVC 获得一个可用的 PV。
  • PVC 使用: 现在,PVC 已经与一个动态创建的 PV 绑定。您可以在 Pod 中使用这个 PVC,使 Pod 能够挂载 NFS 存储并访问其中的数据。

通过这个流程,nfs-client-provisioner 实现了动态创建和管理基于 NFS 的 PV,减轻了管理员的工作负担,同时使开发人员能够更轻松地使用动态的存储资源。需要注意的是,确保您在 PVC 和 StorageClass 的定义中提供正确的配置信息,以确保 nfs-client-provisioner 能够与 NFS 服务器正确交互。

你可能感兴趣的:(kubernetes,linux,网络)