PART 2: ADVANCED CONCEPTS
6. Admission Webhooks
Mutating vs Validating
| Aspect | Mutating | Validating |
| Purpose | Modify the object | Allow or deny |
| When | First phase | Second phase |
| Can modify? | ✅ Yes | ❌ No |
| Can deny? | ✅ Yes | ✅ Yes |
The Ordering
- Mutating webhooks run first.
- Validating admission policies run.
- Validating webhooks run last.
Critical: Mutations by mutating webhooks cause all previously called webhooks to be called again.
Failure Policy
| Policy | Behavior |
| Fail | Reject the request if the webhook is unreachable |
| Ignore | Allow the request if the webhook is unreachable |
The default is Fail (unless overridden).
The recovery pattern: If a webhook is blocking cluster recovery, temporarily patch to Ignore:
bash
kubectl patch validatingwebhookconfiguration my-webhook --type='json' \
-p='[{"op":"replace","path":"/webhooks/0/failurePolicy","value":"Ignore"}]'
⚠️ Warning: This disables enforcement. Use only during confirmed outages.
Webhook Timeout
Admission webhooks created using admissionregistration.k8s.io/v1 default to 30-second timeout.
The Four Extension Points
| Controller | Purpose |
| MutatingAdmissionWebhook | External mutating webhooks |
| MutatingAdmissionPolicy | Declarative in-API mutations |
| ValidatingAdmissionWebhook | External validating webhooks |
| ValidatingAdmissionPolicy | Declarative in-API validation |
ValidatingAdmissionPolicy provides a way to embed declarative validation code within the API without relying on external HTTP callouts.
7. API Priority and Fairness (APF)
APF controls API server behavior under overload. It's stable since Kubernetes v1.29.
The Problem APF Solves
The legacy controls (--max-requests-inflight, --max-mutating-requests-inflight) limit outstanding work but don't ensure important requests get through.
How APF Works
| Concept | Purpose |
| FlowSchema | Categorize incoming requests |
| PriorityLevelConfiguration | Assign priority levels |
| Queue | Buffer requests during bursts |
| Fair Queuing | Prevent starvation |
APF classifies and isolates requests in a fine-grained way. It introduces limited queuing so no requests are rejected during brief bursts. Requests are dispatched using fair queuing so a poorly-behaved controller doesn't starve others.
The Priority Levels
APF has built-in priority levels:
| Level | Purpose |
| system | Control plane components |
| workload-high | Critical workloads |
| workload-low | Standard workloads |
| global-default | Everything else |
| catch-all | Fallback |
Enabling/Disabling
APF is enabled by default. Disable with:
text
kube-apiserver --enable-priority-and-fairness=false
The Long-Running Exception
Some requests classified as "long-running" — such as remote command execution or log tailing — are not subject to APF. This is also true for --max-requests-inflight without APF.
8. The Aggregation Layer
The aggregation layer allows Kubernetes to be extended with additional APIs.
How It Works
The aggregation layer runs in-process with the kube-apiserver. When a request comes in for a registered API group, the main API server proxies it to the extension API server.
What Uses It
| Extension | API Group |
| metrics-server | metrics.k8s.io |
| Custom Metrics | custom.metrics.k8s.io |
| Service Catalog | servicecatalog.k8s.io |
| KubeVirt | kubevirt.io |
When to Use Aggregation vs CRDs
| Approach | Use When |
| CRDs | You need custom resources with standard API behavior |
| Aggregation | You need custom API semantics, subresources, or protocols |
9. Leader Election
In an HA control plane, multiple instances of scheduler and controller-manager run, but only one is active.
How It Works
| Step | Action |
| 1 | All instances try to acquire a Lease object in kube-system |
| 2 | The winner becomes leader and does the work |
| 3 | The leader periodically renews the lease |
| 4 | If the leader dies, another instance acquires the lease |
Both controller-manager and scheduler run three instances (one per control plane node). Leader election ensures only one does the work at a time.
Lease Parameters
| Parameter | Default | Meaning |
| leaseDuration | 15s | How long the lease is valid |
| renewDeadline | 10s | Leader must renew before this |
| retryPeriod | 2s | How often followers retry |
The Failover Delay
If a leader dies, the new leader takes over after leaseDuration expires. Default: ~15 seconds. During this window, no reconciliation happens.
Graceful Leader Transition (Kubernetes 1.36+)
Previously, losing leadership meant os.Exit() and restart. Kubernetes 1.36 introduced graceful leader transition: on losing leadership, the component transitions to follower state and tries to become leader again, avoiding resource waste.
10. Version Skew Policy
Kubernetes components have version compatibility rules. Violating them causes subtle failures.
Supported Versions
The Kubernetes project maintains release branches for the most recent three minor releases. Example: 1.37, 1.36, 1.35.
Version Skew Rules
| Component | Rule |
| kube-apiserver (HA) | Newest and oldest instances within one minor version |
| kubelet | Must not be newer than apiserver; up to 3 minor versions older (2 for < 1.25) |
| kube-proxy | Must not be newer than apiserver; up to 3 minor versions older |
| kubectl | Within one minor version of apiserver (older or newer) |
Examples
| Scenario | Supported? |
| apiserver 1.37, kubelet 1.37 | ✅ |
| apiserver 1.37, kubelet 1.36 | ✅ |
| apiserver 1.37, kubelet 1.34 | ✅ |
| apiserver 1.37, kubelet 1.33 | ❌ |
| apiserver 1.37, kubelet 1.38 | ❌ (newer) |
The HA Narrowing Effect
If apiserver instances are at 1.37 and 1.36, kubelet is supported at 1.36, 1.35, and 1.34. Version 1.37 is not supported because that would be newer than the 1.36 apiserver instance.
11. Certificate Rotation
Control plane certificates expire. If they expire, the cluster breaks.
The Certificates
| Certificate | Default Validity |
| kube-apiserver serving cert | 1 year |
| kube-apiserver client cert | 1 year |
| etcd serving cert | 1 year |
| etcd peer cert | 1 year |
| kubelet client cert | 1 year |
| kubelet serving cert | 1 year |
| CA certificate | 10 years |
Rotation Methods
| Method | Description |
| kubeadm | kubeadm certs renew all |
| kucero | Automatic daemonset-based rotation |
| Manual | Regenerate and replace certificates |
| Cloud-managed | Managed by provider (EKS, GKE, AKS) |
Kucero is a Kubernetes daemonset that performs automatic control plane certificate rotation.
The Rotation Procedure
- Plan a maintenance window (control plane will be briefly unavailable)
- Back up etcd and certificates
- Run rotation command
- Restart control plane components
- Verify cluster health
Warning: Certificate rotation may cause temporary unavailability when components restart. For production, perform during a maintenance window.
12. etcd Compaction and Defragmentation
Why etcd Grows
etcd's database file grows over time even when you're not adding objects. Compaction removes old revisions logically, but the bbolt backend does not shrink the file on disk. Without defragmentation, the file grows indefinitely.
The Two Operations
| Operation | What It Does |
| Compaction | Removes old revisions from the logical keyspace |
| Defragmentation | Reclaims disk space by removing gaps from deleted keys |
Automatic Compaction
The Kubernetes API server passes --etcd-compaction-interval to trigger compaction periodically.
Manual Defragmentation
After compaction, defragment each etcd member:
bash
# Check fragmentation
etcdctl endpoint status --write-out=table
# Defragment one member at a time
etcdctl defrag --endpoints=https://etcd-1:2379
# Verify
etcdctl endpoint status --write-out=table
Warning: Defragmentation blocks the etcd member for the duration. Do one member at a time, never all at once.
Monitoring etcd Size
bash
# Database size
etcdctl endpoint status --write-out=table
# Alarm if DB size > 2 GB (default quota is 2 GB)
etcdctl alarm list
13. The Controller Pattern
The Informer Architecture
Controllers don't poll the API server. They use informers:
text
API Server
│
│ Watch
▼
┌─────────────────────────────────────────────────────────────┐
│ Informer │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │
│ │ Reflector │───►│ DeltaFIFO │───►│ Indexer │ │
│ │ (Watch) │ │ (Queue) │ │ (Cache) │ │
│ └─────────────┘ └─────────────┘ └─────────────┘ │
│ │ │
│ ▼ │
│ ┌─────────────┐ │
│ │ Workqueue │ │
│ │ (Rate- │ │
│ │ limited) │ │
│ └─────────────┘ │
└─────────────────────────────────────────────────────────────┘
The Four Components
| Component | Purpose |
| Reflector | Watches API server for changes |
| DeltaFIFO | Queue of changes |
| Indexer | Local cache for fast reads |
| Workqueue | Rate-limited work queue for the controller |
Why This Design
- No polling: Watch is event-driven
- Local cache: Reads don't hit the API server
- Rate limiting: Prevents API server overload
- Deduplication: Multiple changes to the same object collapse into one work item
14. Cloud Controller Manager
cloud-controller-manager integrates Kubernetes with cloud providers.
What It Does
| Controller | Cloud Integration |
| Node | Adds cloud metadata to Nodes |
| Route | Configures cloud network routes |
| Service | Provisions cloud load balancers |
| Volume | Provisions cloud storage |
The Decoupling Principle
Before cloud-controller-manager, cloud-specific code lived in kube-controller-manager. This made Kubernetes less portable. Cloud-controller-manager decouples cloud logic from core Kubernetes.
1:1 copy of the class Google Doc, code in code blocks.