歡迎訪問個人GitHub
https://github.com/zq2599/blog_demosnode
內容:全部原創文章分類彙總及配套源碼,涉及Java、Docker、Kubernetes、DevOPS等;git
關於Local Persistent Volumes
文中將Local Persistent Volumes簡稱爲Local PV;程序員
- Kubernetes的Local PV自1.7版本進行alpha發佈,在1.10版本beta發佈,最終的正式發佈(General Availability)是在1.14版本;
- 一個Local PV對應指定節點上的一處本地磁盤空間;
- 相比NFS之類的遠程存儲,Local PV提供了本地IO帶來的更好性能;
和HostPath Volume的區別
Local PV出現以前,使用本地磁盤的方法是HostPath Volume,同爲使用本地磁盤,區別在哪呢?github
- 最重要的區別,就是Local PV和具體節點是有關聯的,這意味着使用了Local PV的pod,重啓屢次都會被Kubernetes scheduler調度到同一節點,而若是用的是HostPath Volume,每次重啓均可能被Kubernetes scheduler調度到新的節點,而後使用一樣的本地路徑;
- 當咱們要用HostPath Volume的時候,既能夠在PVC聲明,又能夠直接寫到Pod的配置中,可是Local PV只能在PVC聲明,對於PV資源,一般都有專人管理,這樣就避免了Pod開發者擅自使用本地磁盤帶來的衝突和風險;
- 另外要注意的是,HostPath Volume和Local PV都是在使用本地磁盤,和常見的分佈式文件系統相比,本地磁盤故障會致使數據丟失,保存重要數據請勿使用HostPath Volume和Local PV;
基本概念說完了,接下來實戰體驗;數據庫
實戰環境信息
- 操做系統:CentOS Linux release 7.8.2003 (Core)
- kubernetes:1.15.3
- helm:2.16.1
體驗Local PV的步驟簡述
本次實戰的目標是快速建立Local PV,並驗證該Local PV正常可用,全文由如下部分組成:api
- 建立Local PV;
- 經過helm下載tomcat的chart;
- 修改chart,讓tomcat使用剛纔建立的Local PV;
- 部署tomcat;
- 在服務器上檢查文件夾已正常寫入;
參考文章
若是您想了解Kubernetes和helm的更多信息,請參考:瀏覽器
- 《kubespray2.11安裝kubernetes1.15》;
- 《部署和體驗Helm(2.16.1版本)》;
準備完畢,開始實操;tomcat
建立PV
- 在kubernetes工做節點建立文件夾給Local PV使用,我這是:/root/temp/202005/24/local-pv/
- 給上述文件夾讀寫權限:chmod -R a+r,a+w /root/temp/202005/24/local-pv
- 建立文件local-storage-pv.yaml,內容以下:
apiVersion: v1
kind: PersistentVolume
metadata:
name: example-pv
spec:
capacity:
storage: 10Gi
volumeMode: Filesystem
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Delete
storageClassName: local-storage
local:
path: /root/temp/202005/24/local-pv
nodeAffinity:
required:
nodeSelectorTerms:
- matchExpressions:
- key: kubernetes.io/hostname
operator: In
values:
- node1
- 關於local-storage-pv.yaml有如下幾點須要注意:
a. spec.storageClassName等於local-storage,後面的PVC中也要指定storageClassName等於local-storage;
b. spec.nodeAffinity是必填參數,用於創建Local PV和節點的關係,spec.nodeAffinity.required,nodeSelectorTerms.matchExpressions.values的值包含node1,表示該Local PV能夠在主機名爲node1的節點建立;
- 執行命令kubectl apply -f local-storage-pv.yaml,便可建立PV;
- 執行kubectl describe pv example-pv檢查是否建立成功,以下圖紅框所示,此PV已經可用:

使用PV
接下來經過helm部署tomcat,而且讓tomcat使用上述Local PV,請確保helm已經裝好;服務器
- 增長helm倉庫(帶有tomcat的倉庫):helm repo add bitnami https://charts.bitnami.com/bitnami
- 下載tomcat的chart:helm fetch bitnami/tomcat
- chart下載成功後,當前目錄出現tomcat配置壓縮包tomcat-6.2.4.tgz,解壓:tar -zxvf tomcat-6.2.4.tgz
- 解壓獲得tomcat文件夾,進入後打開values.yaml文件,找到persistence節點,增長下圖紅框中的內容:

- 在tomcat目錄下執行命令:helm install --name-template tomcat001 -f values.yaml . --namespace hello-storageclass
- 查看tomcat的pod和service狀況,一切正常,而且端口映射到了宿主機的31835:

- 瀏覽器訪問宿主機IP:31835,出現tomcat歡迎頁面:

- 去目錄/root/temp/202005/24/local-pv/檢查磁盤使用狀況,以下圖,可見已分配給tomcat的PVC,而且寫入了tomcat的基本數據:

- 再次查看Local PV,發現狀態已經改變:

至此能夠確認,tomcat用上了Local PV,數據被保存在宿主機的指定文件夾;
清理Local PV
- 通常來講,清理PV要作以下操做:
a. 刪除pod,或者deployment;
b. 刪除pvc;
c. 刪除Local PV;
- 這裏因爲用上了helm,所以經過helm將上述步驟1和2執行掉,既命令helm delete tomcat001
- 再在local-storage-pv.yaml所在目錄執行kubectl delete -f local-storage-pv.yaml便可刪除Local PV;
至此,Local PV的學習和實踐就完成了,若是您正在使用這種存儲,但願本文能給您一些參考;微信
你不孤單,欣宸原創一路相伴
- Java系列
- Spring系列
- Docker系列
- kubernetes系列
- 數據庫+中間件系列
- DevOps系列
歡迎關注公衆號:程序員欣宸
微信搜索「程序員欣宸」,我是欣宸,期待與您一同暢遊Java世界...
https://github.com/zq2599/blog_demos