
建議將Pod的資料放入某個持久卷中,以便即使Pod終止后也可以使用這些資料,在Kubernetes(k8s)中,可以在Pod中使用基于NFS的持久卷,在本文中,我們將學習如配置持久卷和持久卷宣告,然后我們將討論如何通過k8s pod中的宣告名稱使用持久卷,
我假設我們有一個功能正常的k8s集群和NFS Server,以下是設定的詳細資訊:
-
NFS服務器IP = 192.168.179.102
-
NFS共享目錄 = /opt/k8s-pods/data
-
K8s群集 = 一個主節點和兩個作業節點
注意: 確保可以從worker節點訪問NFS服務器,并嘗試在每個worker節點上安裝nfs共享以進行測驗,
NFS環境準備
NFS 192.168.179.102 提供共享目錄
#在nfs共享中創建一個index.html檔案,因為我們將在本文后面的nginx pod中掛載該共享,
[root@localhost ~]# yum -y install nfs-utils
[root@localhost ~]# mkdir -p /opt/k8s-pods/data/
[root@localhost ~]# echo "Hello, NFS Storage NGINX" > /opt/k8s-pods/data/index.html
[root@localhost ~]# vim /etc/exports
[root@localhost ~]# cat /etc/exports
/opt/k8s-pods/data 192.168.0.0/16(rw,no_root_squash)
[root@localhost ~]# exportfs -avr
exporting 192.168.0.0/16:/opt/k8s-pods/data
Master Node 安裝驅動設備
[root@k8s-master ~]# mount -t nfs 192.168.179.102:/opt/k8s-pods/data /mnt
mount: wrong fs type, bad option, bad superblock on 192.168.179.102:/opt/k8s-pods/data,
missing codepage or helper program, or other error
(for several filesystems (e.g. nfs, cifs) you might
need a /sbin/mount.<type> helper program)
#可以看到不支持,要安裝驅動存盤設備,所有節點都需要安裝 master node
[root@k8s-master ~]# yum install nfs-utils -y
[root@k8s-master ~]# mount -t nfs 192.168.179.102:/opt/k8s-pods/data /mnt
[root@k8s-master ~]# cd /mnt/
[root@k8s-master mnt]# ls
index.html
[root@k8s-master mnt]# cat index.html
Hello, NFS Storage NGINX
[root@k8s-master ~]# umount /mnt
配置持久卷宣告
要在Pod內掛載持久卷,我們必須指定其持久卷宣告,因此,讓我們使用以下YAML檔案創建持久卷宣告:
[root@k8s-master mnt]# cat nfs-pv.yml
apiVersion: v1
kind: PersistentVolume
metadata:
name: nfs-pv
spec:
capacity:
storage: 2Gi
volumeMode: Filesystem
accessModes:
- ReadWriteMany
persistentVolumeReclaimPolicy: Recycle
storageClassName: nfs
nfs:
path: /opt/k8s-pods/data
server: 192.168.179.102
① capacity 指定 PV 的容量為 2G,
② accessModes 指定訪問模式為 ReadWriteOnce,支持的訪問模式有:
ReadWriteOnce – PV 能以 read-write 模式 mount 到單個節點,
ReadOnlyMany – PV 能以 read-only 模式 mount 到多個節點,
ReadWriteMany – PV 能以 read-write 模式 mount 到多個節點,
③ PersistentVolumes 可以有多種回收策略,包括 “Retain”、”Recycle” 和 “Delete”,對于動態配置的 PersistentVolumes 來說,默認回收策略為 “Delete”,這表示當用戶洗掉對應的 PersistentVolumeClaim 時,動態配置的 volume 將被自動洗掉,如果 volume 包含重要資料時,這種自動行為可能是不合適的,那種情況下,更適合使用 “Retain” 策略,使用 “Retain” 時,如果用戶洗掉 PersistentVolumeClaim,對應的 PersistentVolume 不會被洗掉,相反,它將變為 Released 狀態,表示所有的資料可以被手動恢復,
- Retain – 需要管理員手工回收,
- Recycle – 清除 PV 中的資料,效果相當于執行
rm -rf /thevolume/*, - Delete – 洗掉 Storage Provider 上的對應存盤資源,例如 AWS EBS、GCE PD、Azure Disk、OpenStack Cinder Volume 等,
④ storageClassName 指定 PV 的 class 為 nfs,相當于為 PV 設定了一個分類,PVC 可以指定 class 申請相應 class 的 PV,
⑤ 指定 PV 在 NFS 服務器上對應的目錄,
[root@k8s-master mnt]# kubectl apply -f nfs-pv.yml
[root@k8s-master mnt]# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
nfs-pv 2Gi RWX Recycle Available nfs 2m44s
以上輸出確認PV已成功創建并且可用,STATUS 為 Available,表示 nfs-pv就緒,可以被 PVC 申請,
配置持久卷宣告
接下來創建 PVC,要在Pod內掛載持久卷,我們必須指定其持久卷宣告,因此,讓我們使用以下YAML檔案創建持久卷宣告:
[root@k8s-master mnt]# cat nfs-pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: nfs-pvc
spec:
storageClassName: nfs
accessModes:
- ReadWriteMany
resources:
requests:
storage: 2Gi
PVC 就很簡單了,只需要指定 PV 的容量,訪問模式和 class,
[root@k8s-master mnt]# kubectl apply -f nfs-pvc.yaml
persistentvolumeclaim/nfs-pvc created
#執行完上述操作后,控制平面將使用相同的存盤類名稱查找滿足宣告要求的持久卷,然后將宣告系結到持久卷,示例如下所示:
[root@k8s-master mnt]# kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
nfs-pvc Bound nfs-pv 2Gi RWX nfs 3m37s
[root@k8s-master mnt]# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
nfs-pv 2Gi RWX Recycle Bound default/nfs-pvc nfs 15m
以上輸出確認宣告(nfs-pvc)與持久卷(nfs-pv)系結,現在我們準備在pod中使用基于nfs的持久卷,
在Pod中使用基于NFS的持久卷
在yaml檔案下面創建一個nginx pod,它將在/usr/share/nginx/html上掛載持久卷宣告,
[root@k8s-master mnt]# cat nfs-pv-pod.yml
apiVersion: v1
kind: Pod
metadata:
name: nginx-pv-pod
spec:
volumes:
- name: nginx-pv-storage
persistentVolumeClaim:
claimName: nfs-pvc
containers:
- name: nginx
image: nginx
ports:
- name: http
containerPort: 80
volumeMounts:
- name: nginx-pv-storage
mountPath: /usr/share/nginx/html
與使用普通 Volume 的格式類似,在 volumes 中通過 persistentVolumeClaim 指定使用 nfs-pvc 申請的 Volume,
注意:要獲取有關pod的更多詳細資訊,可使用 kubectl describe pod <pod-name>命令,
以上命令輸出確認已成功創建容器,現在嘗試使用curl命令訪問nginx頁面:
[root@k8s-master mnt]# kubectl get pod -o wide | awk 'NR==1 || $1=="nginx-pv-pod"'
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-pv-pod 1/1 Running 0 5m42s 10.244.0.22 k8s-master <none> <none>
[root@k8s-master mnt]# curl 10.244.0.22
Hello, NFS Storage NGINX
完美,上面curl命令的輸出確認了持久卷已正確掛載到pod中,因為我們可以獲取NFS共享上index.html檔案的內容,如果不再需要使用 PV,可用洗掉 PVC 回收 PV,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/182667.html
標籤:其他
上一篇:基于Linux權限提升的資訊收集
下一篇:通過FRP實作內網滲透
