# ReplicaSet

## [About](https://kubernetes.io/docs/reference/kubernetes-api/workload-resources/replica-set-v1/)

A ReplicaSet ensures a **specified number of Pods** are always running.

* If a Pod dies → ReplicaSet spins up a new one.
* If you scale the replica count → more Pods are created.

Uses **labels** to know which Pods it manages.

{% hint style="warning" %}
Changes inside the Pod, like image version changes, will only be applied for new created Pods.
{% endhint %}

{% hint style="info" %}
You usually **don’t create ReplicaSets directly**, they managed by [#deployment](#deployment "mention") (which gives you extra feature).
{% endhint %}

## Commands

To apply and run a Replicaset configuration:

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

To list all the Replicasets, even the ones created by Deployments:

```bash
kubectl get replicasets
```

To describe a Replicaset:

```bash
kubectl describe replicaset <replicaset-name>
```

To delete a Replicaset:

```bash
kubectl delete replicaset <replicaset-name>
```

## Example

{% code title="replicaset.yaml" %}

```yaml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: myapp-rs
spec:
  replicas: 3
  selector:
    matchLabels:
      app: myapp
  # Pod template
  template:
    metadata:
      labels:
        app: myapp
    spec:
      containers:
        - name: myapp-container
          image: nginx:1.25
          ports:
            - containerPort: 80
          env:
            - name: NODE_ENV
              value: "production"
```

{% endcode %}
