1. Resource Allocations: Requests vs. Limits
Every container specification should define CPU and Memory resources:
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 MebibytesFundamental Differences:
- `requests` (Scheduling Guarantee): Used by
kube-schedulerto 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 inPendingstate. - `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:
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:
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.
DesiredReplicasTarget Container CountExact number of worker pods Kubernetes will maintain in the deployment
CurrentReplicasActive Running PodsCount of pods currently alive and handling user requests
CurrentMetricValueObserved Real-Time LoadAverage metric across all pods (e.g. 80% CPU or 250 req/sec)
DesiredMetricValueTarget SLA ThresholdYour configured SLA limit (e.g. 50% CPU or 100 req/sec)
⌈ ... ⌉Ceiling FunctionRounds fractional calculations up (e.g. 2.1 pods becomes 3 pods)
Ansible Playbook: Defining HPA with Stabilization Windows
- 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: 604. Storage Subsystem: PVs, PVCs, and StatefulSets
Containers have ephemeral filesystems; when a container restarts, modifications are wiped. Persistent data requires the Kubernetes Storage Architecture:
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.