一、簡介
要使用 StorageClass,就得安裝對應的自動配置程序,比如這里存儲后端使用的是 nfs,那么就需要使用到一個 nfs-client 的自動配置程序,也叫它 Provisioner,這個程序使用我們已經配置好的 nfs 服務器,來自動創建持久卷,也就是自動創建 PV。
- 自動創建的 PV 以
${namespace}-${pvcName}-${pvName}這樣的命名格式創建在 NFS 服務器上的共享數據目錄中
二、測試storageclass效果
1、rbac.yaml
[root@k8s-master storage]# cat rbac.yaml
apiVersion: v1
kind: ServiceAccount
metadata:
name: nfs-client-provisioner
# replace with namespace where provisioner is deployed
namespace: default #根據實際環境設定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
# replace with namespace where provisioner is deployed
namespace: default
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
# replace with namespace where provisioner is deployed
namespace: default
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
subjects:
- kind: ServiceAccount
name: nfs-client-provisioner
# replace with namespace where provisioner is deployed
namespace: default
roleRef:
kind: Role
name: leader-locking-nfs-client-provisioner
apiGroup: rbac.authorization.k8s.io
2、創建provisioner.yaml
[root@k8s-master storage]# cat prvisor-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nfs-client-provisioner
labels:
app: nfs-client-provisioner
# replace with namespace where provisioner is deployed
namespace: default #與RBAC文件中的namespace保持一致
spec:
replicas: 1
selector:
matchLabels:
app: nfs-client-provisioner
strategy:
type: Recreate
selector:
matchLabels:
app: nfs-client-provisioner
template:
metadata:
labels:
app: nfs-client-provisioner
spec:
serviceAccountName: nfs-client-provisioner
containers:
- name: nfs-client-provisioner
image: quay.io/external_storage/nfs-client-provisioner:latest
volumeMounts:
- name: nfs-client-root
mountPath: /persistentvolumes
env:
- name: PROVISIONER_NAME
value: wuchang-nfs-storage #provisioner名稱,請確保該名稱與 nfs-StorageClass.yaml文件中的provisioner名稱保持一致
- name: NFS_SERVER
value: 192.168.48.250 #NFS Server IP地址
- name: NFS_PATH
value: /nfsdata #NFS掛載卷
volumes:
- name: nfs-client-root
nfs:
server: 192.168.48.250 #NFS Server IP地址
path: /nfsdata #NFS 掛載卷
3、准備工作好了之后創建storageclass.yaml
[root@k8s-master storage]# cat storageclass.yaml apiVersion: storage.k8s.io/v1 kind: StorageClass metadata: name: managed-nfs-storage provisioner: wuchang-nfs-storage #這里的名稱要和provisioner配置文件中的環境變量PROVISIONER_NAME保持一致 parameters: archiveOnDelete: "false"
4、創建測試的pvc,pvc-test
[root@k8s-master storage]# cat test-pvc.yaml
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: test-claim
annotations:
volume.beta.kubernetes.io/storage-class: "managed-nfs-storage" #與nfs-StorageClass.yaml metadata.name保持一致
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 1Mi
5、創建測試的pod,test-pd
[root@k8s-master storage]# cat pod-demo.yaml
apiVersion: v1
kind: Pod
metadata:
name: test-pd
spec:
containers:
- image: ikubernetes/myapp:v1
name: test-container
volumeMounts:
- mountPath: /test-pd
name: nfs-pvc
volumes:
- name: nfs-pvc
persistentVolumeClaim:
claimName: test-claim #與PVC名稱保持一致
6、進入test-pd進行訪問,創建文件wuchang
[root@k8s-master storage]# kubectl exec -it test-pd /bin/sh kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead. / # ls bin dev etc home lib media mnt proc root run sbin srv sys test-pd tmp usr var / # cd test-pd/ /test-pd # ls SUCCESS hello /test-pd # touch wuchang /test-pd # ls SUCCESS hello wuchang /test-pd #
7、在nfs服務器上查看文件
二、在statefulset上測試文件storageclass
1、創建stateful-nginx.yaml
[root@k8s-master storage]# cat stateful-nginx.yaml
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: nfs-web
spec:
selector:
matchLabels:
app: nfs-web
serviceName: "nginx"
replicas: 2
template:
metadata:
labels:
app: nfs-web
spec:
terminationGracePeriodSeconds: 10
containers:
- name: nginx
image: nginx:1.7.9
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
name: web
volumeMounts:
- name: www
mountPath: /usr/share/nginx/html
volumeClaimTemplates:
- metadata:
name: www
annotations:
volume.beta.kubernetes.io/storage-class: managed-nfs-storage //指明storageclass的名稱
spec:
accessModes: [ "ReadWriteOnce" ]
resources:
requests:
storage: 1Gi
2、已經自動創建兩個大小為1G的PV和PVC
3、查看nfs服務器,存在兩個新的文件夾,格式為${namespace}-${pvcName}-${pvName}
