Volume類型
emptyDir
hostPath
Volume類型
volume是kubernetes Pod中多個容器訪問的共享目錄稳摄。volume被定義在pod上稚字,被這個pod的多個容器掛載到相同或不同的路徑下。volume的生命周期與pod的生命周期相同厦酬,pod內(nèi)的容器停止和重啟時一般不會影響volume中的數(shù)據(jù)胆描。所以一般volume被用于持久化pod產(chǎn)生的數(shù)據(jù)。
Kubernetes提供了眾多的volume類型仗阅,包括emptyDir昌讲、hostPath、nfs减噪、glusterfs短绸、cephfs、ceph rbd等筹裕。具體可以參考官方文檔醋闭。
本篇文章我們簡單說下emptyDir和hostPath。
emptyDir
emptyDir類型的volume在pod分配到node上時被創(chuàng)建朝卒,kubernetes會在node上自動分配 一個目錄证逻,因此無需指定宿主機node上對應的目錄文件。這個目錄的初始內(nèi)容為空抗斤,當Pod從node上移除時囚企,emptyDir中的數(shù)據(jù)會被永久刪除。
emptyDir Volume主要用于某些應用程序無需永久保存的臨時目錄瑞眼,多個容器的共享目錄等龙宏。
下面是一個pod掛載emptyDir的示例:
apiVersion: v1
kind: Pod
metadata:
name: test-pod
spec:
containers:
- image: test-webserver
name: test-container
volumeMounts:
- name: cache-volume
mountPath: /cache
volumes:
- name: cache-volume
emptyDir: {}
hostPath
hostPath Volume為pod掛載宿主機上的目錄或文件,使得容器可以使用宿主機的高速文件系統(tǒng)進行存儲伤疙。缺點是烦衣,在k8s中,pod都是動態(tài)在各node節(jié)點上調(diào)度。當一個pod在當前node節(jié)點上啟動并通過hostPath存儲了文件到本地以后花吟,下次調(diào)度到另一個節(jié)點上啟動時,就無法使用在之前節(jié)點上存儲的文件厨姚。
hostPath使用示例:
apiVersion: v1
kind: Pod
metadata:
name: test-pod
spec:
containers:
- image: test-webserver
name: test-container
volumeMounts:
- name: test-volume
mountPath: /www
volumes:
- name: test-volume
hostPath:
path: /data