Kubernetes(k8s)儲存管理之資料卷volumes(三):NFS資料卷

2022-12-10 18:00:49

一.系統環境

伺服器版本 docker軟體版本 Kubernetes(k8s)叢集版本 CPU架構
CentOS Linux release 7.4.1708 (Core) Docker version 20.10.12 v1.21.9 x86_64

Kubernetes叢集架構:k8scloude1作為master節點,k8scloude2,k8scloude3作為worker節點

伺服器 作業系統版本 CPU架構 程序 功能描述
k8scloude1/192.168.110.130 CentOS Linux release 7.4.1708 (Core) x86_64 docker,kube-apiserver,etcd,kube-scheduler,kube-controller-manager,kubelet,kube-proxy,coredns,calico k8s master節點
k8scloude2/192.168.110.129 CentOS Linux release 7.4.1708 (Core) x86_64 docker,kubelet,kube-proxy,calico k8s worker節點
k8scloude3/192.168.110.128 CentOS Linux release 7.4.1708 (Core) x86_64 docker,kubelet,kube-proxy,calico k8s worker節點

二.前言

Kubernetes(k8s)資料卷volumes型別眾多,本文介紹資料卷volumes之一NFS資料卷

使用資料卷volumes的前提是已經有一套可以正常執行的Kubernetes叢集,關於Kubernetes(k8s)叢集的安裝部署,可以檢視部落格《Centos7 安裝部署Kubernetes(k8s)叢集》https://www.cnblogs.com/renshengdezheli/p/16686769.html

三.NFS資料卷

3.1 NFS資料卷概覽

nfs 卷能將 NFS (網路檔案系統) 掛載到你的 Pod 中。 不像 emptyDir 那樣會在刪除 Pod 的同時也會被刪除,nfs 卷的內容在刪除 Pod 時會被儲存,卷只是被解除安裝。 這意味著 nfs 卷可以被預先填充資料,並且這些資料可以在 Pod 之間共用。
說明:在使用 NFS 卷之前,你必須運行自己的 NFS 服務器並將目標 share 匯出備用。

還需要注意,不能在 Pod spec 中指定 NFS 掛載可選項。 可以選擇設定伺服器端的掛載可選項,或者使用 /etc/nfsmount.conf。 此外,還可以通過允許設定掛載可選項的持久卷掛載 NFS 卷。

3.2 設定NFS伺服器端以及共用目錄

此次共用儲存以nfs為例,在一臺機器上安裝NFS伺服器端,k8s的兩個worker安裝NFS使用者端。

etcd1機器作為NFS的伺服器端,安裝NFS

[root@etcd1 ~]# yum -y install nfs-utils

[root@etcd1 ~]# rpm -qa | grep nfs
libnfsidmap-0.25-19.el7.x86_64
nfs-utils-1.3.0-0.68.el7.2.x86_64

啟動NFS

#使nfs開機自啟動並現在就啟動
[root@etcd1 ~]# systemctl enable nfs-server --now
Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.

#檢視nfs狀態
[root@etcd1 ~]# systemctl status nfs-server 
● nfs-server.service - NFS server and services
   Loaded: loaded (/usr/lib/systemd/system/nfs-server.service; enabled; vendor preset: disabled)
   Active: active (exited) since 二 2022-01-18 17:24:24 CST; 8s ago
  Process: 1469 ExecStartPost=/bin/sh -c if systemctl -q is-active gssproxy; then systemctl reload gssproxy ; fi (code=exited, status=0/SUCCESS)
  Process: 1453 ExecStart=/usr/sbin/rpc.nfsd $RPCNFSDARGS (code=exited, status=0/SUCCESS)
  Process: 1451 ExecStartPre=/usr/sbin/exportfs -r (code=exited, status=0/SUCCESS)
 Main PID: 1453 (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/nfs-server.service

1月 18 17:24:24 etcd1 systemd[1]: Starting NFS server and services...
1月 18 17:24:24 etcd1 systemd[1]: Started NFS server and services.

建立NFS共用目錄,並把目錄/sharedir共用出去

#建立/sharedir作為共用目錄
[root@etcd1 ~]# mkdir /sharedir

[root@etcd1 ~]# vim /etc/exports

#把/sharedir目錄共用出去
[root@etcd1 ~]# cat /etc/exports
/sharedir *(rw,async,no_root_squash)

[root@etcd1 ~]# exportfs -arv
exporting *:/sharedir

3.3 設定NFS使用者端

在k8s叢集的worker節點安裝nfs的使用者端

[root@k8scloude3 ~]# yum -y install nfs-utils

 #安裝nfs的使用者端
[root@k8scloude2 ~]# yum -y install nfs-utils

檢視etcd1(192.168.110.133)機器共用出來的目錄是哪個?

[root@k8scloude2 ~]# showmount -e 192.168.110.133
Export list for 192.168.110.133:
/sharedir *

把192.168.110.133:/sharedir的目錄掛載到/mnt

[root@k8scloude2 ~]# mount 192.168.110.133:/sharedir /mnt

[root@k8scloude2 ~]# df -hT /mnt
檔案系統                  型別  容量  已用  可用 已用% 掛載點
192.168.110.133:/sharedir nfs4  150G  2.5G  148G    2% /mnt

3.4 建立有NFS卷的pod

設定nfs卷,指定共用資料卷的型別為nfs,指定NFS伺服器IP和共用目錄

[root@k8scloude1 volume]# vim share-nfs.yaml 

[root@k8scloude1 volume]# cat share-nfs.yaml 
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: hostpath
  name: nfsshare
spec:
  #nodeName指定該pod執行在k8scloude2節點
  nodeName: k8scloude2
  terminationGracePeriodSeconds: 0
  volumes:
  - name: v1
    #資料卷的型別為nfs
    nfs:
      #nfs伺服器地址
      server: 192.168.110.133
      #共用目錄
      path: /sharedir
      #readOnly: true唯讀
      #readOnly: true
  containers:
  - image: nginx
    imagePullPolicy: IfNotPresent
    name: h1
    resources: {}
    volumeMounts:
    - name: v1
      #把v1卷掛載到/xx目錄
      mountPath: /xx
  dnsPolicy: ClusterFirst
  restartPolicy: Always
status: {}

建立pod

[root@k8scloude1 volume]# kubectl apply -f share-nfs.yaml 
pod/nfsshare created

[root@k8scloude1 volume]# kubectl get pods -o wide
NAME       READY   STATUS    RESTARTS   AGE   IP               NODE         NOMINATED NODE   READINESS GATES
nfsshare   1/1     Running   0          3s    10.244.112.189   k8scloude2   <none>           <none>

進入pod

[root@k8scloude1 volume]# kubectl exec -it nfsshare -- bash 
root@nfsshare:/# ls /xx/

#往共用目錄裡寫入資料
root@nfsshare:/# echo "well well well" >/xx/log.txt
root@nfsshare:/# 
root@nfsshare:/# exit
exit

k8scloude2機器上有對應的檔案

[root@k8scloude2 ~]# cat /mnt/log.txt 
well well well

etcd1機器上也有對應的檔案

[root@etcd1 ~]# cat /sharedir/log.txt 
well well well

刪除pod

[root@k8scloude1 volume]# kubectl delete pod nfsshare --force
warning: Immediate deletion does not wait for confirmation that the running resource has been terminated. The resource may continue to run on the cluster indefinitely.
pod "nfsshare" force deleted

[root@k8scloude1 volume]# kubectl get pods -o wide
No resources found in volume namespace.

設定nfs卷,指定共用資料卷的型別為nfs,指定NFS伺服器IP和共用目錄,不過讓pod執行在k8scloude3上

[root@k8scloude1 volume]# vim share-nfs.yaml 

#nodeName: k8scloude3  指定pod執行在k8scloude3上
[root@k8scloude1 volume]# cat share-nfs.yaml 
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: hostpath
  name: nfsshare
spec:
  nodeName: k8scloude3
  terminationGracePeriodSeconds: 0
  volumes:
  - name: v1
    nfs:
      server: 192.168.110.133
      path: /sharedir
  containers:
  - image: nginx
    imagePullPolicy: IfNotPresent
    name: h1
    resources: {}
    volumeMounts:
    - name: v1
      mountPath: /xx
  dnsPolicy: ClusterFirst
  restartPolicy: Always
status: {}

建立pod

[root@k8scloude1 volume]# kubectl apply -f share-nfs.yaml 
pod/nfsshare created

[root@k8scloude1 volume]# kubectl get pods -o wide
NAME       READY   STATUS    RESTARTS   AGE   IP               NODE         NOMINATED NODE   READINESS GATES
nfsshare   1/1     Running   0          3s    10.244.251.251   k8scloude3   <none>           <none>

因為使用的是NFS共用儲存卷,進入pod,對應的目錄都有檔案

[root@k8scloude1 volume]# kubectl exec -it nfsshare -- bash
root@nfsshare:/# cat /xx/log.txt 
well well well
root@nfsshare:/# 
root@nfsshare:/# exit
exit

當pod排程在k8scloude3上,也出現了相應NFS掛載

[root@k8scloude3 ~]# df -h | grep 192.168.110.133
192.168.110.133:/sharedir  150G  2.5G  148G    2% /var/lib/kubelet/pods/4ebc5f6d-e13c-4bea-a323-3067c4a6e966/volumes/kubernetes.io~nfs/v1

刪除pod

[root@k8scloude1 volume]# kubectl delete pod nfsshare --force
warning: Immediate deletion does not wait for confirmation that the running resource has been terminated. The resource may continue to run on the cluster indefinitely.
pod "nfsshare" force deleted

[root@k8scloude1 volume]# kubectl get pods -o wide
No resources found in volume namespace.

當刪除pod,則這個掛載消失

[root@k8scloude3 ~]# df -h | grep 192.168.110.133

NFS卷存在的問題:每個人都可能連線到儲存伺服器,都必須使用root許可權,對伺服器來說具有安全隱患,還要花時間學習NFS知識。