K8S後端儲存:NFS
大家都知道,NFS是一種基於網路的檔案系統協議,允許在不同的機器之間共享檔案系統資源。在K8S中,可以使用NFS作為後端儲存,以提供持久化儲存和共享儲存卷。但是否適合在生產環境使用NFS作為後端儲存,這取決於具體的應用程式和使用場景。如果應用程式對效能和可靠性要求比較高,可能需要選擇其他更適合的儲存方案,比如ceph。如果只是在測試或者開發環境中,我覺得使用NFS可以更方便地實現共享儲存卷,提高測試或者開發的效率。
搭建NFS
# step1 安裝yum install nfs-utils -y
yum install nfs-utils -y
# step2 建立NFS共享目錄
mkdir /data/nfs_k8s_storage_share
# step3 配置NFS共享,編輯/etc/exports檔案,將要共享的目錄新增到檔案中
/data/nfs_k8s_storage_share *(rw,sync,no_root_squash) # 這將允許任何客戶端以讀寫模式訪問共享目錄。
# step4 重新載入NFS配置
exportfs -r
# step5 啟動NFS服務
systemctl start nfs-server
systemctl enable nfs-server
# step6 確認NFS服務正在執行
systemctl status nfs-server
# step7 注意防火牆,可以關閉或新增允許策略
firewall-cmd --permanent --zone=public --add-service=nfs
firewall-cmd --reload
# step8 驗證是否共享成功
showmount -e 192.168.11.254
k8s工作節點安裝nfs-utils
安裝完即可,無需做任何配置
yum install nfs-utils -y
“
具體在哪個工作節點安裝得看在下面的deployment.yaml中拉起的NFS客戶端配置程式pod跑在哪個節點上,可以讓該pod排程到指定的節點,那麼該節點就要安裝好nfs-utils。如果不干涉排程行為,或者考慮到後續要跑多個副本就可以將全部工作節點都安裝好nfs-utils。
”
在k8s master節點上安裝nfs動態供給外掛
倉庫: https://github.com/kubernetes-sigs/nfs-subdir-external-provisioner
相關yaml下載連結: https://github.com/kubernetes-sigs/nfs-subdir-external-provisioner/blob/master/deploy/rbac.yaml https://github.com/kubernetes-sigs/nfs-subdir-external-provisioner/blob/master/deploy/deployment.yaml https://github.com/kubernetes-sigs/nfs-subdir-external-provisioner/blob/master/deploy/class.yaml
- rbac.yaml:授權訪問ApiServer
- deployment.yaml:部署外掛,部署之前要修改裡面的指向的nfs伺服器地址和共享目錄
- class.yaml:建立儲存類
開始部署外掛
- 部署rbac.yaml
apiVersion: v1
kind: ServiceAccount
metadata:
name: nfs-client-provisioner
# replace with namespace where provisioner is deployed
namespace: default
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: nfs-client-provisioner-runner
rules:
- apiGroups: [""]
resources: ["nodes"]
verbs: ["get", "list", "watch"]
- 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
# replace with namespace where provisioner is deployed
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
# replace with namespace where provisioner is deployed
namespace: default
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
# replace with namespace where provisioner is deployed
namespace: default
subjects:
- kind: ServiceAccount
name: nfs-client-provisioner
# replace with namespace where provisioner is deployed
namespace: default
roleRef:
kind: Role
name: leader-locking-nfs-client-provisioner
apiGroup: rbac.authorization.k8s.io
kubectl create -f rbac.yaml
- 部署deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nfs-client-provisioner
labels:
app: nfs-client-provisioner
# replace with namespace where provisioner is deployed
namespace: default
spec:
replicas: 1
strategy:
type: Recreate
selector:
matchLabels:
app: nfs-client-provisioner
template:
metadata:
labels:
app: nfs-client-provisioner
spec:
serviceAccountName: nfs-client-provisioner
containers:
- name: nfs-client-provisioner
image: tantianran/nfs-subdir-external-provisioner:v4.0.1 # 使用到映象已經轉存到我的倉庫
volumeMounts:
- name: nfs-client-root
mountPath: /persistentvolumes
env:
- name: PROVISIONER_NAME
value: k8s-sigs.io/nfs-subdir-external-provisioner
- name: NFS_SERVER
value: 192.168.11.254 #此處修改成nfs伺服器的ip地址
- name: NFS_PATH
value: /data/nfs_k8s_storage_share # 此處修改成nfs的共享目錄
volumes:
- name: nfs-client-root
nfs:
server: 192.168.11.254 #此處修改成nfs伺服器的ip地址
path: /data/nfs_k8s_storage_share # 此處修改成nfs的共享目錄
kubectl create -f deployment.yaml
- 建立儲存類class.yaml
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: nfs-client
provisioner: k8s-sigs.io/nfs-subdir-external-provisioner # or choose another name, must match deployment's env PROVISIONER_NAME'
parameters:
archiveOnDelete: "false"
kubectl create -f class.yaml
- 檢視
# 檢視授權
kubectl get sa
# 檢視儲存類
kubectl get sc
使用
- 建立一個PVC
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: my-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 5Gi
storageClassName: nfs-client
在這裡,PVC名稱為my-pvc,請求5GB的儲存空間,並將儲存類設定為nfs-client
- 檢視pvc
# pvc已建立
[root@k8s-a-master nfs-storage-yaml]# kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
my-pvc Bound pvc-ce18ae0a-d4b4-4fa9-8241-b8b9551baa61 5Gi RWO nfs-client 2m6s
# 對應的pv已經自動建立
[root@k8s-a-master nfs-storage-yaml]# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
pvc-ce18ae0a-d4b4-4fa9-8241-b8b9551baa61 5Gi RWO Delete Bound default/my-pvc nfs-client 2m7s
[root@k8s-a-master nfs-storage-yaml]#
- 建立一個應用pod
apiVersion: v1
kind: Pod
metadata:
name: test-nginx-pod
spec:
containers:
- name: test-nginx-container
image: nginx
volumeMounts:
- name: storage
mountPath: /data
volumes:
- name: storage
persistentVolumeClaim:
claimName: my-pvc
- 檢視pod是否掛載成功
[root@k8s-a-master nfs-storage-yaml]# kubectl exec -it test-nginx-pod -- df -h
... 3.9G 0 3.9G 0% /sys/fs/cgroup
192.168.11.254:/data/nfs_k8s_storage_share/default-my-pvc-pvc-ce18ae0a-d4b4-4fa9-8241-b8b9551baa61 500G 1.8G 499G 1% /data
...
本文轉載於WX公眾號:不背鍋運維(喜歡的盆友關注我們):https://mp.weixin.qq.com/s/f8eCJNxztMFc3uQ_YJzwgg