Tìm hiểu về loại Storage Volume emptyDir trong Kubernetes – Cuongquach.com | Như chúng ta đã biết thì khi nói về vùng lưu trữ (storage volume) trong Kubernetes thì K8S hiện hỗ trợ đến hơn 20 loại Volume Storage khác nhau: emptyDir, hostPath, csi, local, … phục vụ các nhu cầu hoạt động khác nhau khi thiết kế ứng dụng hệ thống. Trong bài viết này chúng ta sẽ tìm hiểu về loại Volume Storage cơ bản đầu tiên trong Kubernetes là : emptyDir
Contents
Storage Volume emptyDir trong Kubernetes là gì ?
Một số đặc điểm của Volume emptyDir:
- Một emptyDir volume sẽ được khởi tạo với vùng lưu trữ rỗng (khởi tạo trên ổ cứng của Kubernetes Node) đi theo một Pod được khởi động trên Kubernetes Node.
- Một emptyDir volume có thể được mount với bất kì mount point nào trong các container của Pod.
- Các containers trong cùng 1 Pod có thể đọc/ghi với emptyDir Volume.
- Khi một Pod được khởi động lại hoặc bị xoá đi, thì dữ liệu trong emptyDir sẽ bị mất hết.
- Còn khi Container trong Pod bị crash (nhưng không ảnh hưởng gây đến việc xoá/khởi động lại Pod) thì dữ liệu trong volume emptyDir vẫn còn.
- Vì vậy mà emptyDir volume không dùng cho việc lưu trữ các dữ liệu mang có yêu cầu thời gian dài hạn và toàn vẹn dữ liệu như (database, dữ liệu ứng dụng, dữ liệu giám sát hệ thống,…)
- EmptyDir ngoài hỗ trợ tạo khu vực lưu trữ trên ổ cứng, còn hỗ trợ tạo khu vực lưu trữ volume trên RAM với tmpfs nữa đấy.
Một số tình huống sử dụng của Volume emptyDir:
- Thường volume emptyDir dùng với mục đích là một thư mục chứa cache tạm thời (temporary cache).
- Là khu vực lưu trữ dữ liệu chung (shared storage) giữa các container trong cùng 1 Pod, từ đó các ứng dụng container trong cùng Pod có thể trao đổi dữ liệu với nhau qua volume chung.
Cấu hình một emptyDir Volume
apiVersion: v1 kind: Pod metadata: name: test-pd spec: containers: - image: k8s.gcr.io/test-webserver name: test-container volumeMounts: - mountPath: /cache name: cache-volume volumes: - name: cache-volume emptyDir: {}
Demo với emptyDir Volume
- Chúng ta sẽ tạo 2 container trong cùng 1 Pod , sau đó mở shell vào từng con container ghi dữ liệu vào thư mục emptyDir volume đã được mount.
- Container `myvolumes-cuongquach-1` sẽ mount emptyDir volume vào thư mục `/demo1`.
- Container `myvolumes-cuongquach-2` sẽ mount emptyDir volume vào thư mục `/demo2`.
# vi myVolumes-Pod.yaml apiVersion: v1 kind: Pod metadata: name: cuongquach-pod spec: containers: - image: alpine imagePullPolicy: IfNotPresent name: myvolumes-cuongquach-1 command: ['sh', '-c', 'tail -f /dev/null'] volumeMounts: - mountPath: /demo1 name: demo-volume - image: alpine imagePullPolicy: IfNotPresent name: myvolumes-cuongquach-2 command: ['sh', '-c', 'tail -f /dev/null'] volumeMounts: - mountPath: /demo2 name: demo-volume volumes: - name: demo-volume emptyDir: {}
– Khởi tạo Pod với emptyDir volume làm lab nào.
# kubectl apply -f myVolumes-Pod.yaml pod/cuongquach-pod created
– Tạo xong thì coi thông tin chi tiết về Pod `cuongquach-pod` nghen.
# kubectl describe pod -n default cuongquach-pod Name: cuongquach-pod Namespace: default Priority: 0 Node: docker-desktop/192.168.65.3 Start Time: Thu, 19 Mar 2020 23:39:35 +0700 Labels: <none> Annotations: kubectl.kubernetes.io/last-applied-configuration: {"apiVersion":"v1","kind":"Pod","metadata":{"annotations":{},"name":"cuongquach-pod","namespace":"default"},"spec":{"containers":[{"comman... Status: Running IP: 10.1.0.85 Containers: myvolumes-cuongquach-1: Container ID: docker://498feba86cb319276013cd42e7926fa84b535193ce0203a97d9ed26f32e8e0d3 Image: alpine Image ID: docker-pullable://alpine@sha256:ab00606a42621fb68f2ed6ad3c88be54397f981a7b70a79db3d1172b11c4367d Port: <none> Host Port: <none> Command: sh -c sleep 3600 State: Waiting Reason: CrashLoopBackOff Last State: Terminated Reason: Error Exit Code: 1 Started: Thu, 19 Mar 2020 23:40:25 +0700 Finished: Thu, 19 Mar 2020 23:40:25 +0700 Ready: False Restart Count: 3 Environment: <none> Mounts: /demo1 from demo-volume (rw) /var/run/secrets/kubernetes.io/serviceaccount from default-token-lqpg5 (ro) myvolumes-cuongquach-2: Container ID: docker://311897a85ed7fc23bb331c5b33fcc6e8d63942201087215571a0c4186403e62d Image: alpine Image ID: docker-pullable://alpine@sha256:ab00606a42621fb68f2ed6ad3c88be54397f981a7b70a79db3d1172b11c4367d Port: <none> Host Port: <none> Command: sh -c sleep 3600 State: Waiting Reason: CrashLoopBackOff Last State: Terminated Reason: Error Exit Code: 1 Started: Thu, 19 Mar 2020 23:40:25 +0700 Finished: Thu, 19 Mar 2020 23:40:25 +0700 Ready: False Restart Count: 3 Environment: <none> Mounts: /demo2 from demo-volume (rw) /var/run/secrets/kubernetes.io/serviceaccount from default-token-lqpg5 (ro) Conditions: Type Status Initialized True Ready False ContainersReady False PodScheduled True Volumes: demo-volume: Type: EmptyDir (a temporary directory that shares a pod's lifetime) Medium: SizeLimit: <unset> default-token-lqpg5: Type: Secret (a volume populated by a Secret) SecretName: default-token-lqpg5 Optional: false QoS Class: BestEffort Node-Selectors: <none> Tolerations: node.kubernetes.io/not-ready:NoExecute for 300s node.kubernetes.io/unreachable:NoExecute for 300s Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal Scheduled 79s default-scheduler Successfully assigned default/cuongquach-pod to docker-desktop Normal Pulled 64s (x3 over 78s) kubelet, docker-desktop Container image "alpine" already present on machine Normal Created 63s (x3 over 78s) kubelet, docker-desktop Created container myvolumes-cuongquach-1 Normal Started 63s (x3 over 78s) kubelet, docker-desktop Started container myvolumes-cuongquach-1 Normal Pulled 63s (x3 over 78s) kubelet, docker-desktop Container image "alpine" already present on machine Normal Created 63s (x3 over 77s) kubelet, docker-desktop Created container myvolumes-cuongquach-2 Normal Started 62s (x3 over 77s) kubelet, docker-desktop Started container myvolumes-cuongquach-2 Warning BackOff 61s (x3 over 76s) kubelet, docker-desktop Back-off restarting failed container Warning BackOff 60s (x4 over 76s) kubelet, docker-desktop Back-off restarting failed container
– Giờ mở shell vào con container ‘myvolumes-cuongquach-1‘ , rồi tạo file trong thư mục ‘/demo1‘.
# kubectl exec cuongquach-pod -c myvolumes-cuongquach-1 -i -t -- /bin/sh # ls bin demo1 dev etc home lib media mnt opt proc root run sbin srv sys tmp usr var # echo "this message from cuongquach-container 1. " >> /demo1/testfile.txt # cat /demo1/testfile.txt this message from cuongquach-container 1. # exit
– Giờ mở shell vào con container ‘myvolumes-cuongquach-2‘ , rồi đọc file ‘testfile.txt‘ trong thư mục ‘/demo2‘ .
# kubectl exec cuongquach-pod -c myvolumes-cuongquach-2 -i -t -- /bin/sh # ls bin demo2 dev etc home lib media mnt opt proc root run sbin srv sys tmp usr var # cat /demo2/testfile.txt this message from cuongquach-container 1. # exit
Yup vậy là bạn đã hiểu cách sử dụng vùng lưu trữ volume loại emptyDir trong một Pod trên Kubernetes rồi phải không nào. Đây là volume type cơ bản và dễ sử dụng tuỳ theo nhu cầu của ứng dụng.
Nguồn: https://cuongquach.com/