# 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](https://cloud.ionos.com/managed/kubernetes "Managed Kubernetes from IONOS") from IONOS, you get maximum flexibility in customizing 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](https://www.ionos.com/digitalguide/server/configuration/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 favorite text editor or integrated development environment (IDE).

```shell
vim replicaset.yaml
```

### Configuring the YAML file

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

```yaml
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
```

Customize 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:

```shell
$kubectl create -f replicaset.yaml
```

### Checking the status of the ReplicaSet

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

```shell
kubectl get replicasets new_replicaset
```

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

```shell
NAME                  DESIRED   CURRENT   READY   AGE
new_replicaset           3         3        3     1m
```

- **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.

```shell
kubectl get pods
```

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.

```shell
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
```

### 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:

```shell
kubectl replace -f replicaset.yaml
```

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

```shell
kubectl scale --replicas=4-f replicaset-app.yaml
```


This is a markdown version of: [https://www.ionos.com/digitalguide/server/configuration/kubernetes-replicaset/](https://www.ionos.com/digitalguide/server/configuration/kubernetes-replicaset/) for AI/LLM consumption.