概念
StorageClass 可以动态生成一个存储卷PV 供K8s pvc 使用,它的作用是创建PV模板
创建NFS共享目录
[root@k8smaster5 ~]# mkdir /data/storageClass
[root@k8smaster5 ~]# vim /etc/exports
[root@k8smaster5 ~]# cat /etc/exports
/data/volumes *(rw,no_root_squash)
/data/volume_test/v1 *(rw,no_root_squash)
/data/volume_test/v2 *(rw,no_root_squash)
/data/volume_test/v3 *(rw,no_root_squash)
/data/volume_test/v4 *(rw,no_root_squash)
/data/volume_test/v5 *(rw,no_root_squash)
/data/volume_test/v6 *(rw,no_root_squash)
/data/volume_test/v7 *(rw,no_root_squash)
/data/volume_test/v8 *(rw,no_root_squash)
/data/volume_test/v9 *(rw,no_root_squash)
/data/volume_test/v10 *(rw,no_root_squash)
/data/storageClass *(rw,no_root_squash)
导入NFS新增共享目录
[root@k8smaster5 ~]# exportfs -arv
exporting *:/data/storageClass
exporting *:/data/volume_test/v10
exporting *:/data/volume_test/v9
exporting *:/data/volume_test/v8
exporting *:/data/volume_test/v7
exporting *:/data/volume_test/v6
exporting *:/data/volume_test/v5
exporting *:/data/volume_test/v4
exporting *:/data/volume_test/v3
exporting *:/data/volume_test/v2
exporting *:/data/volume_test/v1
exporting *:/data/volumes
导入镜像
[root@k8snode4 ~]# ctr -n=k8s.io images import nfs-subdir-external-provisioner.tar.gz
创建SA
[root@k8smaster4 storage]# cat serviceaccount.yaml
apiVersion: v1
kind: ServiceAccount
metadata:
name: nfs-provisioner
查看SA
[root@k8smaster4 storage]# kubectl get sa
绑定SA
[root@k8smaster4 storage]# kubectl create clusterrolebinding nfs-provisioner-clusterrolebinding --clusterrole=cluster-admin --serviceaccount=default:nfs-provisioner
clusterrolebinding.rbac.authorization.k8s.io/nfs-provisioner-clusterrolebinding created
创建存储类(storageClass)
[root@k8smaster4 storage]# cat storageclass.yaml
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: nfs
provisioner: example.com/nfs
查看执行结果
查看存储类创建结果
[root@k8smaster4 storage]# kubectl get storageclass
NAME PROVISIONER RECLAIMPOLICY VOLUMEBINDINGMODE ALLOWVOLUMEEXPANSION AGE
nfs example.com/nfs Delete Immediate false 15s
创建PVC,关联存储类
[root@k8smaster4 storage]# cat pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: test-claim
spec:
accessModes: ["ReadWriteMany"]
resources:
requests:
storage: 1Gi
storageClassName: nfs
查看PVC创建结果(生成PV)
[root@k8smaster4 storage]# kubectl get pvc
[root@k8smaster4 storage]# kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
test-claim Bound pvc-b709529d-7015-4eb0-ace8-077ccd94779d 1Gi RWX nfs 6s
创建控制器Pod
[root@k8smaster4 storage]# cat nfs-deployment.yaml
apiVersion: apps/v1
kind: Deployment
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
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
- name: NFS_SERVER
value: 192.168.0.220
- name: NFS_PATH
value: /data/storageClass/
volumes:
- name: nfs-client-root
nfs:
server: 192.168.0.220
path: /data/storageClass/
查看执行结果
[root@k8smaster4 storage]# kubectl get deploy
验证内容同步Pod
[root@k8smaster4 storage]# cat testclass.yaml
apiVersion: v1
kind: Pod
metadata:
name: test-storageclass
spec:
containers:
- name: test-container
image: docker.io/library/nginx
imagePullPolicy: IfNotPresent
volumeMounts:
- name: nfs-pvc
mountPath: /usr/share/nginx/html
volumes:
- name: nfs-pvc
persistentVolumeClaim:
claimName: test-claim