Bài 12: Tối Ưu Pod Với Resource Limits Và Requests

Sau khi đã học cách debug Pods với kubectl describeexec ở Bài 11, chúng ta sẽ bước vào domain Pod Design (chiếm 18% trọng số trong kỳ thi CKAD). Bài này tập trung vào việc tối ưu Pods bằng cách thiết lập resource limits và requests hai thông số quan trọng để quản lý CPU và memory, đảm bảo hiệu suất, tránh lãng phí tài nguyên, và ngăn chặn các vấn đề như OOMKilled hoặc node overload.

Bài viết này thuộc domain Pod Design, CKAD yêu cầu bạn thành thạo việc cấu hình resources để ảnh hưởng đến scheduling và Quality of Service (QoS). Trong kỳ thi, bạn có thể gặp task như viết YAML để set requests/limits cho container, xác định QoS class của Pod, hoặc debug Pod không schedule do thiếu resources. Bây giờ chúng ta vào xem resource requests và limits là gì, sự khác biệt, cách chúng ảnh hưởng đến QoS, và cách áp dụng để tối ưu.

Resource Requests Và Limits Là Gì?

Trong Kubernetes, mỗi container trong Pod có thể khai báo requests (yêu cầu tối thiểu) và limits (giới hạn tối đa) cho CPU và memory. Những giá trị này giúp scheduler quyết định node nào để chạy Pod và kubelet quản lý runtime.

  • Requests: Lượng tài nguyên tối thiểu mà container yêu cầu để chạy. Scheduler sử dụng để chọn node có đủ allocatable resources (tổng node capacity trừ system/kube reserved). Nếu không đủ, Pod Pending. Requests cũng quyết định QoS class và priority khi evict.
  • Limits: Giới hạn tối đa container có thể sử dụng. Nếu vượt (memory), container OOMKilled; nếu CPU, throttled (giảm tốc độ). Limits ngăn container “starve” các Pod khác trên node.

Đơn vị:

  • CPU: 1 = 1 vCPU/core; 100m = 0.1 CPU.
  • Memory: Bytes (e.g., 256Mi = 256 mebibytes, 1Gi = 1 gibibyte).

Nếu không set, default từ LimitRange (nếu có) hoặc unlimited (rủi ro cao).

Lợi ích tối ưu:

  • Scheduling hiệu quả: Scheduler fit Pods vào nodes phù hợp, tránh overcommit.
  • Prevent failures: Limits tránh OOM, requests đảm bảo minimum.
  • QoS management: Phân loại Pods để evict low-priority trước khi node stress.
  • Cost optimization: Tránh lãng phí cloud resources.

Dưới đây là sơ đồ minh họa resource requests và limits trong Kubernetes, cho thấy cách chúng ảnh hưởng đến allocation và usage.

0da95f83-2fd0-4582-8495-228ddb0d38b0

Quality of Service (QoS) Classes Và Cách Tính

Dựa trên requests/limits, Kubernetes gán QoS class cho Pod (tổng từ tất cả containers):

  • Guaranteed: Requests = Limits (cho tất cả resources). Ưu tiên cao nhất, không evict trừ critical.
  • Burstable: Requests Burstable > Guaranteed.

Ví dụ: Pod với CPU request 100m, limit 200m; memory request=limit=256Mi => Burstable (vì CPU burstable).

Dưới đây là diagram QoS classes, minh họa cách phân loại dựa trên requests/limits.

https://miro.medium.com/v2/resize:fit:1400/1*tVavgNowToDsGxyglKo-KQ.png

Bảng So Sánh QoS Classes

QoS Class Điều Kiện Ưu Tiên Evict Use Case
Guaranteed Requests = Limits cho tất cả resources Cao nhất Critical apps cần guaranteed resources
Burstable Requests < Limits cho ít nhất một resource Trung bình Apps burstable như web servers
BestEffort Không set requests/limits Thấp nhất Non-critical, test apps

Cấu Hình Resource Limits Và Requests Trong YAML

Trong spec.containers[].resources (hoặc initContainers).

YAML ví dụ:

apiVersion: v1
kind: Pod
metadata:
  name: resource-pod
spec:
  containers:
    - name: app
      image: nginx
      resources:
        requests:
          cpu: "100m"     # 0.1 CPU
          memory: "128Mi"
        limits:
          cpu: "200m"
          memory: "256Mi"

Multi-container: Tổng requests/limits cho Pod là sum của tất cả.

Áp dụng: kubectl apply -f pod.yaml.

Để xem: kubectl describe pod resource-pod (xem Resources section).

Dưới đây là diagram OOMKilled debug, minh họa cách limits dẫn đến termination nếu vượt memory.

https://media.licdn.com/dms/image/v2/D5612AQHE2ObtjTAz7A/article-cover_image-shrink_720_1280/article-cover_image-shrink_720_1280/0/1719982339960?e=2147483647&v=beta&t=C-HSg4QZq62ddJyz9nJWPmBudHc64r9wKMRGWgF81PM

Các Lệnh Kubectl Để Quản Lý Resources

  • Xem resources Pod: kubectl describe pod (Resources field).
  • Top usage: kubectl top pod (yêu cầu metrics-server).
  • Edit resources: kubectl edit pod (sửa YAML, nhưng Pod immutable nên thường recreate).
  • Get QoS: kubectl get pod -o jsonpath='{.status.qosClass}'.
  • Debug OOM: kubectl describe pod (xem Events: OOMKilled).

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

Domain Pod Design (18%) thường có task về resources, như:

  • Set requests/limits trong YAML để đạt QoS Guaranteed.
  • Tối ưu Pod burstable cho app.
  • Debug Pod evict do overcommit.

Mẹo thi:

  • Nhớ đơn vị: m cho milliCPU, Mi/Gi cho memory.
  • QoS tính trên toàn Pod, không per container.
  • Thời gian: 5-7 phút/task, verify bằng describe.
  • Luyện: kubectl explain pod.spec.containers.resources.
  • Kết hợp LimitRange/ResourceQuota nếu đề cập.

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

  1. Tạo Pod với resources: Tạo file resource-pod.yaml như ví dụ, apply: kubectl apply -f resource-pod.yaml.
  2. Kiểm tra:
    kubectl describe pod resource-pod  # Xem Resources, QoS (Burstable)
    kubectl get pod resource-pod -o jsonpath='{.status.qosClass}'
  3. Test OOM: Set low memory limit, run app consume nhiều (e.g., stress tool).
    kubectl run oom-pod --image=polinux/stress --requests=memory=100Mi --limits=memory=200Mi -- stress --vm 1 --vm-bytes 300M
    kubectl describe pod oom-pod  # Thấy OOMKilled
  4. Guaranteed QoS: Set requests=limits.
  5. Cleanup: kubectl delete pod resource-pod oom-pod.

Nếu Pod không schedule, check node resources bằng kubectl describe node.

Kết Luận

Bài này mình đã hướng dẫn chi tiết tối ưu Pod với resource limits và requests, từ khái niệm đến QoS. Bạn giờ có thể quản lý resources hiệu quả, sẵn sàng cho domain Pod Design trong CKAD. Tiếp theo, ở Bài 13: Sử Dụng Liveness Và Readiness Probes, chúng ta sẽ khám phá probes để đảm bảo Pod healthy.

Điều hướng chuỗi bài viết<< Bài 11: Debug Pod Với kubectl describe Và exec>> Bài 13: Sử Dụng Liveness Và Readiness Probes
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