0
0
Kubernetesdevops~5 mins

Why ReplicaSets ensure availability in Kubernetes - Why It Works

Choose your learning style9 modes available
Introduction
When you run applications in Kubernetes, you want them to keep working even if some parts fail. ReplicaSets help by making sure a set number of copies of your app are always running. If one copy stops, ReplicaSets start a new one automatically.
When you want your app to keep running even if a pod crashes or is deleted.
When you need to run multiple copies of the same app to handle more users.
When you want Kubernetes to automatically fix problems by restarting pods.
When you want to update your app without downtime by managing pods carefully.
When you want to ensure your app is always available during maintenance or failures.
Config File - replicaset.yaml
replicaset.yaml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: example-replicaset
  labels:
    app: my-app
spec:
  replicas: 3
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - name: my-app-container
        image: nginx:1.23.3
        ports:
        - containerPort: 80

This file defines a ReplicaSet named example-replicaset that keeps 3 copies of the nginx container running.

The replicas field sets how many copies to keep.

The selector tells Kubernetes which pods belong to this ReplicaSet by matching labels.

The template describes the pod to create, including container image and ports.

Commands
This command creates the ReplicaSet in Kubernetes using the configuration file. It tells Kubernetes to start and maintain 3 pods as defined.
Terminal
kubectl apply -f replicaset.yaml
Expected OutputExpected
replicaset.apps/example-replicaset created
This command lists all pods with the label app=my-app to verify that 3 pods are running as expected.
Terminal
kubectl get pods -l app=my-app
Expected OutputExpected
NAME READY STATUS RESTARTS AGE example-replicaset-abc123 1/1 Running 0 10s example-replicaset-def456 1/1 Running 0 10s example-replicaset-ghi789 1/1 Running 0 10s
-l - Filter pods by label
This command deletes one of the pods to simulate a failure. ReplicaSet will detect this and create a new pod to keep the count at 3.
Terminal
kubectl delete pod example-replicaset-abc123
Expected OutputExpected
pod "example-replicaset-abc123" deleted
Check again to see that the ReplicaSet has started a new pod to replace the deleted one, maintaining availability.
Terminal
kubectl get pods -l app=my-app
Expected OutputExpected
NAME READY STATUS RESTARTS AGE example-replicaset-def456 1/1 Running 0 30s example-replicaset-ghi789 1/1 Running 0 30s example-replicaset-jkl012 1/1 Running 0 5s
-l - Filter pods by label
Key Concept

ReplicaSets keep a set number of pod copies running to ensure your app stays available even if some pods fail or are deleted.

Common Mistakes
Not setting the selector labels correctly in the ReplicaSet spec.
Kubernetes won't know which pods belong to the ReplicaSet, so it won't manage them properly.
Make sure the selector labels exactly match the labels in the pod template.
Deleting pods manually without understanding ReplicaSet behavior.
ReplicaSet will immediately create new pods, which might confuse beginners expecting pods to stay deleted.
Understand that ReplicaSets automatically replace deleted pods to maintain availability.
Summary
Apply a ReplicaSet configuration to tell Kubernetes how many pod copies to keep running.
Use label selectors to link pods to the ReplicaSet for management.
ReplicaSets automatically replace pods if they fail or are deleted to keep your app available.