iT邦幫忙

2019 iT 邦幫忙鐵人賽

DAY 22
1
Kubernetes

15 分鐘學習系列 - 第一次學 Kubernetes 就上手系列 第 22

Day 22 - 使用永久性儲存 Persistent Volume

Container 根據其起始與結束資料的存續有一定的生命週期, 因此當 container 終止後重新啟動, 檔案系統的資料可能會遺失.
因應這樣的需求, Pod 可以設置永久性的儲存來獨立於 Container 生命週期的資料儲存. 典型的應用是具有狀態 (Stateful) 的應用程式. 本篇筆記將補充如何設置永久性儲存 Persistent Volume.

使用 Redis

最簡單的應用是使用 redis 儲存 key-value 資料, 最主要的兩個部分是 MountPath 和 設定為 emptyDir 空的 storage
volumeMounts:
- name: redis-storage
mountPath: /data/redis
volumes:

  • name: redis-storage
    emptyDir: {}
# redis.yaml
apiVersion: v1
kind: Pod
metadata:
  name: redis
spec:
  containers:
  - name: redis
    image: redis
    volumeMounts:
    - name: redis-storage
      mountPath: /data/redis
  volumes:
  - name: redis-storage
    emptyDir: {}

使用命令 kubectl create -f .\redis.yaml 部署 redis pod.

PS C:\k8s\redis> kubectl create -f .\redis.yaml
pod "redis" created

PS C:\k8s\redis> kubectl get pod redis -w
NAME      READY     STATUS    RESTARTS   AGE
redis     2/2       Running   0          45s

使用命令 kubectl exec -it redis -- /bin/bash 進入 pod bash shell

PS C:\k8s\redis> kubectl exec -it redis -- /bin/bash
Defaulting container name to redis.
Use 'kubectl describe pod/redis -n default' to see all of the containers in this pod.
root@redis:/data# cd redis/
root@redis:/data/redis# ls
root@redis:/data/redis# echo Hello > test-file

使用命令 kubectl delete pod redis 將 pod 刪除

PS C:\k8s\redis> kubectl delete pod redis
pod "redis" deleted

使用永久性儲存 Persistent Volume

Pod 也可以配置永久性儲存 Persistent Volume, 以下的 yaml 檔案中 volumeMounts 中的 mountPath 對應的是 container 中 mount 的路徑, 指向的 volume name: task-pv-storage, 而在 volumes 定義中, hostPath 對應的是 local 叢集的路徑 /c/temp/data, type: DirectoryOrCreate 設定為目錄, 若不存在將建立

#pv-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: task-pv-pod
spec:
containers:
- image: nginx
ports:
- containerPort: 80
name: "http-server"
name: task-pv-container
volumeMounts:
- mountPath: /usr/share/nginx/html
name: task-pv-storage
volumes:
- name: task-pv-storage
hostPath:
path: /c/temp/data
type: DirectoryOrCreate

使用命令 kubectl create -f .\pv-pod.yaml 部署

PS C:\k8s\pv> kubectl create -f .\pv-pod.yaml
pod "task-pv-pod" created

使用命令 kubectl exec -it task-pv-pod -- /bin/bash 進入 container

PS C:\k8s\pv> kubectl exec -it task-pv-pod -- /bin/bash
Defaulting container name to task-pv-container.
Use 'kubectl describe pod/task-pv-pod -n default' to see all of the containers in this pod.

將內容寫入檔案中

root@task-pv-pod:/usr/share/nginx/html# echo "Hello World" > index.html
root@task-pv-pod:/usr/share/nginx/html# cat index.html
Hello World

使用命令 kubectl delete -f .\pv-pod.yaml 將 pod 刪除

PS C:\k8s\pv> kubectl delete -f .\pv-pod.yaml
pod "task-pv-pod" deleted

使用命令 kubectl create -f .\pv-pod.yaml 將 pod 建立
再次檢查 index.html 檔案是否仍在

PS C:\k8s\pv> kubectl create -f .\pv-pod.yaml
pod "task-pv-pod" created
PS C:\k8s\pv> kubectl exec -it task-pv-pod -- /bin/bash
Defaulting container name to task-pv-container.
Use 'kubectl describe pod/task-pv-pod -n default' to see all of the containers in this pod.
root@task-pv-pod:/# cat /usr/share/nginx/html/index.html
Hello World

驗證結果: index.html 檔案仍然存在. 因此 Persistent Volume 設定成功!


參考資料:


上一篇
Day 21 - 使用與設定 liveness probes
下一篇
Day 23 - 安裝 Helm
系列文
15 分鐘學習系列 - 第一次學 Kubernetes 就上手30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 則留言

0
cwchiu
iT邦新手 3 級 ‧ 2019-08-22 14:28:20

yaml 亂了 不太容易看

我要留言

立即登入留言