How to Manage Pods with Kubernetes ReplicaSet

ReplicaSet = Controller that keeps N Pods always running. We define the number of Pods running then kubenet keep this no of POD in running state.

We set 3 POD to run then ReplicaSet ensures:

  • ✅ Always 3 pods are running
  • ✅ If 1 pod dies → creates new pod
  • ✅ If extra pods exist → deletes extra

Example of a manifest file: It defines 3 pods to run. If a pod dies or has issues, a new pod is created without affecting the application, as it continues to run on the existing pods.

apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: nginx-rs
spec:
replicas: 3 # Desired pods
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80

Create replica set with the following command:

kubectl apply -f replicaset.yaml

View replicat set

kubectl get rs

Check the number of POD running

kubectl get pods

Describe the replicat set

 kubectl describe rs nginx-rs

Scale the replica Set

kubectl scale rs nginx-rs --replicas=5

Edit replica set

kubectl edit rs nginx-rs

Delete ReplicaSet

kubectl delete rs nginx-

For testing the replica set setting, we can manually delete a pod and check kubernet automatically up one new POD when it run.

— For delete

kubectl delete pod <pod-name>

New Pod is automatically created, verify:

kubectl get pod