Course contentsShow
← Cloud and Infrastructure
Lesson 1165 of 2,98923. Kubernetes WorkloadsFree lesson

Replica Management

Control the number of pod replicas using spec.replicas and understand how Deployments maintain desired state.

Replica Management

What you'll learn: How to control the exact number of pod copies running in your Deployment and how Kubernetes automatically maintains that desired count.

The Core Idea

In Kubernetes, replica management means telling your Deployment exactly how many identical pod copies (replicas) you want running at all times. You do this using spec.replicas in your Deployment manifest. Once set, Kubernetes works continuously to maintain that exact number—if a pod crashes, it creates a new one; if you have too many, it removes extras.

How spec.replicas Works

apiVersion: apps/v1
kind: Deployment
metadata:
  name: web-app
spec:
  replicas: 3  # Request exactly 3 pods
  selector:
    matchLabels:
      app: web
  template:
    metadata:
      labels:
        app: web
    spec:
      containers:
      - name: nginx
        image: nginx:1.20

In this example, replicas: 3 tells Kubernetes "I always want 3 copies of this pod running."

Maintaining Desired State

Think of replicas like a thermostat setting. You set it to 72°F, and the system constantly monitors and adjusts to maintain that temperature. Similarly, Kubernetes continuously compares:

  • Desired state: What you specified in spec.replicas
  • Current state: How many pods are actually running

If a pod dies, Kubernetes immediately schedules a replacement. If you manually delete a pod, another spawns. The Deployment controller never stops watching and correcting.

Changing Replica Count

You can modify replicas anytime with kubectl scale:

kubectl scale deployment web-app --replicas=5

Or update your YAML and reapply it.

Key Takeaway: The spec.replicas field defines your desired pod count, and Kubernetes Deployments automatically maintain that exact number through continuous reconciliation—ensuring high availability and reliability.