Kubernetes 存儲(chǔ)之 volume
作者:Yuan_sr
來(lái)源:SegmentFault 思否社區(qū)
容器磁盤(pán)上的文件的生命周期是短暫的,這就使得在容器中運(yùn)行重要應(yīng)用時(shí)會(huì)出現(xiàn)一些問(wèn)題,首先,當(dāng)容器崩潰時(shí),kubelet會(huì)重啟它,但是容器的文件將丟失---容器以干凈的狀態(tài)(鏡像最初的狀態(tài))重新啟動(dòng),其次,在pod 中同時(shí)運(yùn)行多個(gè)容器是,這些容器之間通常需要共享文件,Kubernetes中的volume 抽象就很好的解決了這些問(wèn)題。
背景
Kubernetes中的卷有明確的壽命--與封裝它的Pod相同,所以,卷的生命比Pod中所有容器都長(zhǎng),當(dāng)這個(gè)容器重啟時(shí)數(shù)據(jù)任然得以保存,當(dāng)然,當(dāng)Pod不存在時(shí),卷也將不復(fù)存在,也許更重要的是,Kubernetes支持多種類型的卷,Pod可以同時(shí)使用任意數(shù)量的卷。
卷的類型
Kubernetes支持一下類型的卷:
awsElasticBlockStoreazureDiskazureFilecephfscsidownwardAPIenptyDirfcflockergcePersistenDiskgitRepoglusterfshostPathiscsilocalnfspersistenVolumeClaimprejectedportworxVolumequobyterbdscaleIOsecretstorageosvsphereVolume
empthDir
暫存空間,例如用于基于磁盤(pán)的合并排序 用作長(zhǎng)時(shí)間計(jì)算崩潰恢復(fù)時(shí)的檢查點(diǎn) Web服務(wù)器容器提供數(shù)據(jù)時(shí),保存內(nèi)容管理器容器提取的文件
apiVersion: v1
kind: Pod
metadata:
name: test-pd
spec:
containers:
- image: k8s.gcr.io/test-webserver
name: test-container
volumeMounts:
- mountPath: /cache
name: cache-volume
- name: liveness-exec-container
image: busybox
imagePullPolicy: IfNotPresent
command: ["/bin/sh","-c","sleep 6000s"]
voluemMounts:
- mountPath: /test
name: cache-volume
volumes:
- name: cahce-volume
emptyDir: {}
hostPath
hostPath 卷將主節(jié)點(diǎn)的文件系統(tǒng)中的文件或目錄掛載到集群中
hostPath 的用途如下:
運(yùn)行需要訪問(wèn)的Docker內(nèi)部的容器;使用 /var/lib/docker的hostPath在容器中運(yùn)行cAdvisor;使用 /dev/cgroups的hostPath
除了所需的path 屬性外,用戶還可以為hostPaht 卷指定type

使用這種類型時(shí)注意,因?yàn)椋?/span>
由于每個(gè)節(jié)點(diǎn)上的文件都不同,具有相同配置(例如從podTemplate創(chuàng)建的)的pod在不同節(jié)點(diǎn)上的行為可能會(huì)有所不同 當(dāng)Kubernetes按照計(jì)劃添加資源感知調(diào)度時(shí),將無(wú)法考慮hostPath使用的資源 在底層主機(jī)上創(chuàng)建的文件或目錄只能由root寫(xiě)入,你需要在特權(quán)容器中以root身份運(yùn)行進(jìn)程,或修改主機(jī)上的文件權(quán)限以便寫(xiě)入hostPath卷
apiVersion: v1
kind: Pod
metadata:
name: test-pd
spec:
containers:
- image: k8s.gcr.io/test-webserver
name: test-container
volumeMounts:
- mountPath: /test-pd
name: test-volume
volumes:
- name: cahce-volume
hostPaht:
# directory location on host
path: /data
# this field is optional
type: Directory

評(píng)論
圖片
表情
