如何使用storageclass實(shí)現(xiàn)動(dòng)態(tài)pv?針對(duì)這個(gè)問(wèn)題,今天小編總結(jié)這篇有關(guān)storageclass實(shí)踐的文章,希望幫助更多想解決這個(gè)問(wèn)題的同學(xué)找到更加簡(jiǎn)單易行的辦法。

在部署nfs-client-provisioner前,我們需要先準(zhǔn)備好nfs存儲(chǔ)服務(wù)器并在所有node節(jié)點(diǎn)上安裝
nfs服務(wù)器:192.168.248.139
共享存儲(chǔ)目錄:/data/nfs
nfs-client-provisioner部署文件
vim nfs-client-provisioner.yamlapiVersion: apps/v1 kind: Deployment metadata: name: nfs-client-provisioner namespace: default spec: replicas: 1 selector: matchLabels: app: nfs-client-provisioner strategy: type: Recreate template: metadata: labels: app: nfs-client-provisioner spec: serviceAccountName: nfs-client-provisioner containers: - name: nfs-client-provisioner image: quay.azk8s.cn/external_storage/nfs-client-provisioner:latest volumeMounts: - name: timezone mountPath: /etc/localtime - name: nfs-client-root mountPath: /persistentvolumes env: - name: PROVISIONER_NAME value: fuseim.pri/ifs - name: NFS_SERVER value: 192.168.248.139 - name: NFS_PATH value: /data/nfs volumes: - name: timezone hostPath: path: /usr/share/zoneinfo/Asia/Shanghai - name: nfs-client-root nfs: server: 192.168.248.139 path: /data/nfsStorageclass部署文件
vim nfs-client-class.yamlapiVersion: storage.k8s.io/v1 kind: StorageClass metadata: name: managed-nfs-storage annotations: storageclass.kubernetes.io/is-default-class: "true" #設(shè)置其為默認(rèn)存儲(chǔ)后端 provisioner: fuseim.pri/ifs #or choose another name, must match deployment's env PROVISIONER_NAME' parameters: archiveOnDelete: "false" #刪除pvc后,后端存儲(chǔ)上的pv也自動(dòng)刪除rbac授權(quán)文件
vim nfs-client-rbac.yamlkind: ServiceAccount apiVersion: v1 metadata: name: nfs-client-provisioner --- 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: 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 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 namespace: default roleRef: kind: Role name: leader-locking-nfs-client-provisioner apiGroup: rbac.authorization.k8s.io準(zhǔn)備好以上三個(gè)文件后,使用kubectl apply命令應(yīng)用即可完成nfs-client-provisioner的部署。
[root@k8s-master-01 Dynamic-pv]# kubectl apply -f . storageclass.storage.k8s.io/managed-nfs-storage created deployment.apps/nfs-client-provisioner created serviceaccount/nfs-client-provisioner created clusterrole.rbac.authorization.k8s.io/nfs-client-provisioner-runner created clusterrolebinding.rbac.authorization.k8s.io/run-nfs-client-provisioner created role.rbac.authorization.k8s.io/leader-locking-nfs-client-provisioner created rolebinding.rbac.authorization.k8s.io/leader-locking-nfs-client-provisioner created查看pod運(yùn)行狀態(tài)和sc
[root@k8s-master-01 Dynamic-pv]# kubectl get pod,sc NAME READY STATUS RESTARTS AGE pod/nfs-client-provisioner-c676947d-pfpms 1/1 Running 0 107s NAME PROVISIONER AGE storageclass.storage.k8s.io/managed-nfs-storage (default) fuseim.pri/ifs 108s可以看到nfs-client-provisioner已經(jīng)正常運(yùn)行,sc已經(jīng)創(chuàng)建成功。接下來(lái)我們測(cè)試創(chuàng)建幾個(gè)pvc
vim mysql-pvc.yamlapiVersion: v1 kind: PersistentVolumeClaim metadata: name: mysql-01-pvc # annotations: # volume.beta.kubernetes.io/storage-class: "managed-nfs-storage" spec: accessModes: ["ReadWriteMany"] resources: requests: storage: 10Gi --- apiVersion: v1 kind: PersistentVolumeClaim metadata: name: mysql-02-pvc spec: accessModes: ["ReadWriteMany"] resources: requests: storage: 5Gi --- apiVersion: v1 kind: PersistentVolumeClaim metadata: name: mysql-03-pvc spec: accessModes: ["ReadWriteMany"] resources: requests: storage: 3Gi[root@k8s-master-01 Dynamic-pv]# kubectl apply -f mysql-pvc.yaml persistentvolumeclaim/mysql-01-pvc created persistentvolumeclaim/mysql-02-pvc created persistentvolumeclaim/mysql-03-pvc created [root@k8s-master-01 Dynamic-pv]# kubectl get pvc,pv NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE persistentvolumeclaim/mysql-01-pvc Bound pvc-eef853e1-f8d8-4ab9-bfd3-05c2a58fd9dc 10Gi RWX managed-nfs-storage 2m54s persistentvolumeclaim/mysql-02-pvc Bound pvc-fc0b8228-81c0-4d91-83b0-6bb20ab37cc3 5Gi RWX managed-nfs-storage 2m54s persistentvolumeclaim/mysql-03-pvc Bound pvc-c6739d7d-4930-49bd-975f-04bffc05dfd6 3Gi RWX managed-nfs-storage 2m54s NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE persistentvolume/pvc-c6739d7d-4930-49bd-975f-04bffc05dfd6 3Gi RWX Delete Bound default/mysql-03-pvc managed-nfs-storage 2m54s persistentvolume/pvc-eef853e1-f8d8-4ab9-bfd3-05c2a58fd9dc 10Gi RWX Delete Bound default/mysql-01-pvc managed-nfs-storage 2m54s persistentvolume/pvc-fc0b8228-81c0-4d91-83b0-6bb20ab37cc3 5Gi RWX Delete Bound default/mysql-02-pvc managed-nfs-storage 2m54s可以看到pvc已經(jīng)創(chuàng)建成功,并自動(dòng)創(chuàng)建了一個(gè)關(guān)聯(lián)的pv資源對(duì)象。我們?cè)俨榭春蠖舜鎯?chǔ)目錄里面是否生成了對(duì)應(yīng)命名格式的pv
[root@localhost nfs]# pwd /data/nfs [root@localhost nfs]# ll total 12 drwxrwxrwx 2 root root 4096 Feb 20 10:05 default-mysql-01-pvc-pvc-eef853e1-f8d8-4ab9-bfd3-05c2a58fd9dc drwxrwxrwx 2 root root 4096 Feb 20 10:05 default-mysql-02-pvc-pvc-fc0b8228-81c0-4d91-83b0-6bb20ab37cc3 drwxrwxrwx 2 root root 4096 Feb 20 10:05 default-mysql-03-pvc-pvc-c6739d7d-4930-49bd-975f-04bffc05dfd6可以看到下面有名字很長(zhǎng)的文件夾,這個(gè)文件夾的命名方式是不是和我們上面的規(guī)則:${namespace}-${pvcName}-${pvName}是一樣的,結(jié)果符合我們的預(yù)期。
接下來(lái)我們部署一個(gè)mysql應(yīng)用,測(cè)試下 StorageClass 方式聲明的 PVC 對(duì)象
cat mysql-config.yaml apiVersion: v1 kind: ConfigMap metadata: name: mysql-config data: custom.cnf: | [mysqld] default_storage_engine=innodb skip_external_locking skip_host_cache skip_name_resolve default_authentication_plugin=mysql_native_passwordcat mysql-secret.yaml apiVersion: v1 kind: Secret metadata: name: mysql-user-pwd data: mysql-root-pwd: cGFzc3dvcmQ=cat mysql-deploy.yaml apiVersion: v1 kind: Service metadata: name: mysql spec: type: NodePort ports: - port: 3306 nodePort: 30006 protocol: TCP targetPort: 3306 selector: app: mysql --- apiVersion: apps/v1 kind: Deployment metadata: name: mysql spec: replicas: 1 selector: matchLabels: app: mysql strategy: type: Recreate template: metadata: labels: app: mysql spec: containers: - image: mysql name: mysql imagePullPolicy: IfNotPresent env: - name: MYSQL_ROOT_PASSWORD valueFrom: secretKeyRef: name: mysql-user-pwd key: mysql-root-pwd ports: - containerPort: 3306 name: mysql volumeMounts: - name: mysql-config mountPath: /etc/mysql/conf.d/ - name: mysql-persistent-storage mountPath: /var/lib/mysql - name: timezone mountPath: /etc/localtime volumes: - name: mysql-config configMap: name: mysql-config - name: timezone hostPath: path: /usr/share/zoneinfo/Asia/Shanghai - name: mysql-persistent-storage persistentVolumeClaim: claimName: mysql-01-pvc[root@k8s-master-01 yaml]# kubectl apply -f . configmap/mysql-config created service/mysql created deployment.apps/mysql created secret/mysql-user-pwd created [root@k8s-master-01 yaml]# kubectl get pod,svc NAME READY STATUS RESTARTS AGE pod/mysql-7c5b5df54c-vrnr8 1/1 Running 0 83s pod/nfs-client-provisioner-c676947d-pfpms 1/1 Running 0 30m NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE service/kubernetes ClusterIP 10.0.0.1 <none> 443/TCP 93d service/mysql NodePort 10.0.0.19 <none> 3306:30006/TCP 83s可以看到mysql應(yīng)用已經(jīng)正常運(yùn)行,我們通過(guò)任意一個(gè)node節(jié)點(diǎn)的ip和30006端口連接mysql數(shù)據(jù)庫(kù)測(cè)試
[root@localhost ~]# mysql -uroot -h292.168.248.134 -P30006 -p Enter password: Welcome to the MariaDB monitor. Commands end with ; or \g. Your MySQL connection id is 10 Server version: 8.0.19 MySQL Community Server - GPL Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MySQL [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.01 sec) MySQL [(none)]>可以看到mysql數(shù)據(jù)庫(kù)連接正常。此時(shí)查看nfs存儲(chǔ),mysql數(shù)據(jù)庫(kù)數(shù)據(jù)已經(jīng)持久化到nfs服務(wù)器/data/nfs/default-mysql-01-pvc-pvc-eef853e1-f8d8-4ab9-bfd3-05c2a58fd9dc目錄中
[root@localhost nfs]# du -sh * 177M default-mysql-01-pvc-pvc-eef853e1-f8d8-4ab9-bfd3-05c2a58fd9dc 4.0K default-mysql-02-pvc-pvc-fc0b8228-81c0-4d91-83b0-6bb20ab37cc3 4.0K default-mysql-03-pvc-pvc-c6739d7d-4930-49bd-975f-04bffc05dfd6 [root@localhost nfs]# cd default-mysql-01-pvc-pvc-eef853e1-f8d8-4ab9-bfd3-05c2a58fd9dc/ [root@localhost default-mysql-01-pvc-pvc-eef853e1-f8d8-4ab9-bfd3-05c2a58fd9dc]# ls auto.cnf binlog.index client-cert.pem ibdata1 ibtmp1 mysql.ibd public_key.pem sys binlog.000001 ca-key.pem client-key.pem ib_logfile0 #innodb_temp performance_schema server-cert.pem undo_001 binlog.000002 ca.pem ib_buffer_pool ib_logfile1 mysql private_key.pem server-key.pem undo_002另外我們可以看到我們這里是手動(dòng)創(chuàng)建的一個(gè) PVC 對(duì)象,在實(shí)際工作中,使用 StorageClass 更多的是 StatefulSet 類(lèi)型的服務(wù),StatefulSet 類(lèi)型的服務(wù)我們也可以通過(guò)一個(gè) volumeClaimTemplates 屬性來(lái)直接使用 StorageClass,如下
vim web.yamlapiVersion: v1 kind: Service metadata: name: nginx labels: app: nginx spec: ports: - port: 80 name: web clusterIP: None selector: app: nginx --- apiVersion: apps/v1 kind: StatefulSet metadata: name: web spec: serviceName: "nginx" replicas: 8 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx imagePullPolicy: IfNotPresent ports: - containerPort: 80 name: web volumeMounts: - name: www mountPath: /usr/share/nginx/html volumeClaimTemplates: - metadata: name: www spec: accessModes: [ "ReadWriteOnce" ] resources: requests: storage: 10Gi直接創(chuàng)建上面的對(duì)象
[root@k8s-master-01 Dynamic-pv]# kubectl apply -f web.yaml service/nginx created statefulset.apps/web created [root@k8s-master-01 Dynamic-pv]# kubectl get pod -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES nfs-client-provisioner-c676947d-wzwhh 1/1 Running 0 41m 10.244.0.176 k8s-node-01 <none> <none> web-0 1/1 Running 0 32s 10.244.1.167 k8s-node-02 <none> <none> web-1 1/1 Running 0 31s 10.244.0.188 k8s-node-01 <none> <none> web-2 1/1 Running 0 29s 10.244.1.168 k8s-node-02 <none> <none> web-3 1/1 Running 0 27s 10.244.0.189 k8s-node-01 <none> <none> web-4 1/1 Running 0 24s 10.244.1.169 k8s-node-02 <none> <none> web-5 1/1 Running 0 22s 10.244.0.190 k8s-node-01 <none> <none> web-6 1/1 Running 0 21s 10.244.1.170 k8s-node-02 <none> <none> web-7 1/1 Running 0 19s 10.244.0.191 k8s-node-01 <none> <none>
查看存儲(chǔ)上的數(shù)據(jù)目錄

可以看出可以自動(dòng)動(dòng)態(tài)的分配nfs存儲(chǔ)卷。以上即為k8s持久化存儲(chǔ)之storageclass實(shí)踐。
以上就是使用storageclass實(shí)現(xiàn)動(dòng)態(tài)pv的具體步驟,內(nèi)容較為全面,而且我也相信有相當(dāng)?shù)囊恍┕ぞ呖赡苁俏覀內(nèi)粘9ぷ骺赡軙?huì)見(jiàn)到或用到的。通過(guò)這篇文章,希望你能收獲更多。
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性?xún)r(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專(zhuān)為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
網(wǎng)頁(yè)標(biāo)題:如何使用storageclass實(shí)現(xiàn)動(dòng)態(tài)pv?-創(chuàng)新互聯(lián)
文章路徑:http://chinadenli.net/article0/hhsoo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站建設(shè)、服務(wù)器托管、外貿(mào)建站、ChatGPT、小程序開(kāi)發(fā)、網(wǎng)站設(shè)計(jì)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容