目錄貼:Kubernetes學習系列
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