Loading

Quipoin Menu

Learn • Practice • Grow

kubernetes / ReplicaSets
tutorial

ReplicaSets

When you run a Pod, it is a single instance. If that Pod fails, it’s gone. To keep multiple copies of your application running and ensure they stay running, you need a controller. The simplest controller is a ReplicaSet.

What Is a ReplicaSet?

A ReplicaSet ensures that a specified number of identical Pods are running at all times. If a Pod fails, the ReplicaSet creates a new one. If there are too many, it terminates extra ones.

A ReplicaSet maintains a stable set of replica Pods running at any given time.

ReplicaSet YAML Example

Here’s a ReplicaSet that maintains three Nginx Pods:
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: nginx-rs
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
  • replicas: 3 – desired number of Pods.
  • selector – identifies which Pods belong to this ReplicaSet.
  • template – the Pod specification to create new Pods.

Creating and Inspecting a ReplicaSet

Save the YAML as nginx-rs.yaml and apply:
kubectl apply -f nginx-rs.yaml
View the ReplicaSet:
kubectl get replicasets
View the Pods it created:
kubectl get pods -l app=nginx

Scaling a ReplicaSet

You can scale the ReplicaSet imperatively:
kubectl scale replicaset nginx-rs --replicas=5
Or edit the YAML and reapply.

Deleting a ReplicaSet

Deleting the ReplicaSet also deletes its Pods:
kubectl delete replicaset nginx-rs


Two Minute Drill
  • ReplicaSet ensures a specified number of identical Pods run at all times.
  • Define replicas, selector, and pod template in YAML.
  • Scale with kubectl scale replicaset name --replicas=N.
  • Deleting a ReplicaSet deletes its Pods.

Need more clarification?

Drop us an email at career@quipoinfotech.com