k8s 持久化儲存
以下是圖片內容的文字辨識及翻譯成繁體中文:
數據卷 emptydir,是本地存儲,pod 重啟,數據不存在了,需要對數據持久化存儲
1. NFS,網絡存儲
- pod 重啟,數據還存在的方式
第一步:找一台服務器作為 NFS 服務端
(1) 安裝 NFS
yum install -y nfs-utils
(2) 設置掛載路徑
[root@atonline ntest /]# vi /etc/exports
/data/nfs *(rw,no_root_squash)
(3) 掛載路徑需要創建出來
[root@atonline ntest data]# mkdir nfs
[root@atonline ntest data]# ls
nfs
說明:
- emptydir 是 Kubernetes 中的一種臨時存儲方式,pod 重啟後數據會丟失。
- NFS(網絡文件系統) 提供一種數據持久化存儲解決方案,pod 重啟後數據仍然保留。
- 操作流程:
- 安裝 NFS 軟件。
- 配置 NFS 的掛載路徑。
- 確保掛載的目錄已創建。
以下是圖片內容的文字辨識及翻譯成繁體中文:
第二步:在 k8s 集群 node 節點安裝 NFS
yum install -y nfs-utils
說明:
- 在 Kubernetes 集群的每個節點上安裝 NFS 客戶端工具 (
nfs-utils
),以便節點能夠掛載 NFS 共享存儲。
以下是圖片內容的文字辨識及翻譯成繁體中文:
第三步:在 NFS 服務器啟動 NFS 服務
# systemctl start nfs
# ps -ef | grep nfs
15:03 ? 00:00:00 [nfsd4_callbacks]
第四步:在 k8s 集群部署應用使用 NFS 持久化網絡存儲
vi nfs-nginx.yaml
kubectl apply -f nfs-nginx.yaml
[root@k8smaster pv]# kubectl exec -it nginx-depl-6b96bc8d7d-92qhv bash
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version.
root@nginx-depl-6b96bc8d7d-92qhv:/# ls /usr/share/nginx/html
index.html
說明:
第三步:啟動 NFS 服務
- 啟動 NFS 服務:通過
systemctl start nfs
。 - 驗證 NFS 啟動狀態:使用
ps -ef | grep nfs
查看進程是否正常啟動。
第四步:部署應用並使用 NFS
- 編輯 YAML 文件:創建或修改
nfs-nginx.yaml
文件,配置 NFS 為持久化存儲。 - 應用配置文件:使用
kubectl apply
部署應用。 - 驗證 NFS 存儲是否生效:
- 使用
kubectl exec
進入 Pod。 - 確認存儲目錄(如
/usr/share/nginx/html
)內的文件是否存在,例如index.html
。
- 使用
PV 和 PVC
-
PV(Persistent Volume,持久化存儲):
- 持久化存儲,對存儲資源進行抽象。
- 對外提供可調用的地方(生產者)。
-
PVC(Persistent Volume Claim,持久化存儲請求):
- 用於調用,不需要關心內部實現細節(消費者)。