Kubernetes部分Volume型別介紹及yaml示例

振宇要低調發表於2017-03-21

1、EmptyDir(本地資料卷)

  EmptyDir型別的volume建立於pod被排程到某個宿主機上的時候,而同一個pod內的容器都能讀寫EmptyDir中的同一個檔案。一旦這個pod離開了這個宿主機,EmptyDirr中的資料就會被永久刪除。所以目前EmptyDir型別的volume主要用作臨時空間,比如Web伺服器寫日誌或者tmp檔案需要的臨時目錄。yaml示例如下:

[root@k8s-master demon2]# cat test-emptypath.yaml 
apiVersion: v1
kind: Pod
metadata:
  labels:
    name: test-emptypath
    role: master
  name: test-emptypath
spec:
  containers:
    - name: test-emptypath
      image: registry:5000/back_demon:1.0
      volumeMounts:
       - name: log-storage
         mountPath: /home/laizy/test/
      command:
      - /run.sh
  volumes:
  - name: log-storage
    emptyDir: {}

2、HostDir(本地資料卷)

  HostDir屬性的volume使得對應的容器能夠訪問當前宿主機上的指定目錄。例如,需要執行一個訪問Docker系統目錄的容器,那麼就使用/var/lib/docker目錄作為一個HostDir型別的volume;或者要在一個容器內部執行CAdvisor,那麼就使用/dev/cgroups目錄作為一個HostDir型別的volume。一旦這個pod離開了這個宿主機,HostDir中的資料雖然不會被永久刪除,但資料也不會隨pod遷移到其他宿主機上。因此,需要注意的是,由於各個宿主機上的檔案系統結構和內容並不一定完全相同,所以相同pod的HostDir可能會在不同的宿主機上表現出不同的行為。yaml示例如下:

[root@k8s-master demon2]# cat test-hostpath.yaml 
apiVersion: v1
kind: Pod
metadata:
  labels:
    name: test-hostpath
    role: master
  name: test-hostpath
spec:
  containers:
    - name: test-hostpath
      image: registry:5000/back_demon:1.0
      volumeMounts:
       - name: ssl-certs
         mountPath: /home/laizy/test/cert
         readOnly: true
      command:
      - /run.sh
  volumes:
  - name: ssl-certs
    hostPath:
     path: /etc/ssl/certs

3、NFS(網路資料卷)

  NFS型別的volume。允許一塊現有的網路硬碟在同一個pod內的容器間共享。yaml示例如下:

[root@k8s-master demon2]# cat test-nfspath.yaml 
apiVersion: v1
kind: Pod
metadata:
  labels:
    name: test-nfspath
    role: master
  name: test-nfspath
spec:
  containers:
    - name: test-nfspath
      image: registry:5000/back_demon:1.0
      volumeMounts:
       - name: nfs-storage
         mountPath: /home/laizy/test/
      command:
      - /run.sh
  volumes:
  - name: nfs-storage
    nfs:
     server: 192.168.20.47
     path: "/data/disk1"

4、Secret(資訊資料卷)

  Kubemetes提供了Secret來處理敏感資料,比如密碼、Token和金鑰,相比於直接將敏感資料配置在Pod的定義或者映象中,Secret提供了更加安全的機制(Base64加密),防止資料洩露。Secret的建立是獨立於Pod的,以資料卷的形式掛載到Pod中,Secret的資料將以檔案的形式儲存,容器通過讀取檔案可以獲取需要的資料。yaml示例如下:

[root@k8s-master demon2]# cat secret.yaml 
apiVersion: v1
kind: Secret
metadata:
 name: mysecret
type: Opaque
data:
 username: emhlbnl1
 password: eWFvZGlkaWFv
[root@k8s-master demon2]# cat test-secret.yaml 
apiVersion: v1
kind: Pod
metadata:
  labels:
    name: test-secret
    role: master
  name: test-secret
spec:
  containers:
    - name: test-secret
      image: registry:5000/back_demon:1.0
      volumeMounts:
       - name: secret
         mountPath: /home/laizy/secret
         readOnly: true
      command:
      - /run.sh
  volumes:
  - name: secret
    secret:
     secretName: mysecret

 

 

 

相關文章