1. 程式人生 > >Docker Kubernetes Volume 本地資料卷

Docker Kubernetes Volume 本地資料卷

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

[email protected]-pd:/# cd /cache/
[email protected]:/cache# ls
[email protected]:/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

[email protected]-pd2:/# cd /data
[email protected]:/data# ls
grub  nss  useradd  yyy