一.背景
存儲資源在所有計算資源中扮演着十分重要的角色,大部分業務場景下都有可能使用到各類存儲資源。在Kubernetes中,系統通過Volume對集羣中的容器動態或靜態提供存儲資源。通常情況下,我們可以認爲容器或者Pod的生命週期時短暫的,當容器被銷燬時,容器內部的數據也同時被清除。爲了持久化保存容器的數據,Kubernetes引入了Volume,類似於Docker的Volume(Docker also has a concept of volumes, though it is somewhat looser and less managed. In Docker, a volume is simply a directory on disk or in another Container. Lifetimes are not managed and until very recently there were only local-disk-backed volumes. Docker now provides volume drivers, but the functionality is very limited for now)。這個Volume被某個Pod掛載之後,這個Pod裏面的所有容器都能使用這個Volume。Kubernetes目前支持的volume類型可以參考文末官方資料。
二.兩種Volume使用舉例
2.1 emptyDir
emptyDir: emptyDir是最基礎的Volume類型。每個emptyDir Volume是主機上的一個空目錄,可以被Pod中所有的容器共享。它對於容器來說是持久的,對於Pod則不是。刪除容器並不會對它造成影響,只有刪除整個Pod時,它纔會被刪除,它的生命週期與所掛載的Pod一致。簡而言之,emptyDir類型的Volume在Pod分配到Node上時被創建,Kubernetes會在Node主機上自動分配一個目錄,因此無需指定Node主機上對應的目錄文件。 這個目錄的初始內容爲空,當Pod從Node上移除時,emptyDir中的數據會被永久刪除。emptyDir主要用於一些無需永久保留的數據,例如臨時目錄,多容器共享目錄等。我們通過實際案例來理解一下,Pod gysl的yaml如下:
apiVersion: v1
kind: Pod
metadata:
name: gysl
spec:
containers:
- image: busybox
name: gysl-01
volumeMounts:
- mountPath: /gysl-01
name: gysl-volume
args:
- /bin/sh
- -c
- echo "This is a test file.">/gysl-01/test.gysl;sleep 20000
- image: busybox
name: gysl-02
volumeMounts:
- mountPath: /gysl-02
name: gysl-volume
args:
- /bin/sh
- -c
- cat /gysl-02/test.gysl;sleep 20000
volumes:
- name: gysl-volume
emptyDir: {}
創建Pod gysl,並查看相關信息:
[root@k8s-m k8s-volumes]# kubectl apply -f emptyDir.yaml
pod/gysl created
[root@k8s-m k8s-volumes]# kubectl get pod
NAME READY STATUS RESTARTS AGE
gysl 2/2 Running 0 10m
[root@k8s-m k8s-volumes]# kubectl logs gysl gysl-02
This is a test file.
該例中,我們創建了一個名爲gysl的Pod,這個pod裏面包含gysl-01和gysl-02兩個容器,這兩個容器同時掛載了名爲gysl-volume的emptyDir,gysl-01的掛載點爲/gysl-01,gysl-02的掛載點爲gysl-02,容器gysl-01創建了一個test.gysl的文件,內容爲:“This is a test file.”在容器gysl-02中,成功顯示了gysl-01創建的文件的內容。
2.2 hostPath
hostPath: hostPath的主要作用是將主機的文件或目錄掛載給Pod的容器使用,使得容器能以較爲良好的性能來存儲數據。接下來我們以Pod gysl-hostpath爲例來理解一下hostPath的相關概念,YAML文件如下:
apiVersion: v1
kind: Pod
metadata:
name: gysl-hostpath
spec:
nodeSelector:
role: test
containers:
- image: ubuntu:18.04
name: gysl-hostpath-container
volumeMounts:
- mountPath: /etc/gysl-test-01
name: gysl-02
- mountPath: /etc/gysl-test-02
name: gysl-01
- mountPath: /gysl-test-dir
name: gysl-dir
args:
- /bin/bash
- -c
- cat /etc/gysl-test-01 /etc/gysl-test-02;ls -l /gysl-test-dir;sleep 3600
volumes:
- hostPath:
path: /root/gysl-01
name: gysl-01
- hostPath:
path: /root/gysl-02
name: gysl-02
- hostPath:
path: /root/gysl-dir
name: gysl-dir
在Node k8s-n1的/root目錄下創建如下文件和目錄:
[root@k8s-n1 ~]# ll
總用量 8
-rw-r--r-- 1 root root 16 11月 21 20:31 gysl-01
-rw-r--r-- 1 root root 16 11月 21 20:31 gysl-02
drwxr-xr-x 2 root root 51 11月 21 20:32 gysl-dir
兩個文件的內容如下:
[root@k8s-n1 ~]# cat gysl-01
This is gysl-01
[root@k8s-n1 ~]# cat gysl-02
This is gysl-02
目錄gysl-dir的內容如下:
[root@k8s-n1 ~]# ll gysl-dir/
總用量 12
-rw-r--r-- 1 root root 16 11月 21 20:32 gysl-01
-rw-r--r-- 1 root root 16 11月 21 20:32 gysl-02
-rw-r--r-- 1 root root 16 11月 21 20:32 gysl-03
給Node k8s-n1指定一個標籤,apply 該Pod:
[root@k8s-m k8s-Volumes]# kubectl label node k8s-n1 role=test
node/k8s-n1 labeled
[root@k8s-m k8s-Volumes]# kubectl apply -f hostPath.yaml
pod/gysl-hostpath created
[root@k8s-m k8s-Volumes]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE
gysl-hostpath 1/1 Running 0 17s 10.244.1.177 k8s-n1 <none>
[root@k8s-m k8s-Volumes]# kubectl logs gysl-hostpath
This is gysl-02
This is gysl-01
total 12
-rw-r--r-- 1 root root 16 Nov 21 12:32 gysl-01
-rw-r--r-- 1 root root 16 Nov 21 12:32 gysl-02
-rw-r--r-- 1 root root 16 Nov 21 12:32 gysl-03
這個例子中,我把名爲gysl-02的hostPath文件掛載到了容器的文件/etc/gysl-test-01上,把名爲gysl-01的hostPath文件掛載到了容器的文件/etc/gysl-test-02上,把名爲gysl-dir的hostPath目錄掛載到了/gysl-test-dir下。通過logs命令,我們不難發現,目標已經達成。
這種掛載方式比emptyDir更爲持久,除非所在Node發生故障。不過,除了掛載一些配置文件/二進制文件之外,一般不採用該類掛載方式,因爲這樣的掛載操作增加了Pod文件與Node主機文件的耦合,不利於統一管理。
三.總結
3.1 在volume的配置過程中,涉及到具體掛載路徑的需要按照一定的規則來配置。例如:文件或目錄需要寫絕對路徑。不按照規則來配置,會出現以下報錯:
Warning Failed 8s (x3 over 20s) kubelet, k8s-n1 Error: Error response from daemon: create ~/gysl: "~/gysl-dir" includes invalid characters for a local volume name, only "[a-zA-Z0-9][a-zA-Z0-9_.-]" are allowed. If you intended to pass a host directory, use absolute path
3.2 emptyDir和hostPath都是比較常見的兩種類型的volume,在使用時需要根據具體情況進行配置。其他類型的volume可參考以上兩種類型及官方文檔進行配置,相關官方文檔會在文末給出。
四.相關資料
5.1 Volumes基本概念
5.3 文章涉及到的YAML文件