kubernetes创建nfs存储空间

在给kubenetes创建nfs存储空间之前,我们必须先有一个nfs服务器。我们可以参照此文来创建一个nfs服务器。创建好后,参照下文为kubenetes创建nfs供应系统

Deploying Service Account and Role Bindings

Next, we’ll configure a service account and role bindings. We’ll use role-based access control to do the configuration. First step is to download the nfs-provisioning repo and change into the nfs-provisioning directory.

  • git clone https://exxsyseng@bitbucket.org/exxsyseng/nfs-provisioning.git
  • cd nfs-provisioning

In this directory we have 4 files. (class.yaml default-sc.yaml deployment.yaml rbac.yaml) We will use the rbac.yaml file to create the service account for nfs and cluster role and bindings.

  • [vagrant@kmaster nfs-provisioning]$ kubectl create -f rbac.yaml

We can verify that the service account, clusterrole and binding was created.

  • [vagrant@kmaster nfs-provisioning]$ kubectl get clusterrole,clusterrolebinding,role,rolebinding | grep nfs

clusterrole.rbac.authorization.k8s.io/nfs-client-provisioner-runner 20m

clusterrolebinding.rbac.authorization.k8s.io/run-nfs-client-provisioner 20m
role.rbac.authorization.k8s.io/leader-locking-nfs-client-provisioner 20m
rolebinding.rbac.authorization.k8s.io/leader-locking-nfs-client-provisioner 20m

Deploying Storage Class

Next let’s run the “class.yaml” to set up the storageclass. A storageclass provides a way for administrators to describe the “classes” of storage they offer.

Let’s edit the “class.yaml” file and set both the storageclass name and the provisioner name.

apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: managed-nfs-storage <--------------------注意,你可能需要修改此处
provisioner: example.com/nfs <--------------------注意,你可能需要修改此处
parameters:
archiveOnDelete: “false”

Once we’ve updated the class.yaml file we can execute the file using kubectl create

  • [vagrant@kmaster nfs-provisioning]$ kubectl create -f class.yaml
  • storageclass.storage.k8s.io/managed-nfs-storage created

Next, check that the storage class was created.

  • [vagrant@kmaster nfs-provisioning]$ kubectl get storageclass
  • NAME PROVISIONER AGE
  • managed-nfs-storage example.com/nfs 48s

Step 4) Deploying NFS Provisioner

Now let’s deploy the nfs provisioner. But first we’ll need to edit the deployment.yaml file. In this file we’ll need to specify the IP Address of our NFS Server (kmaster) 172.42.42.100.

  • kind: Deployment
  • apiVersion: apps/v1
  • metadata:
  • name: nfs-client-provisioner
  • spec:
  • selector:
  • matchLabels:
  • app: nfs-client-provisioner
  • replicas: 1
  • strategy:
  • type: Recreate
  • template:
  • metadata:
  • labels:
  • app: nfs-client-provisioner
  • spec:
  • serviceAccountName: nfs-client-provisioner
  • containers:
  • - name: nfs-client-provisioner
  • image: quay.io/external_storage/nfs-client-provisioner:latest
  • volumeMounts:
  • - name: nfs-client-root
  • mountPath: /persistentvolumes
  • env:
  • - name: PROVISIONER_NAME
  • value: example.com/nfs <--------------------注意,你可能需要修改此处,与上文的类别相同
  • - name: NFS_SERVER
  • value: 172.42.42.100 <--------------------注意,你可能需要修改此处
  • - name: NFS_PATH
  • value: /srv/nfs/kubedata <--------------------注意,你可能需要修改此处
  • volumes:
  • - name: nfs-client-root
  • nfs:
  • server: 172.42.42.100 <--------------------注意,你可能需要修改此处
  • path: /srv/nfs/kubedata <--------------------注意,你可能需要修改此处

Once we’ve made the changes, save the file and apply the changes by running “kubectl create”.

  • [vagrant@kmaster nfs-provisioning]$ kubectl create -f deployment.yaml
  • deployment.apps/nfs-client-provisioner created

After applying the changes, we should see a pod was created for nfs-client-provisioner.

  • [vagrant@kmaster nfs-provisioning]$ kubectl get all
  • NAME READY STATUS RESTARTS AGE
  • pod/nfs-client-provisioner-5b4f5775c7-9j2dw 1/1 Running 0 4m2s
  • NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
  • service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 2d22h
  • NAME READY UP-TO-DATE AVAILABLE AGE
  • deployment.apps/nfs-client-provisioner 1/1 1 1 4m2s
  • NAME DESIRED CURRENT READY AGE
  • replicaset.apps/nfs-client-provisioner-5b4f5775c7 1 1 1 4m2s

We can run “kubectl describe” to see more details about the pod.

  • [vagrant@kmaster ~]$ kubectl describe pod nfs-client-provisioner-5b4f5775c7-9j2dw
  • Name: nfs-client-provisioner-5b4f5775c7-9j2dw
  • Namespace: default
  • Priority: 0
  • Node: kworker2.example.com/172.42.42.102
  • Start Time: Sun, 03 Nov 2019 20:11:51 +0000
  • Labels: app=nfs-client-provisioner
  • pod-template-hash=5b4f5775c7
  • Annotations: cni.projectcalico.org/podIP: 192.168.136.65/32
  • Status: Running
  • IP: 192.168.136.65
  • IPs:
  • IP: 192.168.136.65
  • Controlled By: ReplicaSet/nfs-client-provisioner-5b4f5775c7
  • Containers:
  • nfs-client-provisioner:
  • Container ID: docker://95432ef4c256b48746b61f44a0292557b73abaced78342acafeae3c36681343b
  • Image: quay.io/external_storage/nfs-client-provisioner:latest
  • Image ID: docker-pullable://quay.io/external_storage/nfs-client-provisioner@sha256:022ea0b0d69834b652a4c53655d78642ae23f0324309097be874fb58d09d2919
  • Port: <none>
  • Host Port: <none>
  • State: Running
  • Started: Sun, 03 Nov 2019 20:11:56 +0000
  • Ready: True
  • Restart Count: 0
  • Environment:
  • PROVISIONER_NAME: example.com/nfs
  • NFS_SERVER: 172.42.42.100
  • NFS_PATH: /srv/nfs/kubedata
  • Mounts:
  • /persistentvolumes from nfs-client-root (rw)
  • /var/run/secrets/kubernetes.io/serviceaccount from nfs-client-provisioner-token-wgwct (ro)
  • Conditions:
  • Type Status
  • Initialized True
  • Ready True
  • ContainersReady True
  • PodScheduled True
  • Volumes:
  • nfs-client-root:
  • Type: NFS (an NFS mount that lasts the lifetime of a pod)
  • Server: 172.42.42.100
  • Path: /srv/nfs/kubedata
  • ReadOnly: false
  • nfs-client-provisioner-token-wgwct:
  • Type: Secret (a volume populated by a Secret)
  • SecretName: nfs-client-provisioner-token-wgwct
  • Optional: false
  • QoS Class: BestEffort
  • Node-Selectors: <none>
  • Tolerations: node.kubernetes.io/not-ready:NoExecute for 300s
  • node.kubernetes.io/unreachable:NoExecute for 300s
  • Events: <none>

发表评论

邮箱地址不会被公开。 必填项已用*标注