一、StorageClass概述
StorageClass 为管理员提供了描述存储 "类" 的方法,实现了存储的动态供给,简单来说,StorageClass能够根据pvc来自动创建pv,减轻了集群管理员创建pv的负担。
创建一个StorageClass需要以下几部分
- provisioner:StorageClass能够自动创建pv的前提是我们给StorageClass指定一块可以使用的存储,provisioner即定义了StorageClass使用哪块存储。
- RBAC: 这里定义了StorageClass需要的权限。RBAC相关知识可以参考:https://blog.csdn.net/hszxd479946/article/details/108691533
- StorageClass: 定义StorageClass存储类,并且说明使用哪一个provisioner
二、创建StorageClass
以下的操作都经过实际测试,如果遇到问题可以留言。
step1:创建RBAC
创建RBAC的yaml文件---rbac.yaml:
apiVersion: v1
kind: ServiceAccount
metadata:
name: nfs-client-provisioner
namespace: scm-tools #注意修改namespace
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: nfs-client-provisioner-runner
rules:
- apiGroups: [""]
resources: ["persistentvolumes"]
verbs: ["get", "list", "watch", "create", "delete"]
- apiGroups: [""]
resources: ["persistentvolumeclaims"]
verbs: ["get", "list", "watch", "update"]
- apiGroups: ["storage.k8s.io"]
resources: ["storageclasses"]
verbs: ["get", "list", "watch"]
- apiGroups: [""]
resources: ["events"]
verbs: ["create", "update", "patch"]
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: run-nfs-client-provisioner
subjects:
- kind: ServiceAccount
name: nfs-client-provisioner
namespace: scm-tools #注意修改namespace
roleRef:
kind: ClusterRole
name: nfs-client-provisioner-runner
apiGroup: rbac.authorization.k8s.io
---
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: leader-locking-nfs-client-provisioner
namespace: scm-tools #注意修改namespace
rules:
- apiGroups: [""]
resources: ["endpoints"]
verbs: ["get", "list", "watch", "create", "update", "patch"]
---
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: leader-locking-nfs-client-provisioner
namespace: scm-tools #注意修改namespace
subjects:
- kind: ServiceAccount
name: nfs-client-provisioner
namespace: scm-tools #注意修改namespace
roleRef:
kind: Role
name: leader-locking-nfs-client-provisioner
apiGroup: rbac.authorization.k8s.io
yaml文件就绪之后,执行:
kubectl apply -f rbac.yaml
step2:创建provisioner
创建provisioner的yaml文件---provisioner.yaml:
vim provisioner.yaml
内容如下,重要的地方都做了注释:
apiVersion: apps/v1
kind: Deployment # provisioner的类型是一个deployment
metadata:
name: nfs-client-provisioner
labels:
app: nfs-client-provisioner
namespace: scm-tools # 指定provisioner所属的namespace,改成你自己的namespace
spec:
replicas: 1
strategy:
type: Recreate
selector:
matchLabels:
app: nfs-client-provisioner
template:
metadata:
labels:
app: nfs-client-provisioner
spec:
serviceAccountName: nfs-client-provisioner # 指定provisioner使用的sa
containers:
- name: nfs-client-provisioner
image: vbouchaud/nfs-client-provisioner:latest # 指定provisioner的镜像
volumeMounts:
- name: nfs-client-root
mountPath: /persistentvolumes # 固定写法
env:
- name: PROVISIONER_NAME
value: scm-storage-class # 指定分配器的名称,创建storageclass会用到
- name: NFS_SERVER
value: 172.16.1.100 # 指定使用哪一块存储,这里用的是nfs,此处填写nfs的地址
- name: NFS_PATH
value: /data # 使用nfs哪一块盘符
volumes:
- name: nfs-client-root
nfs:
server: 172.16.1.100 # 和上面指定的nfs地址保持一致
path: /data # 和上面指定的盘符保持一致
provisioner.yaml就绪之后,执行:
kubectl apply -f provisioner.yaml
查看provisioner的状态:
[root@scm-master demo_02]# kubectl get deploy -n scm-tools
NAME READY UP-TO-DATE AVAILABLE AGE
nfs-client-provisioner 1/1 1 1 45h
如果READY一直是0/1,执行以下命令查看报错信息:
kubectl describe deploy nfs-client-provisioner -n ${你的namespace名称}
step3:创建storageclass
准备StorageClass.yaml
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: scm-storage # storageclass的名字
provisioner: scm-storage-class # 必须与provisioner.yaml中PROVISIONER_NAME的值一致
parameters:
archiveOnDelete: "false"
StorageClass.yaml就绪后,执行:
kubectl apply -f StorageClass.yaml
查看已创建的StorageClass:
[root@scm-master demo_02]# kubectl get sc | grep scm-storage
scm-storage scm-storage-class 45h
step4: 创建一个pvc进行测试,检查StorageClass是否自动创建了pv
准备pvc的yaml文件---scm-pvc.yaml:
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: scm-pvc
namespace: scm-tools
spec:
storageClassName: scm-storage # 需要与上面创建的storageclass的名称一致
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi
scm-pvc.yaml文件就绪后,执行:
kubectl apply -f scm-pvc.yaml
查看pvc的情况:
[root@scm-master pv_test]# kubectl get pvc -n scm-tools
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
scm-pvc Bound pvc-13c656a9-a6fb-497c-8156-85c084d4a26a 10Gi RWO scm-storage 9s
pvc的状态显示为Bound,表示我们的StorageClass功能正常,至此StorageClass就算创建完成啦。
如果pvc的状态是Pending或者其他状态,执行以下命令查看异常原因:
kubectl describe pvc scm-pvc -n ${你的namespace名称}