Kubernetes: ReplicaSet
Tags: kubernetes, webdev, devops, tutorial
Published: 2023-04-19 08:33:26

## What is ReplicaSet?

ReplicaSet is a Kubernetes resource used to have a set of Pods running in Kubernetes running a specified number of times. ReplicaSet defines a requirement that defines the Pods' requirements to keep the Pods working properly.


## What are their Duties?

- **Creating a specified number of copies:** ReplicaSet is especially used for applications that require high availability. It provides high availability of applications by creating a specified number of pods.
- **Making sure the pods are working properly:** ReplicaSet makes sure that the pods are always working properly by making the specified number of replicas. In this way, in case of errors or malfunctions that may occur in the pods, ReplicaSet creates a new pod and ensures the smooth operation of the application.
- **Automatic scaling:** ReplicaSet enables applications to scale by detecting increases in application traffic and automatically creating new pods. Thus, performance problems that may occur as a result of increases in application traffic are prevented.
- **Managing the distribution of pods:** ReplicaSet manages the distribution of pods, allowing a specified number of copies of pods running on multiple nodes to be created. This increases the high availability of applications and prevents performance issues.
- **Managing updates:** ReplicaSet manages the update processes by uploading the new version to the pods in case of new versions of the applications are released. This makes it easier to manage application versions and application updates can be done more quickly.

```yaml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: my-replicaset
spec:
  replicas: 3
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - name: my-container
        image: nginx
```

This YAML file defines a ReplicaSet resource. The metadata part contains the name of the ReplicaSet. The spec part determines the number of replicas and defines a selector to determine which pods belong to the ReplicaSet. It also sets a template for all pods under ReplicaSet.

In the template part, a spec is defined that defines the properties of the pods. In this example, the nginx image with a single container is used. It is stated that ReplicaSet will work with 3 replicas. This means that ReplicaSet will always have 3 replicas.

You can apply the ReplicaSet to your Kubernetes cluster with kubectl using this example file:

```
kubectl apply -f my-replicaset.yaml
```

The kubectl scale command is used to scale the ReplicaSet. For example, if you created your ReplicaSet as three replicas, you can scale it to 5 replicas using the following command:

```
kubectl scale rs my-replicaset --replicas=5
```

This command sets the replica number of the ReplicaSet named my-replicaset to 5. If you haven't named your ReplicaSet before, you can get its name with kubectl get rs command.
New replicas are created based on the pod template specified by ReplicaSet and deployed based on the resources of your Kubernetes cluster.

If you want to undo the changes made, you can undo the action applied with ReplicaSet using the rollout undo command:


```
kubectl rollout undo deployment my-replicaset
```


## Things to pay attention

- ReplicaSet controls the Pods and scales a number of copies according to the specification of the specified state.
- ReplicaSet automatically manages Pods restart to ensure Pods maintainability.
- ReplicaSet does not control the location or distribution of Pods, it only ensures that a certain number of copies are run.
- ReplicaSet can also be used with update strategies such as RollingUpdate.
- The ReplicaSet's Pod template defines the template used when the Pod was created.
- ReplicaSet creates and manages Pods automatically, so it may be more convenient to manage Pods through ReplicaSet rather than directly.