Part 12 · Kubernetes · 12.1.01

Control plane và reconciliation loops

Kubernetes vận hành bằng mô hình desired state: client ghi trạng thái mong muốn qua API server, các controller độc lập quan sát trạng thái hiện tại và lặp lại hành động cho tới khi hệ thống tiến gần về trạng thái đó.

Mental model: một API request được chấp nhận chỉ có nghĩa object đã đi qua control path và có thể đã được persist; nó không đồng nghĩa Pod đã chạy, Deployment đã Available hay traffic đã sẵn sàng.

1. Control Plane

Control plane là tập các thành phần quản lý trạng thái toàn cluster. kube-apiserver là HTTP API front door; các client và component trao đổi với cluster thông qua API này. etcd lưu API data nhất quán. kube-scheduler tìm Pod chưa được bind và chọn node phù hợp. kube-controller-manager chạy các controller hiện thực hành vi của Kubernetes API. cloud-controller-manager là thành phần tùy chọn để tích hợp cloud provider.

Trên worker node, kubelet theo dõi Pod được giao cho node và phối hợp container runtime, volumes, networking và probes. Container runtime thực thi containers thông qua CRI-compatible integration. Vì vậy, kubelet/runtime không thuộc control plane nhưng là phần bắt buộc của vòng thực thi desired state trên node.

2. Components và trách nhiệm

ComponentVai trò chínhĐiểm vận hành cần nhớ
kube-apiserverExpose Kubernetes API; xử lý authentication, authorization, admission và API persistence path.Thường scale theo nhiều replica sau load balancer; cần bảo vệ khỏi overload và request storm.
etcdConsistent key-value store cho API server data.Quorum cần majority; snapshot/restore là phần của disaster recovery.
kube-schedulerChọn node cho Pod chưa được bind.Trong HA có thể chạy nhiều instance nhưng chỉ leader thực hiện scheduling chính.
kube-controller-managerChạy nhiều reconciliation controllers.Controller phải chịu được retry, duplicate observation và state thay đổi trong lúc xử lý.
cloud-controller-managerKết nối Kubernetes với cloud provider.Chỉ cần khi cluster dùng integration tương ứng.
kubelet + runtimeBiến PodSpec đã bind thành workload thực tế trên node.Node/network/runtime failure có thể làm status trễ hoặc Pod chưa ready dù object vẫn tồn tại.

3. Create Deployment call flow

  1. Client gửi Deployment object tới kube-apiserver.
  2. Request đi qua authentication, authorization, admission, validation/defaulting theo API path được cấu hình.
  3. Object được persist vào cluster state; watchers/informers có thể quan sát thay đổi.
  4. Deployment controller reconcile và tạo/cập nhật ReplicaSet.
  5. ReplicaSet controller reconcile và tạo/cập nhật Pods để đạt replica count mong muốn.
  6. Scheduler quan sát Pod chưa bind, chọn node và ghi binding.
  7. Kubelet trên node quan sát Pod đã được giao, phối hợp runtime, mount, network và probes rồi cập nhật status.
  8. Các controller liên quan Service/EndpointSlice cập nhật tập backend đủ điều kiện để nhận traffic dựa trên object và readiness.
kubectl / client
      |
      v
kube-apiserver ---> etcd
      |
      +--> Deployment controller --> ReplicaSet --> Pods
      |
      +--> Scheduler ---------------> Pod -> Node binding
                                      |
                                      v
                                   kubelet
                                      |
                                      v
                             runtime / network / volumes
Failure window: request có thể đã persist nhưng controller chưa xử lý, Pod chưa được schedule, image chưa pull xong, probe chưa pass hoặc EndpointSlice chưa đưa Pod vào traffic. Khi debug cần xác định đang kẹt ở hop nào thay vì chỉ nhìn kết quả của lệnh create/apply.

4. Reconciliation semantics

Controller nên được hiểu là level-triggered reconciliation: controller so sánh observed state với desired state rồi thực hiện hành động cần thiết. Thiết kế tốt phải idempotent, chấp nhận retry và không dựa vào giả định event chỉ tới đúng một lần hay luôn giữ thứ tự tuyệt đối.

Do đó, automation nên chờ condition rõ ràng như Available, readiness hoặc rollout completion thay vì coi HTTP success từ API server là completion của workload.

5. HA, leader election và failure model

API server có thể chạy nhiều replica và được đặt sau load balancer. Với controller manager và scheduler, nhiều replica thường phối hợp bằng leader election để chỉ một active leader thực hiện một nhóm control duties tại một thời điểm. Kubernetes dùng Lease objects cho node heartbeats và cho các cơ chế leader election.

etcd phụ thuộc quorum: cluster N members chỉ chịu được tối đa (N-1)/2 permanent member failures mà vẫn giữ majority. Khi mất quorum, cluster không thể tiếp tục consensus cho update; recovery phải dựa vào quy trình snapshot/restore hoặc member recovery phù hợp.

FailureĐiều vẫn có thể tiếp tụcĐiều bị ảnh hưởng
API server/control plane tạm unavailableExisting containers trên node có thể tiếp tục chạy.Create/update/delete, scheduling mới, nhiều controller actions và cluster recovery bị trì hoãn.
Scheduler leader mấtPod đang chạy vẫn chạy.Pod chưa bind chờ leader mới / scheduling resume.
Controller manager leader mấtData plane hiện tại có thể tiếp tục.Reconciliation tạm dừng cho tới khi failover.
Node heartbeat/Lease mấtKhông chứng minh hardware đã chết ngay lập tức.Control plane phải dùng timeout và các signal khác để suy luận node availability.
etcd mất quorumMột số workload hiện hữu có thể tiếp tục ở data plane.Control plane không thể commit state updates bình thường.

6. Operational signals và capacity

Production troubleshooting cần nhìn cả latency và backlog của control loop. Các signal hữu ích gồm API request rate/latency/errors, API Priority and Fairness queue/reject behavior, etcd request/disk latency và quorum health, scheduler pending Pods, controller workqueue depth/retries, node Lease freshness, Pod scheduling latency, rollout duration và EndpointSlice/readiness propagation delay.

Capacity planning không chỉ là CPU/RAM trung bình. Hãy test burst create/update, watch fan-out, controller resync/retry, node churn và rollout lớn. Giữ headroom để control plane vẫn tiến triển khi một instance hoặc failure domain bị mất, thay vì chỉ đủ capacity cho steady state.

7. Security boundaries

API access là security boundary trung tâm: authentication xác định caller; authorization quyết định hành động được phép; admission có thể mutate hoặc reject object trước khi persistence. Dùng least-privilege RBAC, bảo vệ kubeconfig/service-account credentials, hạn chế quyền truy cập trực tiếp etcd, mã hóa traffic và kiểm soát admission/webhook dependencies theo threat model.

Admission webhook nằm trên write path có thể trở thành availability dependency. Khi thiết kế policy cần hiểu timeout/failure policy, tránh webhook vòng lặp hoặc phụ thuộc downstream không ổn định, và có break-glass procedure được audit.

8. Rollback và recovery

Checklist khi debug: API request đã accepted chưa → object đã persist chưa → controller nào chịu trách nhiệm → controller có reconcile/retry không → Pod đã schedule chưa → kubelet/runtime/network/volume có thành công không → readiness đã pass chưa → backend đã được đưa vào traffic chưa.
Tài liệu: Kubernetes Components · Controllers · Kubernetes API · Controlling Access · API Priority and Fairness · Leases · etcd Disaster Recovery