Logo

Personal Ops Runbook

Personal runbook covering infrastructure operations for Cloud, Kubernetes, OpenStack, and Ceph environments. Includes deployment and teardown procedures, node management, cluster monitoring setup, and incident response workflows compiled from day-to-day operational work. Intended strictly for personal reference — configurations and scripts are environment-specific and not guaranteed to work as-is elsewhere.

[Kubernetes 4] Ổ đĩa và lưu trữ trong Pod


Ở bài viết này mình sử dụng cách lưu trữ đơn giản là sử dụng thư mục trên node master và mount vào Pod để sử dụng nhé, để đi vào sâu chúng ta sẽ tìm hiểu ở các bài sau.

Mình sẽ sử dụng Nginx để demo chạy 1 website đơn giản như dưới, tạo thư mục lưu code /home/mywebsite và tạo file /home/mywebsite/index.html để lưu nội dung website.

root@k8s-standalone:/home# mkdir -p /home/mywebsite
root@k8s-standalone:/home# cat > /home/mywebsite/index.html << OEF
Chào mừng đến với website của HoangHD
OEF

Tiếp theo mình sẽ tạo 1 file yaml như dưới, ở phần này chúng ta để ý các trường như sau:

  • spec.volumes[0].hostPath.path: gán thư mục cần mount vào Pod trên máy host
  • spec.volumes[0].name: tên của ổ đĩa
  • spec.containers[0].volumeMounts.0.mountPath: thư mục cần mount trên Pod
  • spec.containers[0].volumeMounts.0.name: tên của ổ đĩa đã được tạo ở phần spec.volumes[0].name
cat > ./nginx.yaml << OEF
apiVersion: v1
kind: Pod
metadata:
  name: nginx
  labels:
    label: nginx
spec:
  volumes:
    - name: "mywebsite"
      hostPath:
          path: "/home/mywebsite" #Thư mục trên node master
  containers:
  - name: n1
    image: nginx
    resources:
      limits:
        memory: "128Mi"
        cpu: "100m"
    ports:
      - containerPort: 80
    volumeMounts:
      - mountPath: /var/www/html
        name: "mywebsite"
OEF

Chúng ta triển khai file yaml trên sau đó vào Pod Nginx rồi vào thư mục /var/www/html/ để kiểm tra file index.html đã tồn tại chưa, nếu nó tồn tại thì chúng ta đã mount thành công

root@k8s-standalone:/home# kubectl exec -it nginx bash
root@nginx:/# cat /var/www/html/index.html
Chào mừng đến với website của HoangHD

Ở bài sau mình sẽ hướng dẫn các bạn làm thế nào để truy cập được website trên. Chúc các bạn thành công.