目錄
一.PV介紹
1.含義
2.關鍵配置參數(shù)
二.PVC介紹
1.含義
2.關鍵參數(shù)配置
三.PV和PVC的生命周期問題
1.PV的生命周期會有4個階段
2.用戶申請空間PV的周期流程
3.PV和PVC的使用/釋放/回收
四.案例演示
1.NFS配置
2.新建PV
3.新建PVC
4.新建Pod測試
5.模擬刪除Pod后后端目錄內數(shù)據(jù)不會受影響
?
一.PV介紹
1.含義
稱為持久化卷,是對底層的共享存儲的一種抽象。一般情況下PV由 kubernetes管理員進行創(chuàng)建和配置,關聯(lián)于底層具體的共享存儲技術,并通過插件完成與共享存儲的對接。
2.關鍵配置參數(shù)
這些參數(shù)在后面的例子中都有用到
(1)存儲類型
這是要與底層存儲對應的東西,像后面用到的nfs等類型
(2)存儲能力
capacity,支持使用storage子項進行配置
(3)訪問模式
accessModes,用戶對于該存儲資源的訪問權限,有如下三種模式(因存儲類型而異):
ReadWriteOnce(RWO):讀寫,被單個節(jié)點掛載
ReadOnlyMany(ROX): 只讀,可被多節(jié)點掛載
ReadWriteMany(RWX):讀寫,可被多節(jié)點掛載
(4)回收策略
persistentVolumeReclaimPolicy,pv不供使用之后的三種處理策略(因存儲類型而異):
Retain 保留數(shù)據(jù),需要管理員手工清理數(shù)據(jù)
Recycle清除數(shù)據(jù)
Delete刪除與 PV 相連的后端存儲完成刪除操作
(5)存儲類別
storageClassName,設定一個類別,pvc需要匹配此類的pv時才能相匹配,pvc不申請具有類的pv時,只能與沒有設定類的pv匹配,不能和設定了類的pv匹配
二.PVC介紹
1.含義
持久卷聲明,是用戶對于存儲需求的一種聲明。用戶向kubernetes系統(tǒng)發(fā)出的一種資源需求申請(可以是訪問模式,存儲空間,存儲類別的需求),kubernetes再進一步去申請PV
2.關鍵參數(shù)配置
(1)訪問模式
accessModes,主要是配置用戶對存儲資源的訪問權限
(2)篩選條件
selector,和以往的selector一樣,對有l(wèi)abel的pv進行選擇
(3)存儲類別
storageClassName,上文已提及
(4)資源請求
resources,通過requests-storage子項進行資源大小申請
三.PV和PVC的生命周期問題
1.PV的生命周期會有4個階段
Available:可用狀態(tài),還未被任何 PVC 綁定
Bound:PV 已經(jīng)被 PVC 綁定
Released:PVC 被刪除,但是資源還未被集群重新聲明
Failed:該 PV 的自動回收失敗
2.用戶申請空間PV的周期流程
管理員創(chuàng)建PV,用戶創(chuàng)建PVC,kubernetes通過PVC請求去找符合條件的PV并將PV與PVC綁定,匹配成功后用戶進行使用,不成功則將PVC標記為Pending狀態(tài),一直持續(xù)到匹配到下一個符合條件的PV
3.PV和PVC的使用/釋放/回收
通過volume掛載來使用PVC,使用完畢后刪除掉PVC即可,但是現(xiàn)在PV卻不能立刻去綁定另外的PVC,因為上面不清楚是否還存在殘留數(shù)據(jù)(取決于你PV配置的回收策略),清除完畢后就可以進行信心PVC的綁定
四.案例演示
如下案例以nfs,my-pv1,my-pv2,my-pvc1,my-pvc2,my-pod1,my-pod2等資源來演示,主要是演示對于nginx訪問日志的簡單存儲問題
1.NFS配置
存儲類型選擇NFS來演示文章來源:http://www.zghlxwxcb.cn/news/detail-840171.html
[root@k8s-master pv]# cat /etc/exports
/root/pv/pv1 192.168.2.0/24(rw,no_root_squash) ? #共享后端目錄,可以自己新建
/root/pv/pv2 192.168.2.0/24(rw,no_root_squash)
[root@k8s-master pv]# systemctl restart nfs
[root@k8s-master pv]# pwd
/root/pv
[root@k8s-master pv]# ll
total 12
-rw-r--r-- 1 root root 1011 Mar 7 20:14 pod.yaml
drwxr-xr-x 5 root root ? 74 Mar 7 20:16 pv1
drwxr-xr-x 5 root root ? 74 Mar 7 20:14 pv2
-rw-r--r-- 1 root root 353 Mar 6 21:54 pvc.yaml
-rw-r--r-- 1 root root 555 Mar 7 15:59 pv.yaml
2.新建PV
[root@k8s-master pv]# cat pv.yaml
apiVersion: v1
kind: Namespace
metadata:
name: myns
?
---
?
apiVersion: v1
kind: PersistentVolume
metadata:
name: my-pv1
namespace: myns
spec:
capacity:
? storage: 1G ? #共1G
accessModes:
- ReadWriteMany ? #可讀寫
persistentVolumeReclaimPolicy: Retain ? #需手動刪除后端內容
nfs: ? #nfs掛載信息
? path: /root/pv/pv1 ? #剛才建立的后端目錄
? server: 192.168.2.150
?
---
?
apiVersion: v1
kind: PersistentVolume
metadata:
name: my-pv2
namespace: myns
spec:
capacity:
? storage: 1G
accessModes:
- ReadWriteMany
persistentVolumeReclaimPolicy: Retain
nfs:
? path: /root/pv/pv2
? server: 192.168.2.150
3.新建PVC
[root@k8s-master pv]# cat pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: my-pvc1
namespace: myns
spec:
accessModes:
- ReadWriteMany
resources:
? requests:
? ? storage: 500M ? #申請500M
?
---
?
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: my-pvc2
namespace: myns
spec:
accessModes:
- ReadWriteMany
resources:
? requests:
? ? storage: 600M
4.新建Pod測試
[root@k8s-master pv]# cat pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: my-pod1
namespace: myns
spec:
containers:
- name: my-nginx1
? image: nginx
? ports:
? - name: nginx-port1
? ? containerPort: 80
? volumeMounts:
? - name: my-volume
? ? mountPath: /var/log/nginx
- name: my-busybox1
? image: busybox
? command: ["/bin/sh","-c","tail -f /logs/access.log"]
? volumeMounts:
? - name: my-volume
? ? mountPath: /logs
volumes:
- name: my-volume
? persistentVolumeClaim:
? ? claimName: my-pvc1
?
---
?
apiVersion: v1
kind: Pod
metadata:
name: my-pod2
namespace: myns
spec:
containers:
- name: my-nginx2
? image: nginx
? ports:
? - name: nginx-port2
? ? containerPort: 80
? volumeMounts:
? - name: my-volume
? ? mountPath: /var/log/nginx
- name: my-busybox2
? image: busybox
? command: ["/bin/sh","-c","tail -f /logs/access.log"]
? volumeMounts:
? - name: my-volume
? ? mountPath: /logs
volumes:
- name: my-volume
? persistentVolumeClaim:
? ? claimName: my-pvc2
?
[root@k8s-master pv]# kubectl get pv,pvc,pod -n myns -o wide
NAME ? ? ? ? ? ? ? ? ? ? CAPACITY ? ACCESS MODES ? RECLAIM POLICY ? STATUS ? CLAIM ? ? ? ? STORAGECLASS ? REASON ? AGE ? VOLUMEMODE
persistentvolume/my-pv1 ? 1G ? ? ? ? RWX ? ? ? ? ? Retain ? ? ? ? ? Bound ? myns/my-pvc1 ? ? ? ? ? ? ? ? ? ? ? ? ? 22m ? Filesystem
persistentvolume/my-pv2 ? 1G ? ? ? ? RWX ? ? ? ? ? Retain ? ? ? ? ? Bound ? myns/my-pvc2 ? ? ? ? ? ? ? ? ? ? ? ? ? 22m ? Filesystem
?
NAME ? ? ? ? ? ? ? ? ? ? ? ? ? STATUS ? VOLUME ? CAPACITY ? ACCESS MODES ? STORAGECLASS ? AGE ? VOLUMEMODE
persistentvolumeclaim/my-pvc1 ? Bound ? my-pv1 ? 1G ? ? ? ? RWX ? ? ? ? ? ? ? ? ? ? ? ? ? 22m ? Filesystem
persistentvolumeclaim/my-pvc2 ? Bound ? my-pv2 ? 1G ? ? ? ? RWX ? ? ? ? ? ? ? ? ? ? ? ? ? 22m ? Filesystem
?
NAME ? ? ? ? READY ? STATUS ? RESTARTS ? AGE ? ? IP ? ? ? ? ? ? ? NODE ? ? ? NOMINATED NODE ? READINESS GATES
pod/my-pod1 ? 2/2 ? ? Running ? 0 ? ? ? ? 7m43s ? 10.244.107.195 ? k8s-node3 ? <none> ? ? ? ? ? <none>
pod/my-pod2 ? 2/2 ? ? Running ? 0 ? ? ? ? 7m43s ? 10.244.36.67 ? ? k8s-node1 ? <none> ? ? ? ? ? <none>
?
[root@k8s-master pv]# ansible all -m shell -a "curl 10.244.107.195" #訪問產(chǎn)生日志
[root@k8s-master pv]# ansible all -m shell -a "curl 10.244.36.67"
?
[root@k8s-master pv]# cat pv1/access.log
10.244.36.64 - - [07/Mar/2024:12:15:05 +0000] "GET / HTTP/1.1" 200 615 "-" "curl/7.29.0" "-"
10.244.169.128 - - [07/Mar/2024:12:15:05 +0000] "GET / HTTP/1.1" 200 615 "-" "curl/7.29.0" "-"
192.168.2.153 - - [07/Mar/2024:12:15:05 +0000] "GET / HTTP/1.1" 200 615 "-" "curl/7.29.0" "-"
[root@k8s-master pv]# cat pv2/access.log
10.244.169.128 - - [07/Mar/2024:12:15:20 +0000] "GET / HTTP/1.1" 200 615 "-" "curl/7.29.0" "-"
192.168.2.151 - - [07/Mar/2024:12:15:20 +0000] "GET / HTTP/1.1" 200 615 "-" "curl/7.29.0" "-"
10.244.107.192 - - [07/Mar/2024:12:15:20 +0000] "GET / HTTP/1.1" 200 615 "-" "curl/7.29.0" "-"
5.模擬刪除Pod后后端目錄內數(shù)據(jù)不會受影響
[root@k8s-master pv]# kubectl get pods -n myns
NAME ? ? READY ? STATUS ? RESTARTS ? AGE
my-pod1 ? 2/2 ? ? Running ? 0 ? ? ? ? 14m
my-pod2 ? 2/2 ? ? Running ? 0 ? ? ? ? 14m
[root@k8s-master pv]# kubectl delete pod my-pod1 my-pod2 -n myns
pod "my-pod1" deleted
pod "my-pod2" deleted
[root@k8s-master pv]# ll
total 12
-rw-r--r-- 1 root root 1011 Mar 7 20:14 pod.yaml
drwxr-xr-x 5 root root ? 74 Mar 7 20:16 pv1
drwxr-xr-x 5 root root ? 74 Mar 7 20:14 pv2
-rw-r--r-- 1 root root 353 Mar 6 21:54 pvc.yaml
-rw-r--r-- 1 root root 555 Mar 7 15:59 pv.yaml
[root@k8s-master pv]# cat pv1/access.log
10.244.36.64 - - [07/Mar/2024:12:15:05 +0000] "GET / HTTP/1.1" 200 615 "-" "curl/7.29.0" "-"
10.244.169.128 - - [07/Mar/2024:12:15:05 +0000] "GET / HTTP/1.1" 200 615 "-" "curl/7.29.0" "-"
192.168.2.153 - - [07/Mar/2024:12:15:05 +0000] "GET / HTTP/1.1" 200 615 "-" "curl/7.29.0" "-"
?文章來源地址http://www.zghlxwxcb.cn/news/detail-840171.html
到了這里,關于k8s中的PV和PVC存儲介紹的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!