Kubernetes Volume

2018年8月16日17:00:26 發(fā)表評論 4,657 ℃

emptyDir

emptyDir 是最基礎(chǔ)的 Volume 類型。

Kubernetes Volume

① 文件最底部 volumes 定義了一個 emptyDir 類型的 Volume shared-volume。

② producer 容器將 shared-volume mount 到 /producer_dir 目錄。

③ producer 通過 echo 將數(shù)據(jù)寫到文件 hello 里。

④ consumer 容器將 shared-volume mount 到 /consumer_dir 目錄。

⑤ consumer 通過 cat 從文件 hello 讀數(shù)據(jù)。

hostPath

hostPath Volume 的作用是將 Docker Host 文件系統(tǒng)中已經(jīng)存在的目錄 mount 給 Pod 的容器。大部分應用都不會使用 hostPath Volume,因為這實際上增加了 Pod 與節(jié)點的耦合,限制了 Pod 的使用。不過那些需要訪問 Kubernetes 或 Docker 內(nèi)部數(shù)據(jù)(配置文件和二進制庫)的應用則需要使用 hostPath。

kubectl edit --namespace=kube-system pod kube-apiserver-master #查看 kube-apiserver Pod 的配置

外部 Storage Provider

AWS Elastic Block Store 示例

Kubernetes Volume

分布式存儲Ceph示例

Kubernetes Volume

Ceph文件系統(tǒng)的/some/path/in/side/cephfs目錄被mount到容器路徑/test-ceph。

PersistentVolume (PV)和PersistentVolumeClaim (PVC) 

PersistentVolume (PV) 

是外部存儲系統(tǒng)中的一塊存儲空間,由管理員創(chuàng)建和維護。與 Volume 一樣,PV 具有持久性,生命周期獨立于 Pod。

PersistentVolumeClaim (PVC) 

是對 PV 的申請 (Claim)。PVC 通常由普通用戶創(chuàng)建和維護。需要為 Pod 分配存儲資源時,用戶可以創(chuàng)建一個PVC,指明存儲資源的容量大小和訪問模式(比如只讀)等信息,Kubernetes 會查找并提供滿足條件的 PV。

NFS PersistentVolume

每個node安裝nfs-utils,并在nfs-server設(shè)置好共享。

[root@nfs-server ~]# cat /etc/exports

/nfsdata 192.168.159.0/24(rw,async,no_root_squash)

創(chuàng)建一個PV

Kubernetes Volume

① capacity 指定 PV 的容量為 1G。

② accessModes 指定訪問模式為 ReadWriteOnce,支持的訪問模式有:

ReadWriteOnce – PV 能以 read-write 模式 mount 到單個節(jié)點。

ReadOnlyMany – PV 能以 read-only 模式 mount 到多個節(jié)點。

ReadWriteMany – PV 能以 read-write 模式 mount 到多個節(jié)點。

③ persistentVolumeReclaimPolicy 指定當 PV 的回收策略為 Recycle,支持的策略有:

Retain – 需要管理員手工回收。

Recycle – 清除 PV 中的數(shù)據(jù),效果相當于執(zhí)行 rm -rf /thevolume/*。

Delete – 刪除 Storage Provider 上的對應存儲資源,例如 AWS EBS、GCE PD、Azure Disk、OpenStack Cinder Volume 等。

④ storageClassName 指定 PV 的 class 為 nfs。相當于為 PV 設(shè)置了一個分類,PVC 可以指定 class 申請相應 class 的 PV。

⑤ 指定 PV 在 NFS 服務器上對應的目錄。

創(chuàng)建PVC

Kubernetes Volume

Kubernetes Volume

在pod中使用儲存,在 volumes 中通過 persistentVolumeClaim 指定使用 mypvc1 申請的 Volume。

Kubernetes Volume

回收PV

kubectl delete pvc mypvc1 #刪除PVC mypvc1

PV動態(tài)供給

StorageClass standard

Kubernetes Volume

StorageClass slow

Kubernetes Volume

這兩個 StorageClass 都會動態(tài)創(chuàng)建 AWS EBS,不同在于 standard 創(chuàng)建的是 gp2 類型的 EBS,而 slow 創(chuàng)建的是 io1 類型的 EBS。

PVC 在申請 PV 時,只需要指定 StorageClass 和容量以及訪問模式,比如:

Kubernetes Volume

Mysql數(shù)據(jù)庫示例

1、創(chuàng)建PC和PVC

Kubernetes Volume

Kubernetes Volume

2、部署mysql

Kubernetes Volume

kubectl run -it --rm --image=mysql:5.6 --restart=Never mysql-client -- mysql -h mysql -ppassword #訪問mysql server

【騰訊云】云服務器、云數(shù)據(jù)庫、COS、CDN、短信等云產(chǎn)品特惠熱賣中

發(fā)表評論

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: