Docker Kubernetes Volume 本地數據卷
emptyDir
- 當Pod分配到Node時,首先創建一個空卷,並掛載到Pod中的容器。
- Pod中的容器可以讀取和寫入卷中的文件。
- 當Pod從節點中刪除emptyDir時,該數據也會被刪除。
- 注:適用於容器之間的數據共享。
hostPath
- 一個hostPath卷掛載Node文件系統上的文件或目錄到Pod中的容器。
- 注:指定宿主級的數據目錄掛載到容器中。
環境:
- 系統:Centos 7.4 x64
- Docker版本:18.09.0
- Kubernetes版本:v1.8
- 管理節點:192.168.1.79
- 工作節點:192.168.1.78
- 工作節點:192.168.1.77
創建emptydir實例
1、管理節點:創建yaml文件
vim emptydir.yaml
apiVersion: v1 kind: Pod metadata: name: test-pd spec: containers: - image: nginx:1.12 name: test-container volumeMounts: - mountPath: /cache name: cache-volume volumes: - name: cache-volume emptyDir: {}

# api版本 apiVersion: v1 # 指定創建資源對象 kind: Pod # 源數據、可以寫name,命名空間,對象標簽 metadata: # 服務名稱 name: test-pd # 容器資源信息 spec: # 容器管理 containers: # 鏡像名稱 - image: nginx:1.12 # 容器名稱 name: test-container # 容器數據卷管理 volumeMounts: # 容器內掛載目錄 - mountPath: /cache # 容器掛載數據名稱 name: cache-volume # 宿主數據卷管理 volumes: # 創建數據卷名稱 - name: cache-volume # emptydir標准語法 emptyDir: {}
2、管理節點:創建Pod
kubectl create -f emptydir.yaml
3、測試

命令:kubectl exec test-pd -it bash root@test-pd:/# cd /cache/ root@test-pd:/cache# ls root@test-pd:/cache#

命令:kubectl describe pods test-pd Mounts: /cache from cache-volume (rw) Conditions: Type Status Initialized True Ready True PodScheduled True Volumes: cache-volume: Type: EmptyDir (a temporary directory that shares a pod's lifetime) Medium: QoS Class: BestEffort Node-Selectors: <none> Tolerations: <none>
創建hostPath實例
1、管理節點:創建yaml文件
apiVersion: v1 kind: Pod metadata: name: test-pd2 spec: containers: - image: nginx:1.12 name: test-container volumeMounts: - mountPath: /data name: test-volume volumes: - name: test-volume hostPath: path: /etc/default type: Directory

# api版本 apiVersion: v1 # 指定創建資源對象 kind: Pod # 源數據、可以寫name,命名空間,對象標簽 metadata: # 服務名稱 name: test-pd2 # 容器資源信息 spec: # 容器管理 containers: # 鏡像名稱 - image: nginx:1.12 name: test-container # 容器數據卷管理 volumeMounts: # 容器掛載目錄 - mountPath: /data # 容器掛載數據名稱 name: test-volume # 宿主數據卷管理 volumes: # 創建數據卷名稱 - name: test-volume # 數據卷地址 hostPath: # 掛載到容器的宿主目錄 path: /etc/default # 類型為目錄文件 type: Directory
2、管理節點:創建Pod
kubectl create -f hostpath.yaml
3、測試
命令:kubectl exec test-pd2 -it bash root@test-pd2:/# cd /data root@test-pd2:/data# ls grub nss useradd yyy