Lesson 6 / 10
Module 6Lesson 1⏱ 55 min study

Lesson 6: Resource Governance, Autoscaling & Storage Architecture

Master CPU/memory requests and limits, evaluate Kubernetes QoS tiers, implement dynamic Horizontal Pod Autoscaling, and manage PersistentVolumes with StatefulSets.

✦ Key Architectural Takeaways
  • •Requests dictate scheduler node placement guarantees; Limits define hard cgroup enforcement thresholds.
  • •Exceeding CPU limits results in CFS process throttling; exceeding memory limits triggers the Linux OOMKiller.
  • •Kubernetes assigns Pods into three Quality of Service (QoS) classes: Guaranteed, Burstable, and BestEffort.
  • •Horizontal Pod Autoscalers (HPA) query metrics and scale replica counts dynamically based on target thresholds.
  • •Stateful workloads require PersistentVolumeClaims (PVC), dynamic StorageClasses, and StatefulSets for predictable network and storage identities.

1. Resource Allocations: Requests vs. Limits

Every container specification should define CPU and Memory resources:

YAMLSYSTEM SCHEMATIC
resources:
  requests:
    cpu: "250m"      # 250 millicores (0.25 CPU core)
    memory: "256Mi"  # 256 Mebibytes
  limits:
    cpu: "500m"      # 500 millicores (0.50 CPU core)
    memory: "512Mi"  # 512 Mebibytes

Fundamental Differences:

  • `requests` (Scheduling Guarantee): Used by kube-scheduler to determine which worker node has sufficient unreserved capacity to host the Pod. If no node has 250m CPU and 256Mi RAM available, the Pod remains in Pending state.
  • `limits` (Cgroup Enforcement Ceiling):
  • CPU Limits (Compressible Resource): When a container reaches its CPU limit, the Linux kernel CFS scheduler throttles execution by pausing clock cycles. The application slows down, but is NOT terminated.
  • Memory Limits (Incompressible Resource): When a container exceeds its memory limit, the Linux kernel cannot pause memory usage. The OOMKiller terminates the container immediately with Exit Code 137 (OOMKilled).
§

2. Kubernetes Quality of Service (QoS) Classes

When a worker node experiences severe memory pressure, Kubernetes must evict Pods to save the operating system. It decides eviction order based on QoS tiers:

ARCHITECTURE DIAGRAMSYSTEM SCHEMATIC
Eviction Priority Under Memory Pressure:
1. BestEffort (Evicted First)
2. Burstable  (Evicted Second)
3. Guaranteed (Protected; Evicted Last)
  • Guaranteed: Every container in the Pod has identical CPU/memory requests and limits (requests == limits). These are mission-critical services protected until the very last resort.
  • Burstable: Containers specify requests, but limits are higher (requests < limits). Common for bursty web services.
  • BestEffort: Containers declare NO requests and NO limits. Lowest priority; first to be evicted when node memory is constrained.
§

3. Horizontal Pod Autoscaler (HPA) Mechanics

The Horizontal Pod Autoscaler automatically increases or decreases the number of Pod replicas based on observed CPU/memory utilization or custom application metrics.

The HPA controller queries the Metrics Server periodically (default: every 15 seconds) and calculates desired replicas using the formula:

∑Kubernetes Horizontal Pod Autoscaler (HPA) Formula
AUTOSCALING ALGORITHM
DesiredReplicas = lceil CurrentReplicas cdot (CurrentMetricValue / DesiredMetricValue) ceil
💡
In Plain English (The Core Intuition)

Calculates the exact number of container pods required by dividing actual real-time load by your desired target metric, rounding up to the nearest whole pod to guarantee zero downtime.

Variable & Component Breakdown
DesiredReplicasTarget Container Count

Exact number of worker pods Kubernetes will maintain in the deployment

CurrentReplicasActive Running Pods

Count of pods currently alive and handling user requests

CurrentMetricValueObserved Real-Time Load

Average metric across all pods (e.g. 80% CPU or 250 req/sec)

DesiredMetricValueTarget SLA Threshold

Your configured SLA limit (e.g. 50% CPU or 100 req/sec)

⌈ ... ⌉Ceiling Function

Rounds fractional calculations up (e.g. 2.1 pods becomes 3 pods)

Ansible Playbook: Defining HPA with Stabilization Windows

YAMLSYSTEM SCHEMATIC
- name: Deploy Horizontal Pod Autoscaler
  kubernetes.core.k8s:
    state: present
    definition:
      apiVersion: autoscaling/v2
      kind: HorizontalPodAutoscaler
      metadata:
        name: web-app-hpa
        namespace: production-workloads
      spec:
        scaleTargetRef:
          apiVersion: apps/v1
          kind: Deployment
          name: web-application
        minReplicas: 2
        maxReplicas: 10
        metrics:
          - type: Resource
            resource:
              name: cpu
              target:
                type: Utilization
                averageUtilization: 70
        behavior:
          scaleDown:
            stabilizationWindowSeconds: 300 # Prevents scaling thrashing
            policies:
              - type: Percent
                value: 50
                periodSeconds: 60
§

4. Storage Subsystem: PVs, PVCs, and StatefulSets

Containers have ephemeral filesystems; when a container restarts, modifications are wiped. Persistent data requires the Kubernetes Storage Architecture:

ARCHITECTURE DIAGRAMSYSTEM SCHEMATIC
Cloud Storage Provider (AWS EBS / GCP Persistent Disk / Ceph)
                           |
                           v
              [ StorageClass (CSI Driver) ]
              (Dynamically provisions storage)
                           |
                           v
           [ PersistentVolume (PV - Cluster Resource) ]
                           |
                           v Bound (1-to-1)
       [ PersistentVolumeClaim (PVC - User Request) ]
                           |
                           v Mounted as Volume
                 [ StatefulSet Pod: db-0 ]
  • PersistentVolume (PV): A piece of physical storage provisioned in the cluster (e.g., an AWS EBS volume or NFS export).
  • PersistentVolumeClaim (PVC): A request for storage by an application, specifying size and access mode (ReadWriteOnce, ReadOnlyMany, ReadWriteMany).
  • StorageClass: Enables dynamic provisioning. When a PVC requests a StorageClass, the cloud CSI driver automatically provisions the underlying storage volume on the fly.
  • StatefulSets: Specialized controllers designed for stateful applications (databases, Kafka, ZooKeeper) requiring stable network identities (db-0, db-1) and dedicated, persistent disks that survive pod restarts.