Stage 11, Part 1 of 9, Cluster Lifecycle & Operations

Text

PART 1: CORE THEORY

1. The Cluster Lifecycle

A Kubernetes cluster is not a static artifact. It has a lifecycle: provision, operate, upgrade, scale, and eventually retire. Every phase must be planned, automated, and tested.

The Five Phases

PhaseActivitiesTools
ProvisioningCreate control plane, join nodes, install CNI/CSIkubeadm, Cluster API, managed services
OperationsMonitor, scale, back up, rotate certsPrometheus, Velero, cert-manager
UpgradesControl plane, then workers, then validationkubeadm, Cluster API
Disaster RecoveryRestore from etcd snapshot or Velero backupetcdctl, Velero
RetirementDrain nodes, delete cluster, clean up cloud resourceskubectl, Terraform

Why Lifecycle Management Matters

A poorly prepared update represents a significant portion of cluster incidents. IT teams spend an average of 34 working days per year resolving Kubernetes issues, and a large share comes from unplanned upgrade problems.


2. Kubernetes Version Skew Policy

The version skew policy defines which combinations of component versions are supported. Violating it causes subtle failures that are hard to diagnose.

Supported Versions

The Kubernetes project maintains release branches for the most recent three minor releases (e.g., 1.37, 1.36, 1.35). Kubernetes 1.19 and newer receive approximately 1 year of patch support.

Version Skew Rules

ComponentRule
kube-apiserver (HA)Newest and oldest instances must be within one minor version
kubeletMust not be newer than apiserver; may be up to three minor versions older
kube-proxyMust not be newer than apiserver; may be up to three minor versions older
kubectlWithin one minor version of apiserver (older or newer)

Example: HA Cluster with Version Skew

text
kube-apiserver instances: 1.37 and 1.36
kubelet supported: 1.36, 1.35, 1.34
Note: 1.37 is NOT supported because that would be newer than the 1.36 apiserver instance[reference:2]

The Golden Rule

Never more than one minor version difference between components during an upgrade. Upgrade control plane first, then workers one by one.


3. Cluster Upgrades

Kubernetes upgrades follow a strict control plane first, then workers one by one sequence.

The Three Phases

text
Phase 1: Control Plane Upgrade
    │
    ├── Upgrade API server
    ├── Upgrade controller manager
    ├── Upgrade scheduler
    └── Upgrade etcd
    │
    ▼
Phase 2: Worker Node Upgrade (one at a time)
    │
    ├── Cordon node
    ├── Drain node (evict pods)
    ├── Upgrade kubelet and kube-proxy
    ├── Uncordon node
    └── Repeat for each node
    │
    ▼
Phase 3: Validation
    │
    ├── Check node status
    ├── Verify workloads
    └── Monitor for issues

Pre-Upgrade Checklist

StepCommand
Check current versionkubectl version --short
Check target versionkubeadm upgrade plan
Audit deprecated APIskubectl get --raw /metrics | grep apiserver_requested_deprecated_apis
Back up etcdetcdctl snapshot save
Check PDBskubectl get pdb -A
Verify node healthkubectl get nodes

kubeadm Upgrade Commands

bash
# On control plane node
sudo kubeadm upgrade plan
sudo kubeadm upgrade apply v1.36.0

# On other control plane nodes
sudo kubeadm upgrade node

# On worker nodes (one at a time)
kubectl cordon <node>
kubectl drain <node> --ignore-daemonsets --delete-emptydir-data
sudo kubeadm upgrade node
kubectl uncordon <node>

Upgrade Timeline

Cluster SizeExpected Time
3 nodes30–60 minutes
10 nodes2–4 hours
50 nodes8–12 hours
100+ nodes1–2 days

4. Node Lifecycle Management

Node lifecycle operations are the foundation of cluster maintenance.

Cordon

Cordon marks a node as unschedulable. Existing Pods continue running, but new Pods won't be scheduled.

bash
kubectl cordon <node>

Drain

Drain evicts all Pods from a node (except DaemonSets) and marks it unschedulable.

bash
kubectl drain <node> --ignore-daemonsets --delete-emptydir-data

Options:

FlagPurpose
--ignore-daemonsetsSkip DaemonSet Pods (they can't be evicted)
--delete-emptydir-dataDelete Pods using emptyDir
--forceForce deletion of Pods not managed by controllers
--grace-periodOverride default grace period
--timeoutTimeout for the drain

Uncordon

Uncordon makes the node schedulable again.

bash
kubectl uncordon <node>

PodDisruptionBudgets and Drain

PDBs protect applications during voluntary disruptions. A PDB defines how many pods must remain available during a drain. Without PDBs, a node drain can take your entire service offline if all replicas are on one node.

Common PDB mistakes:

MistakeImpact
Never allow disruptionsAllowed to equal 0 in steady stateDrain stalls forever
Using PDBs for single-replica deploymentsDrain blocks
Scope PDB selector too broadlyUnexpected protection
Not setting PDBs at allService outage during drain

Graceful Node Shutdown

Kubernetes supports graceful node shutdown — when the OS signals shutdown, kubelet terminates Pods gracefully.

yaml
# /var/lib/kubelet/config.yaml
shutdownGracePeriod: 30s
shutdownGracePeriodCriticalPods: 10s

5. etcd Backup and Restore

etcd stores 100% of Kubernetes cluster state — namespaces, deployments, services, RBAC, CRDs, secrets. Without a functional etcd backup, corruption or data loss means complete cluster reconstruction.

Manual Snapshot

bash
export ETCDCTL_API=3

etcdctl snapshot save /backup/etcd-$(date +%Y%m%d-%H%M%S).db \
  --endpoints=https://127.0.0.1:2379 \
  --cacert=/etc/kubernetes/pki/etcd/ca.crt \
  --cert=/etc/kubernetes/pki/etcd/server.crt \
  --key=/etc/kubernetes/pki/etcd/server.key

# Verify integrity
etcdctl snapshot status /backup/etcd-20260228-143000.db --write-out=table

Automated etcd Backup (CronJob)

yaml
apiVersion: batch/v1
kind: CronJob
metadata:
  name: etcd-backup
  namespace: kube-system
spec:
  schedule: "0 */6 * * *"  # Every 6 hours
  jobTemplate:
    spec:
      template:
        spec:
          hostNetwork: true
          containers:
          - name: backup
            image: registry.k8s.io/etcd:3.5.12-0
            command:
            - /bin/sh
            - -c
            - |
              etcdctl snapshot save /backup/etcd-$(date +%Y%m%d-%H%M%S).db
              find /backup -mtime +7 -delete
            env:
            - name: ETCDCTL_API
              value: "3"
            volumeMounts:
            - name: etcd-certs
              mountPath: /etc/kubernetes/pki/etcd
              readOnly: true
            - name: backup
              mountPath: /backup

Restore Procedure

  1. Stop the API server (move static pod manifests)
  2. Remove old etcd data from /var/lib/etcd
  3. Restore the snapshot with etcdutl snapshot restore
  4. Restore static pod manifests
  5. Restart kubelet
  6. Verify cluster health

Critical: Test restores in staging before production. The etcd snapshot restore process modifies cluster identifiers.


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