一、简介
1.1什么是StorageClass
Kubernetes提供了一套可以自动创建PV的机制,即:Dynamic Provisioning。而这个机制的核心在于StorageClass这个API对象。
StorageClass对象会定义下面两部分内容:
- PV的属性。比如,存储类型,Volume的大小等。
- 创建这种PV需要用到的存储插件,即存储制备器。
有了这两个信息之后,Kubernetes就能够根据用户提交的PVC,找到一个对应的StorageClass,之后Kubernetes就会调用该StorageClass声明的存储插件,进而创建出需要的PV。
但是其实使用起来是一件很简单的事情,你只需要根据自己的需求,编写YAML文件即可,然后使用kubectl create命令执行即可。
1.2StorageClass架构
在动态资源供应模式下,通过StorageClass和PVC完成资源动态绑定(系统自动生成PV),并供Pod使用的存储管理机制。
二、创建
2.1创建StorageClass存储服务
利用nfs-client-provisioner来生成一个基于nfs的StorageClass,部署配置yaml配置如下,保持为nfs-sc.yaml:
apiVersion: v1
kind: ServiceAccount
metadata:
name: nfs-client-provisioner
namespace: kube-system
---
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: ["list", "watch", "create", "update", "patch"]
- apiGroups: [""]
resources: ["endpoints"]
verbs: ["get", "list", "watch", "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: kube-system
roleRef:
kind: ClusterRole
name: nfs-client-provisioner-runner
apiGroup: rbac.authorization.k8s.io
---
kind: Deployment
apiVersion: apps/v1
metadata:
name: nfs-provisioner-01
namespace: kube-system
spec:
replicas: 1
strategy:
type: Recreate
selector:
matchLabels:
app: nfs-provisioner-01
template:
metadata:
labels:
app: nfs-provisioner-01
spec:
serviceAccountName: nfs-client-provisioner
containers:
- name: nfs-client-provisioner
image: jmgao1983/nfs-client-provisioner:latest
imagePullPolicy: IfNotPresent
volumeMounts:
- name: nfs-client-root
mountPath: /persistentvolumes
env:
- name: PROVISIONER_NAME
value: nfs-provisioner-01 # 此处供应者名字供storageclass调用
- name: NFS_SERVER
value: 10.7.48.223 # 填入NFS的地址
- name: NFS_PATH
value: /data/lnmp/test # 填入NFS挂载的目录
volumes:
- name: nfs-client-root
nfs:
server: 10.7.48.223 # 填入NFS的地址
path: /data/lnmp/test # 填入NFS挂载的目录
---
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: nfs-test
provisioner: nfs-provisioner-01
# Supported policies: Delete、 Retain , default is Delete
reclaimPolicy: Retain
#部署服务
[root@k8smaster StorageClass]# kubectl apply -f nfs-sc.yaml
#因为是偏向系统层面的服务所以放到了kube-system命名空间
[root@k8smaster StorageClass]# kubectl -n kube-system get pod
#查看sc状态
[root@k8smaster StorageClass]# kubectl get sc
NAME PROVISIONER RECLAIMPOLICY VOLUMEBINDINGMODE ALLOWVOLUMEEXPANSION AGE
nfs-test nfs-provisioner-01 Retain Immediate false 2m43s
2.2 基于SC创建PVC测试
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: pvc-sc
spec:
storageClassName: nfs-test
accessModes:
- ReadWriteMany
resources:
requests:
storage: 1Mi
[root@k8smaster StorageClass]# kubectl apply -f pvc-sc.yaml
persistentvolumeclaim/pvc-sc created
#查看pv和pvc
[root@k8smaster StorageClass]# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
pvc-bc40c73b-cf4a-4c2d-a551-c2fdbd7e245a 1Mi RWX Retain Bound default/pvc-sc nfs-test 52s
[root@k8smaster StorageClass]# kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
pvc-sc Bound pvc-bc40c73b-cf4a-4c2d-a551-c2fdbd7e245a 1Mi RWX nfs-test 2m31s
使用nginx测试
---
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: nginx
name: nginx
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- image: nginx
name: nginx
volumeMounts: # 我们这里将nginx容器默认的页面目录挂载
- name: html-files
mountPath: "/usr/share/nginx/html"
volumes:
- name: html-files
persistentVolumeClaim:
claimName: pvc-sc
[root@k8smaster StorageClass]# kubectl apply -f nginx.yaml
deployment.apps/nginx created
#查看pod是否运行
[root@k8smaster StorageClass]# kubectl get po
NAME READY STATUS RESTARTS AGE
nginx-76f5df5fcf-f8x9m 1/1 Running 0 46s
#进入pod测试是否设置成功
[root@k8smaster StorageClass]# kubectl exec -it nginx-76f5df5fcf-f8x9m -- bash
root@nginx-76f5df5fcf-f8x9m:/# echo 'test' > /usr/share/nginx/html/index.html
root@nginx-76f5df5fcf-f8x9m:/# exit
exit
[root@k8smaster StorageClass]# cat /data/lnmp/test/default-pvc-sc-pvc-bc40c73b-cf4a-4c2d-a551-c2fdbd7e245a/index.html
test