之前介绍的PV和PVC模式都是需要先创建好PV,然后定义好PVC和pv进行一对一的Bond,但是如果PVC请求成千上万,那么就需要创建成千上万的PV,对于运维人员来说维护成本很高,
Kubernetes提供一种自动创建PV的机制,叫StorageClass
,它的作用就是创建PV的模板。k8s集群管理员通过创建storageclass可以动态生成一个存储卷pv供k8s pvc使用。
具体来说,StorageClass会定义以下两部分:
=========
1、PV的属性 ,比如存储的大小、类型等;
2、创建这种PV需要使用到的存储插件,比如Ceph、NFS等
=========
有了这两部分信息,Kubernetes就能够根据用户提交的PVC,找到对应的StorageClass,然后Kubernetes就会调用 StorageClass声明的存储插件,创建出需要的PV。
查看定义的storageclass需要的字段:kubectl explain storageclass
每个StorageClass都包含字段provisioner,parameters和reclaimPolicy
[root@master 10]# kubectl explain storageclass
KIND: StorageClass
VERSION: storage.k8s.io/v1
DESCRIPTION:
StorageClass describes the parameters for a class of storage for which
PersistentVolumes can be dynamically provisioned.
StorageClasses are non-namespaced; the name of the storage class according
to etcd is in ObjectMeta.Name.
FIELDS:
allowVolumeExpansion <boolean>
allowedTopologies <[]Object>
apiVersion <string>
kind <string>
metadata <Object>
mountOptions <[]string>
parameters <map[string]string> # 键值对类型
provisioner <string> -required- # 供应商
reclaimPolicy <string> # 回收策略
volumeBindingMode <string>
provisioner:供应商,storageclass需要有一个供应者,用来确定我们使用什么样的存储来创建pv,常见的provisioner:(https://kubernetes.io/zh/docs/concepts/storage/storage-classes/)
provisioner既可以由内部供应商提供,也可以由外部供应商提供,如果是外部供应商可以参考https://github.com/kubernetes-incubator/external-storage/下提供的方法创建。
https://github.com/kubernetes-sigs/sig-storage-lib-external-provisioner
以NFS为例,要想使用NFS,我们需要一个nfs-client的自动装载程序,称之为provisioner,这个程序会使用我们已经配置好的NFS服务器自动创建持久卷,也就是自动帮我们创建PV。
allowVolumeExpansion:允许卷扩展,PersistentVolume 可以配置成可扩展。将此功能设置为true时,允许用户通过编辑相应的 PVC 对象来调整卷大小。当基础存储类的allowVolumeExpansion字段设置为 true 时,以下类型的卷支持卷扩展。
注意:此功能仅用于扩容卷,不能用于缩小卷。
将nfs-subdir-external-provisioner.tar.gz上传到工作节点node01和node02上,然后手动解压。
[root@node01 ~]# ctr -n=k8s.io images import nfs-subdir-external-provisioner.tar.gz
unpacking registry.cn-beijing.aliyuncs.com/mydlq/nfs-subdir-external-provisioner:v4.0.0 (sha256:69b08b256d7e9f5823cf09dece7eabf025cc60c652e4ae08201978bb2862a276)...done
You have new mail in /var/spool/mail/root
[root@node02 ~]# ctr -n=k8s.io images import nfs-subdir-external-provisioner.tar.gz
unpacking registry.cn-beijing.aliyuncs.com/mydlq/nfs-subdir-external-provisioner:v4.0.0 (sha256:69b08b256d7e9f5823cf09dece7eabf025cc60c652e4ae08201978bb2862a276)...done
You have new mail in /var/spool/mail/root
[root@master 11]# cat serviceaccount.yaml
apiVersion: v1
kind: ServiceAccount
metadata:
name: nfs-provisioner
[root@master 11]# kubectl apply -f serviceaccount.yaml
serviceaccount/nfs-provisioner created
[root@master 11]# kubectl get sa
NAME SECRETS AGE
default 0 5d13h
nfs-provisioner 0 21s
扩展:什么是sa?
sa的全称是serviceaccount。
serviceaccount是为了方便Pod里面的进程调用Kubernetes API或其他外部服务而设计的。
指定了serviceaccount之后,我们把pod创建出来了,我们在使用这个pod时,这个pod就有了我们指定的账户的权限了。
[root@master 11]# kubectl create clusterrolebinding nfs-provisioner-clusterrolebinding --clusterrole=cluster-admin --serviceaccount=default:nfs-provisioner
clusterrolebinding.rbac.authorization.k8s.io/nfs-provisioner-clusterrolebinding created
[root@master 11]# cat /etc/exports
/data/volumes 10.32.1.147/24(rw,no_root_squash)
/data/volume_test/v1 10.32.1.147/24(rw,no_root_squash)
/data/volume_test/v2 10.32.1.147/24(rw,no_root_squash)
/data/volume_test/v3 10.32.1.147/24(rw,no_root_squash)
/data/volume_test/v4 10.32.1.147/24(rw,no_root_squash)
/data/volume_test/v5 10.32.1.147/24(rw,no_root_squash)
/data/volume_test/v6 10.32.1.147/24(rw,no_root_squash)
/data/volume_test/v7 10.32.1.147/24(rw,no_root_squash)
/data/volume_test/v8 10.32.1.147/24(rw,no_root_squash)
/data/volume_test/v9 10.32.1.147/24(rw,no_root_squash)
/data/volume_test/v10 10.32.1.147/24(rw,no_root_squash)
/data/nfs_pro 10.32.1.147/24(rw,no_root_squash)
[root@master 11]# exportfs -arv
exporting 10.32.1.147/24:/data/nfs_pro
exporting 10.32.1.147/24:/data/volume_test/v10
exporting 10.32.1.147/24:/data/volume_test/v9
exporting 10.32.1.147/24:/data/volume_test/v8
exporting 10.32.1.147/24:/data/volume_test/v7
exporting 10.32.1.147/24:/data/volume_test/v6
exporting 10.32.1.147/24:/data/volume_test/v5
exporting 10.32.1.147/24:/data/volume_test/v4
exporting 10.32.1.147/24:/data/volume_test/v3
exporting 10.32.1.147/24:/data/volume_test/v2
exporting 10.32.1.147/24:/data/volume_test/v1
exporting 10.32.1.147/24:/data/volumes
[root@master 11]# 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
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: 10.32.1.147
- name: NFS_PATH
value: /data/nfs_pro/
volumes:
- name: nfs-client-root
nfs:
server: 10.32.1.147
path: /data/nfs_pro/
[root@master 11]# kubectl apply -f nfs-deployment.yaml
deployment.apps/nfs-provisioner created
[root@master 11]# kubectl get pods | grep nfs
nfs-provisioner-d5bd78f66-56ccg 1/1 Running 0 6s
[root@master 11]# cat nfs-storageclass.yaml
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: nfs
provisioner: example.com/nfs
[root@master 11]# kubectl apply -f nfs-storageclass.yaml
storageclass.storage.k8s.io/nfs created
[root@master 11]# kubectl get storageclass
NAME PROVISIONER RECLAIMPOLICY VOLUMEBINDINGMODE ALLOWVOLUMEEXPANSION AGE
nfs example.com/nfs Delete Immediate false 22s
# 显示内容如上,说明storageclass创建成功了
注意:provisioner处写的example.com/nfs
应该跟安装nfs provisioner时候的env下的PROVISIONER_NAME的value值保持一致,如下:
env:
- name: PROVISIONER_NAME
value: `example.com/nfs`
[root@master 11]# cat claim.yaml
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: test-claim1
spec:
accessModes: ["ReadWriteMany"]
resources:
requests:
storage: 1Gi
storageClassName: nfs
[root@master 11]# kubectl apply -f claim.yaml
persistentvolumeclaim/test-claim1 created
# 查看是否动态生成了pv,pvc是否创建成功,并和pv绑定
[root@master 11]# kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
test-claim1 Bound pvc-6ecea632-e5ff-4ef9-99df-33638fe90a1c 1Gi RWX nfs 13s
# 通过上面可以看到test-claim1的pvc已经成功创建了,绑定的pv是pvc-6ecea632-e5ff-4ef9-99df-33638fe90a1c
# 这个pv是由storageclass调用nfs provisioner自动生成的。
步骤总结:
[root@master 11]# cat read-pod.yaml
kind: Pod
apiVersion: v1
metadata:
name: read-pod
spec:
containers:
- name: read-pod
image: nginx
imagePullPolicy: IfNotPresent
volumeMounts:
- name: nfs-pvc
mountPath: /usr/share/nginx/html
restartPolicy: "Never"
volumes:
- name: nfs-pvc
persistentVolumeClaim:
claimName: test-claim1
[root@master 11]# kubectl apply -f read-pod.yaml
pod/read-pod created
[root@master 11]# kubectl get pods | grep read
read-pod 1/1 Running 0 16s