Skip to content

Create and Manage Kubernetes ReplicaSets

Let's see how you can create and manage ReplicaSets.

Here is the Docker Image used in this tutorial: reyanshkharga/nginx


You cannot create a ReplicaSet using imperative command. The only way to create ReplicaSets is by using the declarative approach.

Step 1. Create ReplicaSet Manifest

First, we need to write the ReplicaSet manifest as follows:

apiVersion: apps/v1
kind: ReplicaSet
  name: my-replicaset
  replicas: 2
      app: nginx
        app: nginx
        tier: backend
      - name: nginx
        image: reyanshkharga/nginx:v1
        imagePullPolicy: Always
          - containerPort: 80

Required fields:

  • apiVersion - Which version of the Kubernetes API you're using to create this object.
  • kind - What kind of object you want to create.
  • metadata - Data that helps uniquely identify the object, including a name string, UID , and - optional namespace.
  • spec - What state you desire for the object.

Step 2: Create ReplicaSet

Let's use kubectl apply to apply the manifest and create the ReplicaSet.

# Create replicaset
kubectl apply -f my-replicaset.yml

Step 3: List ReplicaSets

# List all replicasets
kubectl get replicasets

# List all replicasets with expanded (aka "wide") output
kubectl get replicasets -o wide

Resource types are case-insensitive and you can specify the singular, plural, or abbreviated forms.

The following commands produce the same output:

kubectl get rs 
kubectl get replicaset
kubectl get replicasets


replicaset is abbreviated as rs.

Step 4: Describe a ReplicaSet

# Command template
kubectl describe rs <replicaset-name>
kubectl describe rs/<replicaset-name>

# Actual command
kubectl describe rs my-replicaset
kubectl describe rs/my-replicaset

Step 5: Scale a ReplicaSet

You can use kubectl scale command to scale a ReplicaSet up or down by changing the desired number of replicas.

# Command template
kubectl scale rs <replicaset-name> --replicas <desired-count>
kubectl scale rs/<replicaset-name> --replicas <desired-count>

# Actual command - scale up
kubectl scale rs my-replicaset --replicas 3
kubectl scale rs/my-replicaset --replicas 3

# Actual command - scale down
kubectl scale rs my-replicaset --replicas 1
kubectl scale rs my-replicaset --replicas 1

Or, you can change the value of replicas field to a desired value in the manifest and then apply the manifest again. This method is desirable and recommended.

# Update the replicaset
kubectl apply -f my-replicaset.yml

Step 6: ReplicaSet in Action

Let's delete a pod in the ReplicaSet and notice what happens.

kubectl delete pod <pod-name>

You will notice that as soon as the pod terminates, the ReplicaSet will spawn a new pod using the template to ensure that the desired number of pods is always running.

# Watch and continuously update the status of pods in real-time
kubectl get pods -w

Step 7: Delete a ReplicaSet

# Command template
kubectl delete rs <replicaset-name>
kubectl delete rs/<replicaset-name>

# Actual command
kubectl delete rs my-replicaset
kubectl delete rs/my-replicaset