
建議將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
[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已成功創建并且可用,
配置持久卷宣告
要在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
[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
注意:要獲取有關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檔案的內容,
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/182309.html
標籤:其他
上一篇:基于Linux權限提升的資訊收集
下一篇:通過FRP實作內網滲透
