Stage 3, Part 1 of 10 · p. 19, Control Plane Internals

PART 1: CORE THEORY

1. The Control Plane

The control plane is the brain of Kubernetes. It makes all the decisions: what runs where, how many replicas, what happens when something fails. It consists of four components that work together.

The Four Components

text
┌─────────────────────────────────────────────────────────────┐
│                    CONTROL PLANE COMPONENTS                 │
│                                                             │
│  ┌───────────────┐     ┌───────────────┐                  │
│  │  kube-        │     │  etcd         │                  │
│  │  apiserver    │◄───►│  (State DB)   │                  │
│  │  (REST API)   │     │  Raft quorum  │                  │
│  └───────┬───────┘     └───────────────┘                  │
│          │                                                  │
│          │                                                  │
│  ┌───────┴───────┐     ┌───────────────┐                  │
│  │  kube-        │     │  kube-        │                  │
│  │  scheduler    │     │  controller-  │                  │
│  │  (Filter →    │     │  manager      │                  │
│  │   Score →     │     │  (Reconcile)  │                  │
│  │   Bind)       │     │               │                  │
│  └───────────────┘     └───────────────┘                  │
└─────────────────────────────────────────────────────────────┘

Component Responsibilities

ComponentRoleStateful?
kube-apiserverREST API, the only component that talks to etcdStateless
etcdKey-value store, the single source of truthStateful
kube-schedulerAssigns Pods to NodesStateless
kube-controller-managerRuns reconciliation loopsStateless

The Key Insight

Only the API server talks to etcd. Everything else talks to the API server. This is the fundamental design pattern.


2. The API Server

kube-apiserver is the front door to Kubernetes. Every kubectl command, every controller action, every kubelet heartbeat goes through it.

The Request Flow (Step by Step)

When a request hits the API server:

text
Request → Authentication → Authorization → Admission → etcd
                │                │              │
                ▼                ▼              ▼
            Who are you?   Are you allowed?   Should we
                                              modify it?

Step 1: Authentication (AuthN)

The API server verifies identity using:

Step 2: Authorization (AuthZ)

The API server checks permissions using:

Kubernetes authorization takes place after authentication. Usually, a client making a request must be authenticated before its request can be allowed; however, Kubernetes also allows anonymous requests in some circumstances.

Step 3: Admission Control

Admission controllers intercept requests after authentication and authorization but before persistence to etcd.

Admission control proceeds in two phases:

  1. Mutating admission controllers run first. They can modify the object.
  2. Validating admission controllers run second. They can only allow or deny.

A mutating webhook can change the object before it's saved, while a validating admission webhook can only allow or deny it. This ordering matters because mutations always run first, so validation sees the final version of the object.

Step 4: Persistence

If the request passes all checks, the API server writes it to etcd.

API Server Aggregation

The aggregation layer allows Kubernetes to be extended with additional APIs beyond what the core Kubernetes APIs offer. It runs in-process with the kube-apiserver.

This is how metrics-server, custom metrics, and other extension APIs work.


3. etcd

etcd is a distributed, reliable key-value store that serves as the single source of truth for all cluster state. If etcd is lost, the cluster is lost.

Why etcd Matters

FactDetail
All cluster state lives in etcdPods, Services, ConfigMaps, Secrets, RBAC, everything
etcd uses Raft consensusGuarantees consistency across nodes
etcd must have quorumMajority of nodes must agree
etcd is the performance bottleneckSlow etcd = slow cluster

Raft Consensus and Quorum

etcd uses the Raft consensus algorithm. The core requirement is quorum: a majority of nodes must agree for a transaction to be committed.

The quorum formula:

text
Quorum = floor(N/2) + 1

3 nodes: quorum = 2, tolerates 1 failure
5 nodes: quorum = 3, tolerates 2 failures
7 nodes: quorum = 4, tolerates 3 failures

Why odd numbers? Even numbers don't improve fault tolerance. A 4-node cluster needs 3 nodes to confirm, but a 5-node cluster also needs only 3 nodes — yet the 5-node cluster can tolerate 2 failures versus 1 for the 4-node cluster.

Production recommendation: Use 3 nodes for most clusters, 5 for large-scale or high-availability-critical clusters. Never exceed 7 nodes — the performance overhead outweighs the fault tolerance benefit.

etcd Performance Requirements

RequirementRecommendation
DiskSSD or NVMe with low fsync latency
Disk latency< 10ms for fsync
NetworkLow latency between etcd nodes (< 10ms)
MemoryDefault 8 GB limit; monitor for quota issues
CPU2-4 dedicated cores

The disk I/O warning: etcd's Raft messages (including heartbeats) must be persisted to the WAL log using fdatasync. If the underlying disk is a mechanical HDD, fsync latency can exceed 10ms, causing heartbeats to be delayed and triggering false leader elections.

etcd Heartbeat and Election Tuning

ParameterDefaultRecommended (Cross-AZ)
heartbeat-interval100ms200ms
election-timeout1000ms1500ms+

The formula: election-timeout ≥ 5 × heartbeat-interval + max_network_rtt.


4. The Scheduler

kube-scheduler watches for unscheduled Pods (spec.nodeName is empty) and assigns each to a feasible Node.

The Filter → Score → Bind Pipeline

For every Pending Pod, the scheduler runs:

text
All Nodes
    │
    ▼
PHASE 1: FILTERING
  Drop nodes that can't fit the Pod
  (resources, affinity, taints, ports)
    │
    ▼
Feasible Nodes
    │
    ▼
PHASE 2: SCORING
  Score each feasible node (0-100)
  (load balancing, spreading, locality)
    │
    ▼
Highest-scoring node
    │
    ▼
PHASE 3: BINDING
  Write spec.nodeName onto the Pod
  (via API server)

The scheduler only chooses and writes the node name onto the Pod. It doesn't start the container. Starting the container is the job of the kubelet on the chosen node.

The Scheduler Framework (Plugin Engine)

The scheduler is a plugin engine with ten extension points. Plugins register at different stages of the scheduling cycle.

Extension PointPurpose
QueueSortOrder Pods in the scheduling queue
PreFilterPre-process Pod info
FilterEliminate unsuitable nodes
PostFilterHandle unschedulable Pods (preemption)
PreScorePre-process scoring
ScoreRank feasible nodes
ReserveReserve resources
PermitAllow/deny/wait
PreBindPre-binding setup
BindFinalize placement

The Default Scoring Strategy

The scheduler's default scoring strategy is LeastAllocated — it spreads workloads rather than packing them. This is intentional: it prioritizes failure-domain resilience over density.

Bin packing is opt-in via MostAllocated or RequestedToCapacityRatio in NodeResourcesFit.

The Critical Misunderstanding

The scheduler operates on requests, not actual usage. A Pod requesting 2 CPUs but using 200m looks like a 2-CPU Pod to every scoring and consolidation decision.

This is why right-sizing requests is critical for cluster efficiency.


5. The Controller Manager

kube-controller-manager is a single binary that bundles dozens of reconciliation loops.

The Controller Pattern

A controller is a continuous loop that:

  1. Observes the current state
  2. Compares it to the desired state
  3. Acts to reconcile the difference
text
Desired State (from API server)
    │
    ▼
┌─────────────────────────────────────────────────────────────┐
│  Controller Loop                                            │
│  ┌─────────────┐    ┌─────────────┐    ┌─────────────┐    │
│  │  Observe    │───►│  Compare    │───►│    Act      │    │
│  │  Current    │    │  Desired vs │    │  (create,   │    │
│  │  State      │    │  Current    │    │   update,   │    │
│  └─────────────┘    └─────────────┘    │   delete)   │    │
│         ▲                               └──────┬──────┘    │
│         └──────────────────────────────────────┘           │
└─────────────────────────────────────────────────────────────┘

What Controllers Run in kube-controller-manager

ControllerWhat It Reconciles
DeploymentReplicaSets for Deployments
ReplicaSetPods for ReplicaSets
NodeNode lifecycle, evictions
JobPods for Jobs
CronJobJobs on schedule
ServiceAccountSA tokens
EndpointSliceService endpoints
NamespaceNamespace lifecycle
PersistentVolumePV/PVC binding

Special Roles

The controller-manager also:


1:1 copy of the class Google Doc, code in code blocks.