Kubernetes集群部署NFS存储

说明

NFS存储由四部分组成deploy.yaml rbac.yaml storageclass.yaml

NFS_Server端安装部署

$ apt -y install nfs-utils rpcbind
$ apt -y install nfs-kernel-server
$ vim /etc/exports
/share/data1 *(rw,sync,no_root_squash)
$ systemctl restart nfs-kernel-server.service

在K8S 集群部署对应组件

RBAC

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: ["nodes"]
    verbs: ["get", "list", "watch"]
  - 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

storageclass

apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: qiqios-nfs-storage
  ### 如果取消annotations注释此storageclass将作为默认storageclass自动创建pv
  annotations:
    storageclass.kubernetes.io/is-default-class: "true"
provisioner: qiqios.com/nfs
parameters:
  archiveOnDelete: "false"

Deployment

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nfs-client-provisioner
  labels:
    app: nfs-client-provisioner
  # replace with namespace where provisioner is deployed
  namespace: default
spec:
  replicas: 1
  strategy:
    type: Recreate
  selector:
    matchLabels:
      app: nfs-client-provisioner
  template:
    metadata:
      labels:
        app: nfs-client-provisioner
    spec:
      serviceAccountName: nfs-client-provisioner
      containers:
        - name: nfs-client-provisioner
          image: docker.io/dyrnq/nfs-subdir-external-provisioner:v4.0.2 #主要NFS镜像必须使用这个版本
          volumeMounts:
            - name: nfs-client-root
              mountPath: /persistentvolumes
          env:
            - name: PROVISIONER_NAME
              value: qiqios.com/nfs
            - name: NFS_SERVER
              value: 192.168.1.200
            - name: NFS_PATH
              value: /share/data1
      volumes:
        - name: nfs-client-root
          nfs:
            server: 192.168.1.200  #NFS_server的地址
            path: /share/data1

创建上述组件

$ kubectl apply -f .
$ kubectl get deploy,sc  grep nfs
deployment.apps/nfs-client-provisioner                1/1     1            1           24d
storageclass.storage.k8s.io/qiqios-nfs-storage (default)   qiqios.com/nfs   Delete          Immediate           false                  24d

创建测试样例

kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: test-claim
  annotations:
    volume.beta.kubernetes.io/storage-class: qiqios-nfs-storage
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 1Mi
---
kind: Pod
apiVersion: v1
metadata:
  name: test-pod
spec:
  containers:
  - name: test-pod
    image: busybox:stable
    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

创建测试PV和POD 并查看

$ kubectl apply -f .
$ kubectl get pv
NAME                                       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM                                                                                                                    STORAGECLASS         REASON   AGE
pvc-dbc4823d-4bc9-40ee-b4f8-aa1024287828   1Mi        RWX            Delete           Bound    default/test-claim                                                                                                       qiqios-nfs-storage            9s
root@master1:~/pod/nfs# kubectl get pod
NAME                                                     READY   STATUS      RESTARTS         AGE
test-pod                                                 0/1     Completed   0                9s

去NFS_Server端查看文件

$ ll /share/data1/
total 20
drwxr-xr-x 5 root root 4096 Apr  5 18:32 ./
drwxr-xr-x 3 root root 4096 Mar 12 14:06 ../
drwxrwxrwx 2 root root 4096 Apr  5 18:32 default-test-claim-pvc-dbc4823d-4bc9-40ee-b4f8-aa1024287828/

$ ll /share/data1/default-test-claim-pvc-dbc4823d-4bc9-40ee-b4f8-aa1024287828/
total 8
drwxrwxrwx 2 root root 4096 Apr  5 18:32 ./
drwxr-xr-x 5 root root 4096 Apr  5 18:32 ../
-rw-r--r-- 1 root root    0 Apr  5 18:32 SUCCESS

说明: pvc-dbc4823d-4bc9-40ee-b4f8-aa1024287828 这个是对应pod使用的pv卷,pod创建的文件SUCCESS 也在其中。


Kubernetes集群部署NFS存储
http://www.qiqios.cn/2023/04/05/kubernetes集群部署nfs存储/
作者
一亩三分地
发布于
2023年4月5日
许可协议