Article Directory
1. StorageClass Storage Class Theory
The role of StorageClass mainly has the following aspects:
- Dynamic storage volume allocation: StorageClass can dynamically create storage volumes according to defined attributes, eliminating the need to manually create and manage storage volumes.
- Attribute management of storage volumes: StorageClass can define the attributes of storage volumes, such as storage type, storage capacity, access mode, etc., so as to better meet the storage requirements of applications.
- Management of storage resources: StorageClass can classify and manage storage resources, which is convenient for developers to choose according to application requirements.
Each StorageClass has a provider (Provisioner), which is used to decide which volume plug-in to use to prepare PV. This field must be specified, and the official website provides the following table for suppliers:
volume plugin | built-in preparer | Configuration example |
---|---|---|
AWSElasticBlockStore | ✓ | AWS EBS |
AzureFile | ✓ | Azure File |
AzureDisk | ✓ | Azure Disk |
CephFS | - | - |
Cinder | ✓ | OpenStack Cinder |
FC | - | - |
FlexVolume | - | - |
GCEPersistentDisk | ✓ | GCE PD |
iSCSI | - | - |
NFS | - | NFS |
RBD | ✓ | Ceph RBD |
VsphereVolume | ✓ | vSphere |
PortworxVolume | ✓ | Portworx Volume |
Local | - | Local |
This article takes NFS as an example. To use NFS, we need an nfs-client automatic loading program called provisioner. This program will use the NFS server we have configured to automatically create persistent volumes, that is, automatically create them for us. PV.
2. Case: Storageclass storage class practical demonstration
1. Build NFS server
Note: All Node nodes in the K8S cluster need to install nfs-utils
the package
yum -y install nfs-utils
mkdir /data/nfs_pro -p
vim /etc/exports
/data/nfs_pro *(rw,no_root_squash)
Loading takes effect && start NFS service
exportfs -arv
systemctl enable nfs --now
2. Build an NFS provider (provisioner)
Step 1: Create the SA account needed to run nfs-provisioner
cat nfs-serviceaccount.yaml
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: nfs-provisioner
Execute the YAML file && view the created SA account:
kubectl apply -f nfs-serviceaccount.yaml
kubectl get sa nfs-provisioner
Step 2: Authorize the SA account:
kubectl create clusterrolebinding nfs-provisioner-clusterrolebinding --clusterrole=cluster-admin --serviceaccount=default:nfs-provisioner
Step 3: Install the nfs-provisioner program YAML as follows:
cat nfs-deployment.yaml
---
kind: Deployment
apiVersion: apps/v1
metadata:
name: nfs-provisioner
spec:
selector:
matchLabels:
app: nfs-provisioner
replicas: 1
strategy: # 更新策略
type: Recreate
template:
metadata:
labels:
app: nfs-provisioner
spec:
serviceAccount: nfs-provisioner # 指定SA账号
containers:
- name: nfs-provisioner
image: registry.cn-beijing.aliyuncs.com/mydlq/nfs-subdir-external-provisioner:v4.0.0
imagePullPolicy: IfNotPresent
volumeMounts:
- name: nfs-client-root
mountPath: /persistentvolumes
env:
- name: PROVISIONER_NAME
value: example.com/nfs # NFS供应商名称
- name: NFS_SERVER
value: 16.32.15.200 # NFS服务端地址
- name: NFS_PATH
value: /data/nfs_pro/ # NFS共享目录
volumes:
- name: nfs-client-root
nfs:
server: 16.32.15.200 # NFS服务端地址
path: /data/nfs_pro/ # NFS共享目录
Execute the YAML file && view the Pod status:
kubectl apply -f nfs-deployment.yaml
kubectl get pods
3. Create a StorageClass storage class
cat nfs-storageclass.yaml
---
kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
name: nfs
provisioner: example.com/nfs # 指定NFS供应商名称,和上面对应上
Note: The value written in provisioner example.com/nfs
should be consistent with the value of PROVISIONER_NAME under env when nfs provisioner is installed.
Execute the YAML file && view storageclass status:
kubectl apply -f nfs-storageclass.yaml
kubectl get sc nfs
4. Create PVC and dynamically generate PV through StorageClass
cat nfs-pvc.yaml
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: storageclass-pvc-demo
spec:
accessModes: ["ReadWriteMany"]
resources:
requests:
storage: 1Gi
storageClassName: nfs # 指定使用storageclass的名称,来自动生产PV
Execute the YAML file && check whether the PV is automatically generated
kubectl apply -f nfs-pvc.yaml
kubectl get pvc
As shown in the figure above, the PV has been automatically created and bound to the PVC.
5. Create a Pod to mount PVC
cat nfs-pod-demo.yaml
---
apiVersion: v1
kind: Pod
metadata:
name: nfs-pod-demo
labels:
type: nfs
spec:
volumes:
- persistentVolumeClaim:
claimName: storageclass-pvc-demo # 指定PVC
name: nfs-storage # 卷名称
containers:
- name: nfs-pod-demo
image: nginx
imagePullPolicy: IfNotPresent
volumeMounts:
- name: nfs-storage # 指定上面卷名称
mountPath: /usr/share/nginx/html # 容器挂载目录
Execute the YAML file && view the Pod status:
kubectl apply -f nfs-pod-demo.yaml
kubectl get pods nfs-pod-demo
In the PVC binding host directory, create an index.html file
echo "storageclass demo successd...." > /data/nfs_pro/default-storageclass-pvc-demo-pvc-d4e47c42-d969-44d5-983d-bf36994b6c86/index.html
Obtain Pod IP access website:
kubectl get pods nfs-pod-demo -o wide
3. Summary of steps
1. Build NFS server
2. Build an NFS provider, specify the NFS server IP address and shared directory
3. Create a StorageClass resource and specify the NFS provider
4. Create PVC, use to storageClassName
automatically specify StorageClass
5. Create a Pod and use PVC