DEV Community

Cover image for 🎯 Scenario #8 β€” Deploy a ReplicaSet and Verify Self-Healing of Pods in Kubernetes
Latchu@DevOps
Latchu@DevOps

Posted on

🎯 Scenario #8 β€” Deploy a ReplicaSet and Verify Self-Healing of Pods in Kubernetes

ReplicaSets ensure that the desired number of Pods is always running.

If a Pod is deleted manually β†’ ReplicaSet AUTO-CREATES a new one.


βœ… Step 1 β€” Create ReplicaSet YAML

# rs-nginx.yaml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: nginx-rs
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx-demo
  template:
    metadata:
      labels:
        app: nginx-demo
    spec:
      containers:
      - name: nginx
        image: nginx:1.25
        ports:
        - containerPort: 80
Enter fullscreen mode Exit fullscreen mode

Apply:

kubectl apply -f rs-nginx.yaml
Enter fullscreen mode Exit fullscreen mode

βœ… Step 2 β€” Verify ReplicaSet + Pods

kubectl get rs
kubectl get pods -o wide
Enter fullscreen mode Exit fullscreen mode

You should see 3 Pods with random hash-like suffixes:

nginx-rs-7dd8f995f5-rkzfx
nginx-rs-7dd8f995f5-pxbgl
nginx-rs-7dd8f995f5-7cj2p
Enter fullscreen mode Exit fullscreen mode

βœ… Step 3 β€” Test Self-Healing (Delete a Pod)

Pick any one:

kubectl delete pod nginx-rs-7dd8f995f5-rkzfx
Enter fullscreen mode Exit fullscreen mode

Watch Pods:

kubectl get pods -w
Enter fullscreen mode Exit fullscreen mode

Expected behavior:

  • Deleted Pod goes away
  • ReplicaSet automatically creates a NEW pod
  • Count stays at 3 always

Example:

pod "nginx-rs-xxx" deleted
pod "nginx-rs-newxxx" created
Enter fullscreen mode Exit fullscreen mode

πŸŽ‰ ReplicaSet self-healing verified!

1


⚠️ Self-Healing Works Only If Labels Match Selector

If you edit the Pod labels:

kubectl label pod POD_NAME app-
Enter fullscreen mode Exit fullscreen mode

β†’ ReplicaSet stops managing it
β†’ Will create a replacement pod.


πŸ”₯ BONUS TEST β€” Scale ReplicaSet

Increase replicas:

kubectl scale rs nginx-rs --replicas=5
Enter fullscreen mode Exit fullscreen mode

Decrease:

kubectl scale rs nginx-rs --replicas=2
Enter fullscreen mode Exit fullscreen mode

2


🌟 Thanks for reading! If this post added value, a like ❀️, follow, or share would encourage me to keep creating more content.


β€” Latchu | Senior DevOps & Cloud Engineer

☁️ AWS | GCP | ☸️ Kubernetes | πŸ” Security | ⚑ Automation
πŸ“Œ Sharing hands-on guides, best practices & real-world cloud solutions

Top comments (0)