What do Kubernetes ReplicaSets do?

Kubernetes ReplicaSets are a reliable and highly flexible solution for managing container applications. You can define the number of identical pods and the containers they contain in the YAML files.

What are Kubernetes ReplicaSets?

ReplicaSets are a central resource in Kubernetes that are responsible for managing identical pods. Their main purpose is to keep the desired number of copies of a pod constant. ReplicaSets continuously monitor the state of the pods and automatically initiate scaling. Depending on the configuration, they start new pods or terminate surplus ones.

Tip

With managed Kubernetes from IONOS, you get maximum flexibility in customising your Kubernetes resources. In the IONOS cloud infrastructure, you can precisely configure worker nodes, from the number of CPUs to the memory size.

How to create a Kubernetes ReplicaSet

To operate ReplicaSets, you need a running Kubernetes cluster, either locally on your development system (such as Minikube) or in a production environment. In the Kubernetes tutorial, we explain the setup in detail. Make sure that Kubeconfig is set correctly to communicate with the cluster. Then you can start configuring the ReplicaSet.

Opening a text editor

Create and open a YAML file with your favourite text editor or integrated development environment (IDE).

vim replicaset.yaml
shell

Configuring the YAML file

Within the editor, you define the YAML configuration for your ReplicaSet.

apiVersion: apps/v1
kind: ReplicaSet
metadata:
    name: new_replicaset
spec:
    replicas: 3
    selector:
        matchLabels:
            app: app
    template:
        metadata:
            labels:
                app: app
        spec:
            containers:
            - name: container
                image: container-image:latest
yaml

Customise the names and images according to your application and save the file.

Activating the Kubernetes ReplicaSet

Use the following command to apply the configuration from your YAML file to the Kubernetes cluster:

$kubectl create -f replicaset.yaml
shell

Checking the status of the ReplicaSet

Check the status of the created Kubernetes ReplicaSet to make sure it has been successfully activated.

kubectl get replicasets new_replicaset
shell

You should see an output showing your new ReplicaSet with the desired number of replicas.

NAME                  DESIRED   CURRENT   READY   AGE
new_replicaset           3         3        3     1m
shell
  • NAME: The name of the ReplicaSet.
  • DESIRED: The desired number of replicas as specified in the YAML file.
  • CURRENT: The current number of running replicas.
  • READY: The number of replicas that are marked as ‘READY’ and ready for traffic.
  • AGE: The time since the ReplicaSet was created.

Checking the status of the pods

In addition, check the status of the pods you have created.

kubectl get pods
shell

This will show you a list of the created pods with information about their status. If all pods have the status Running and the desired number of replicas has been reached, your Kubernetes ReplicaSet has been successfully created and activated.

NAME                           READY       STATUS    RESTARTS    AGE
new_replicaset-xxxx        		1/1       Running        0        1m
new_replicaset-yyyy             1/1       Running        0        1m
new_replicaset-zzzz             1/1       Running        0        1m
shell

Scaling ReplicaSets

If you want to change the number of replicas in your ReplicaSet, adjust your YAML file. Simply set the value of the replicas field to the desired number and save the file. Then enter the following command to replace the existing Kubernetes ReplicaSet with the updated ReplicaSet from your YAML file:

kubectl replace -f replicaset.yaml
shell

Alternatively, you can scale the number of replicas with kubectl scale:

kubectl scale --replicas=4-f replicaset-app.yaml
shell
IONOS Cloud Managed Kubernetes
Container workloads in expert hands

The ideal platform for demanding, highly scalable container applications. Managed Kubernetes works with many cloud-native solutions and includes 24/7 expert support.

Was this article helpful?
Page top