Bài 7: Thiết Kế Pod Với Nhiều Container

Sau khi đã học cách cấu hình Deployment và ReplicaSet trong Bài 6, chúng ta sẽ bước vào domain Multi-Container Pods (chiếm 18% trọng số trong kỳ thi CKAD). Bài này tập trung vào việc thiết kế Pods với nhiều containers, bao gồm các patterns như sidecar, ambassador, adapter, và đặc biệt là init containers. Đây là kỹ năng quan trọng để xây dựng ứng dụng phức tạp hơn, nơi các containers chia sẻ tài nguyên và hỗ trợ lẫn nhau mà không cần tách thành nhiều Pods riêng lẻ.

Domain Multi-Container Pods trong CKAD yêu cầu bạn hiểu cách các containers tương tác trong cùng Pod, chia sẻ network/storage, và xử lý lifecycle khác nhau. Trong kỳ thi, bạn có thể gặp task kiểu như tạo Pod với init container để chuẩn bị data trước khi app container chạy, hoặc thêm sidecar để logging/monitoring.

Lý Do Thiết Kế Pod Với Nhiều Container

Mặc dù Pods thường có một container chính (app container), Kubernetes hỗ trợ nhiều containers trong cùng Pod để giải quyết các vấn đề phức tạp. Các containers chia sẻ:

  • Network namespace: Cùng IP và ports, giao tiếp qua localhost.
  • Storage volumes: Mount chung để chia sẻ files.
  • IPC namespace: Chia sẻ process signals nếu cần.
  • Lifecycle: Pod ready khi tất cả containers ready, nhưng init containers chạy trước.

Lợi ích:

  • Tight coupling: Containers hỗ trợ lẫn nhau (ví dụ: logging sidecar thu thập logs từ app).
  • Atomicity: Đơn vị deploy nhỏ nhất, đảm bảo tất cả containers chạy cùng node.
  • Efficiency: Giảm overhead so với nhiều Pods riêng (mỗi Pod có overhead network/IP).
  • Patterns: Sidecar (enhance), Adapter (transform output), Ambassador (proxy external).

Không nên dùng multi-container nếu containers không phụ thuộc chặt chẽ, vì khó scale độc lập. Trong CKAD, tập trung vào việc viết YAML đúng để tránh lỗi như containers không chia sẻ volume.

Dưới đây là sơ đồ kiến trúc multi-container Pod minh họa cách các containers chia sẻ tài nguyên trong Pod.

https://matthewpalmer.net/kubernetes-app-developer/multi-container-pod-design.png

Các Design Patterns Phổ Biến Trong Multi-Container Pods

Kubernetes khuyến nghị ba patterns chính: Sidecar, Ambassador, và Adapter. Ngoài ra, Init Containers là loại đặc biệt chạy trước app containers.

1. Init Containers

Init containers chạy sequentially trước app containers, dùng để setup môi trường (ví dụ: clone repo, check database ready, generate config). Chúng chạy đến hoàn thành (exit 0), nếu fail thì retry theo restartPolicy (default Always cho init).

Đặc điểm:

  • Chạy trước, không chạy parallel với app.
  • Không support readiness/liveness probes (vì one-time).
  • Chia sẻ volumes với app containers.
  • Dùng cho tasks không thuộc app chính, tránh bloat image.

Ví dụ: Init container clone Git repo vào volume, rồi app container dùng code đó.

Dưới đây là diagram lifecycle init containers của devopscube cũng đã thể hiện khá rõ ràng cho thấy thứ tự chạy và chuyển sang app phase.

https://devopscube.com/content/images/2025/03/init-container-2.gif

2. Sidecar Pattern

Sidecar là container phụ chạy parallel với app, enhance functionality (ví dụ: logging, monitoring, encryption). Sidecar không thay thế app mà hỗ trợ nó.

Ví dụ: App container write logs vào file, sidecar đọc và ship đến ELK stack.

3. Ambassador Pattern

Ambassador là proxy cho app container kết nối external services (ví dụ: database sharding, A/B testing). App connect localhost, ambassador handle logic.

Ví dụ: App connect DB qua ambassador, nó route đến prod/dev dựa trên env.

4. Adapter Pattern

Adapter transform output của app để match chuẩn (ví dụ: convert logs sang JSON cho monitoring tool).

Dưới đây là ví dụ patterns sidecar, adapter, ambassador trong multi-container Pod.

https://matthewpalmer.net/kubernetes-app-developer/multi-container-pod-design.png

Bảng Tóm Tắt Các Patterns

Pattern Mô tả Ví dụ Ưu điểm
Init Container Chạy trước app để setup Clone repo, wait for service ready Đảm bảo precondition, không block app
Sidecar Hỗ trợ app parallel Logging agent, file sync Enhance mà không modify app code
Ambassador Proxy external connections DB proxy, service mesh sidecar Abstract networking complexity
Adapter Transform app output Metrics exporter, log formatter Standardize output cho integration

Thiết Kế Multi-Container Pod Trong YAML

Trong Pod spec, định nghĩa mảng containers cho app/sidecar, và initContainers riêng.

YAML Cơ Bản Với Init Và Sidecar

apiVersion: v1
kind: Pod
metadata:
  name: multi-pod
spec:
  initContainers:
  - name: init-setup
    image: busybox
    command: ['sh', '-c', 'echo "Setup done" > /work-dir/setup.txt']
    volumeMounts:
    - name: shared-vol
      mountPath: /work-dir
  containers:
  - name: app-container
    image: nginx
    ports:
    - containerPort: 80
    volumeMounts:
    - name: shared-vol
      mountPath: /usr/share/nginx/html
  - name: sidecar-logger
    image: busybox
    command: ['sh', '-c', 'tail -f /var/log/nginx/access.log']
    volumeMounts:
    - name: log-vol
      mountPath: /var/log/nginx
  volumes:
  - name: shared-vol
    emptyDir: {}
  - name: log-vol
    emptyDir: {}

Lưu ý: Init chạy trước, containers chạy parallel. Sử dụng volumes để share data.

Các Lệnh Kubectl Để Quản Lý Multi-Container Pods

  • Tạo Pod: kubectl apply -f multi-pod.yaml.
  • Get Pods: kubectl get pods (xem status multi-containers).
  • Describe: kubectl describe pod multi-pod (xem events mỗi container, init status).
  • Logs: kubectl logs multi-pod -c sidecar-logger (chỉ định container).
  • Exec: kubectl exec -it multi-pod -c app-container -- /bin/sh.
  • Debug Init Fail: Nếu init crash, Pod ở Init phase, dùng describe xem logs init.

Liên Hệ Với Chứng Chỉ CKAD

Domain Multi-Container Pods (18%) yêu cầu thiết kế Pods phức tạp, chiếm tỷ lệ lớn. Task điển hình:

  • Tạo Pod với init container mount volume và setup file cho app.
  • Thêm sidecar chia sẻ volume để process data từ app.
  • Debug multi-container bằng logs/describe mỗi container.

Mẹo thi:

  • Nhớ chỉ định -c cho logs/exec, vì default lấy container đầu.
  • Init containers ở spec.initContainers, không phải containers.
  • Thời gian: 6-8 phút/task, verify bằng exec cat file trong volume.
  • Luyện: Sử dụng kubectl explain pod.spec.initContainers cho docs.
  • Tránh overkill: Chỉ dùng multi nếu cần share localhost/volume.

Thực Hành Thực Tế Trên Minikube

  1. Tạo YAML: Sao chép YAML trên vào multi-pod.yaml.
  2. Áp dụng:
    kubectl apply -f multi-pod.yaml
    kubectl get pods  # Chờ Running (sau Init:0/1 -> Running)
  3. Kiểm tra Init:
    kubectl describe pod multi-pod  # Xem events init complete
  4. Logs và Exec:
    kubectl logs multi-pod -c init-setup  # Logs init (nếu fail)
    kubectl exec -it multi-pod -c app-container -- ls /usr/share/nginx/html  # Thấy setup.txt
    kubectl logs multi-pod -c sidecar-logger  # Xem logs
  5. Cleanup: kubectl delete pod multi-pod.

Nếu init fail, Pod stuck ở Init, edit YAML sửa command.

Kết Luận

Bài này mình đã hướng dẫn thiết kế Pod với nhiều containers, từ patterns đến YAML thực tế. Bạn giờ có thể build Pods phức tạp, sẵn sàng cho domain Multi-Container Pods trong CKAD. Tiếp theo, ở Bài 8: Sử Dụng Sidecar Và Init Container, chúng ta sẽ đi sâu hơn vào implementation cụ thể.

Điều hướng chuỗi bài viết<< Bài 6: Cấu Hình Deployment Và Replica Set>> Bài 8: Sử Dụng Sidecar Và Init Container

Thông tin nổi bật

Báo cáo quan trọng

Sự kiện đang hiện hành

Chia sẻ bài viết:
Theo dõi
Thông báo của
0 Góp ý
Được bỏ phiếu nhiều nhất
Mới nhất Cũ nhất
Phản hồi nội tuyến
Xem tất cả bình luận