Kubernetes

Kubernetes - Medium

Page 3 of 4

Workloads

Not every workload fits the Deployment model of "identical, interchangeable replicas."

  • StatefulSet - for workloads where identity matters: each replica gets a stable, predictable name (db-0, db-1, db-2, not random suffixes), its own stable PersistentVolumeClaim that follows it across rescheduling, and ordered, sequential startup/shutdown (db-0 fully ready before db-1 starts). This is what real databases and other clustered stateful systems need - a plain Deployment's interchangeable, arbitrarily-ordered Pods are actively wrong for them.
  • DaemonSet - runs exactly one Pod on every node (or every matching node) automatically, including new nodes as they join the cluster - the standard shape for node-level agents: log collectors, monitoring agents, CNI plugins themselves.
  • Job - runs a Pod (or several) to completion for a one-off task, tracking success/failure, and retrying on failure up to a configurable limit - the right primitive for a batch task, not a Deployment (which would just restart a "completed" Pod forever, since it doesn't understand success).
  • CronJob - a Job that runs on a schedule (standard cron syntax) - nightly backups, scheduled reports.

Headless Services (clusterIP: None) are what StatefulSets rely on for stable per-Pod DNS names (db-0.db-service.namespace.svc.cluster.local) rather than one load-balanced VIP - each Pod needs to be individually addressable, not hidden behind aggregate load-balancing.

kubectl get statefulsets
kubectl get daemonsets
kubectl get jobs
kubectl get cronjobs
kubectl create job manual-run --from=cronjob/nightly-backup     # trigger a CronJob's Job manually, e.g. to test it

Scaling

Horizontal Pod Autoscaler (HPA) adjusts a Deployment/StatefulSet's replica count automatically based on observed metrics - most commonly CPU or memory utilization percentage against the Pod's own requests, but it can also scale on custom or external metrics (queue depth, requests-per-second) once a metrics adapter is wired in.

kubectl autoscale deployment web --cpu-percent=70 --min=2 --max=10
kubectl get hpa                                      # current vs target metric, current replica count

Vertical Pod Autoscaler (VPA) takes the opposite axis - instead of changing replica count, it adjusts a Pod's resource requests/limits based on observed real usage. Conceptually useful for right-sizing workloads whose actual CPU/memory needs are hard to guess up front, though it typically requires restarting Pods to apply a new size (a real operational tradeoff against HPA, which scales without disrupting existing Pods).

Cluster Autoscaler operates one layer up from both: it adds or removes nodes from the cluster based on whether Pods are stuck Pending for lack of capacity, or nodes are sitting significantly underutilized. HPA/VPA answer "how much should this workload consume," Cluster Autoscaler answers "does the cluster have enough machines to fit what's being asked of it."


Rolling Updates

Revisiting the Deployment rollout mechanics with the knobs that actually matter operationally:

  • maxSurge - how many Pods above the desired replica count are allowed to exist temporarily during a rollout (absolute number or percentage).
  • maxUnavailable - how many Pods below the desired count are tolerated during a rollout.

Setting maxUnavailable: 0 guarantees full capacity is always available during a rollout (strictly surge-only), at the cost of needing enough spare cluster capacity to briefly run more Pods than the steady-state count - the real tradeoff between guaranteed availability and resource headroom.

A rollout that gets stuck (new Pods crash-looping, never becoming Ready) doesn't roll back on its own by default - it just stalls, with kubectl rollout status hanging and old Pods still serving traffic. kubectl rollout undo is a manual, deliberate action; progressDeadlineSeconds is what marks the Deployment as failed after a timeout, but even then, nothing auto-reverts unless something else (a CI/CD pipeline, an operator) is watching for that condition and triggers the rollback itself.


    Welcome to OpsQuiz!

    Real scenario-based DevOps questions, hands-on practice, and clear explanations for every answer.