K8S's StorageClass combat (NFS)

About StorageClass

In the K8S environment, when the pod needs storage space, StorageClass is more flexible and convenient than PV. The official document address is: https://kubernetes.io/docs/concepts/storage/persistent-volumes/
Insert picture description here

This actual combat

The goal of this actual combat is to quickly create an NFS type StorageClass and verify that the StorageClass is normally available. The full text consists of the following parts:

  1. Create StorageClass;
  2. Download the chart of tomcat through helm;
  3. Modify the chart so that tomcat uses the StorageClass just created;
  4. Check that the folder has been written normally on the NFS server;

Environmental information and preparation

The following is the necessary environmental information for creating StorageClass:

  1. Governors : 1.15
  2. Kubernetes host: CentOS Linux release 7.7.1908
  3. NFS service: IP address 192.168.50.135 , folder / volume1 / nfs-storageclass-test

Reference article

If you want to learn more about building Kubernetes and NFS, please refer to:

  1. "Kubespray2.11 install kubernetes1.15"
  2. "Install and use NFS in Ubuntu16 environment"
  3. "K8S uses NFS of Synology DS218 +"

If you have prepared kubernetes and NFS, let's start the actual combat;

How to create StorageClass

Make clear what you need to do to create StorageClass:

  1. Create a namespace, use hello-storageclass here (you can also choose your favorite);
  2. Create rbac: Because StorageClass has corresponding pods to run, each pod has its own identity, serviceaccount, and this serviceaccount is bound to a certain role, so we must create: serviceaccount, rule, rolebinding;
  3. Create a provisioner, that is, a work class associated with NFS, which is responsible for providing storage resources to PVC. Here, nfs-client-provisioner is used;
  4. Create StorageClass, all need PVC to get storage space through this StorageClass;

Next, please SSH into the kubernetes environment and follow the above steps;

Create StorageClass

  1. 创建namespace:kubectl create namespace hello-storageclass
  2. Download the script to create rbac directly from my github, address: https://raw.githubusercontent.com/zq2599/blog_demos/master/storageclass-demo/rbac.yaml
  3. In the downloaded rbac.yaml file, the namespace is kafka-test , and now it needs to be replaced with hello-storages , execute the command replacement: sed -i 's / kafka-test / hello-storageclass /' rbac.yaml
  4. Create rbac: kubectl apply -f rbac.yaml
  5. The script to create a provisioner is also downloaded from my github at https://raw.githubusercontent.com/zq2599/blog_demos/master/storageclass-demo/deployment.yaml
  6. In the downloaded deployment.yaml file, the namespace is kafka-test , and now it should be replaced with hello-storages , execute the command replacement: sed -i 's / kafka-test / hello-storageclass /' deployment.yaml
  7. Open deployment.yaml, set the NFS parameters, modify the four parameters in the red box below, red boxes 1 and 3 are NFS server addresses, red boxes 2 and 4 are NFS-allocated folder directories, please follow the actual NFS Resources to set:
    Insert picture description here
  8. Create a provisioner: kubectl apply -f deployment.yaml
  9. It is strongly recommended to use the kubectl describe pod xxxxxx -n hello-storageclass and kubectl logs -f xxxxxx -n hello-storageclass commands to check whether the provisioner has been successfully created. The following picture is a problem I have encountered. use:
    Insert picture description here
  10. The script to create StorageClass is also downloaded from my github at https://raw.githubusercontent.com/zq2599/blog_demos/master/storageclass-demo/class.yaml
  11. The downloaded class.yaml can be used directly without modification: kubectl apply -f class.yaml
  12. The StorageClass in class.yaml is called managed-nfs-storage , and the latter PVC uses this name to apply for storage space;
  13. Use the df command to check the mounting situation and find that the NFS has been mounted to the K8S host:
    Insert picture description here
  14. At this point, the StorageClass is ready, and the PVC in the K8S environment can be applied for use. Next, verify whether the application pod can use the storage space of the StorageClass through actual combat;

Ready to work

  1. The next actual combat is to deploy tomcat in kubernetes through helm. The storage space required by the tomcat is allocated through StorageClass. Please prepare helm on kubernetes. I use version 2.16
  2. For the installation and use of helm, please refer to "Deployment and Experience Helm (Version 2.16.1)"

tomcat use StorageClass combat

  1. Add helm warehouse (warehouse with tomcat): helm repo add bitnami https://charts.bitnami.com/bitnami
  2. Download the chart of tomcat: helm fetch bitnami / tomcat
  3. After the chart is successfully downloaded, the tomcat configuration compression package tomcat-6.2.4.tgz appears in the current directory . Unzip: tar -zxvf tomcat-6.2.4.tgz
  4. Unzip to get the tomcat folder, open the values.yaml file after entering , find the persistence node, and add the content in the red box below:
    Insert picture description here
  5. Execute the command in the tomcat directory:
helm install --name-template tomcat001 -f values.yaml . --namespace hello-storageclass
  1. Check tomcat's pod and service, everything is normal, and the port is mapped to the host's 30300:
    Insert picture description here
  2. Browser visits host IP: 30300 , tomcat welcome page appears:
    Insert picture description here
  3. Go to the NFS server to check the disk usage. As shown in the figure below, you can see the PVC that has been assigned to tomcat and the basic data of tomcat is written:
    Insert picture description here

Clean up resources

This actual combat created various types of resources, you can use the following command to clear them all:

helm del --purge tomcat001
kubectl delete storageclass managed-nfs-storage
kubectl delete deployment nfs-client-provisioner -n hello-storageclass
kubectl delete clusterrolebinding run-nfs-client-provisioner
kubectl delete serviceaccount nfs-client-provisioner -n hello-storageclass
kubectl delete role leader-locking-nfs-client-provisioner -n hello-storageclass
kubectl delete rolebinding leader-locking-nfs-client-provisioner -n hello-storageclass
kubectl delete clusterrole nfs-client-provisioner-runner
kubectl delete namespace hello-storageclass

At this point, the actual combat of the creation and use of StorageClass is completed. If you are learning about kubernetes storage, I hope this article can give you some reference

Welcome to pay attention to my public number: programmer Xinchen

Insert picture description here

Published 376 original articles · praised 986 · 1.28 million views

Guess you like

Origin blog.csdn.net/boling_cavalry/article/details/105465672