Kubernetes volume數據卷

Volume是kubernetes Pod中多個容器訪問的共享目錄node

Kubernetes提供了衆多的volume類型,包括:emptyDir、hostPath、NFS、GlusterFS、configMap、Cephfsnginx

一、emptyDir
emptyDir類型的vloume在Pod分配到node上時被建立,kubernetes會在node上自動分配一個目錄,因此無須指定宿主機node上對應的目錄文件,該目錄初始化內容爲空,當Pod從node上被移除時,emptyDir中的數據會被永久刪除web

1.編輯emptyDir.yaml文件api

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: web-demo-empty
spec:
  replicas: 2
  selector:
    matchLabels:
      app: app-demo-empty
  template:
    metadata:
      labels:
        app: app-demo-empty
    spec:
      containers:
      - name: tomcat-demo
        image: tomcat
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 8080
        volumeMounts:
          # 將/mydata-data目錄掛載到共享倉庫
          - mountPath: /mydata-data
            name: datavol

      - name: nginx-demo
        image: nginx
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 80
        volumeMounts:
          # 將/mydata-data目錄掛載到共享倉庫
          - mountPath: /mydata-data
            name: datavol

      # emptyDir是一個共享倉庫(中央倉庫)
      volumes:
        - name: datavol
          emptyDir: {}

2.執行kubectl createtomcat

$ kubectl create -f  emptyDir.yaml
deployment.extensions/web-demo-empty created

3.先進入tomcat-demo容器中建立文件bash

$  kubectl exec -it web-demo-empty-9b5644c45-7bpn5 -c tomcat-demo bash
$ touch /mydata-data/data.txt
$ ls /mydata-data/
data.txt

4.在進入nginx-demo容器查看是否存在data.txt文件app

$ kubectl exec -it web-demo-empty-9b5644c45-7bpn5 -c nginx-demo bash
$ ls /mydata-data/
data.txt

通過試驗證實emptyDir是一個共享空目錄,能夠讓Pod中的多個容器之間共享該目錄ide

二、hostPath
hostPath類型的vloume爲Pod掛載到宿主機上的目錄或文件,使得容器能夠使用宿主機的文件系統進行存儲,可是在kubernetes中,Pod都是基於schedule組件來動態調度在不一樣的node節點上,當一個Pod在當前node節點上啓動並經過hostPath存儲了文件到本地後,下次在調度另一個節點上啓動時,就沒法使用以前節點上存儲的文件了。測試

1.編輯hostPath.yaml文件code

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: web-demo-hostpath
spec:
  replicas: 2
  selector:
    matchLabels:
      app: app-demo-hostpath
  template:
    metadata:
      labels:
        app: app-demo-hostpath
    spec:
      containers:

      containers:
      - name: nginx-demo
        image: nginx
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 80
        volumeMounts:
          - mountPath: /mydata-data
            name: datavol

      volumes:
        - name: datavol
          hostPath:
            path: "/data"

2.執行kubectl create

$ kubectl create -f hostPath.yaml
deployment.extensions/web-demo-hostpath created

3.須要在每一個node節點建立/data目錄,並建立測試文件

$ mkdir /data 
$ touch /data/test.txt

4.進入容器裏查看是否將/mydata-data掛載到了宿主機上

$ kubectl exec -it web-demo-hostpath-7866c644c4-7f8fk bash
$ ls /mydata-data/
test.txt
相關文章
相關標籤/搜索