# Kubernetes - ReplicaSet & DaemonSet

**ReplicaSet** is a Kubernetes object designed to maintain a specified number of pod replicas running at any given time. It ensures that a defined number of identical pods are always available, even in the event of failures. This feature is crucial for achieving high availability and reliability for your applications.

##### **<mark>Creating a ReplicaSet</mark>**

Below is an example of a simple ReplicaSet configuration that runs two instances of an Nginx container:

```plaintext
apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: my-replicaset
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: cont1
        image: httpd
        ports:
        - containerPort: 80
```

In this example:

* **replicas: 2** specifies the desired number of pod replicas.
    
* The **selector** matches pods with the label `app: nginx`.
    
* The **template** defines the pod’s specifications, including the container image (`httpd`) and the port (`80`).
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724276283051/3af38490-a0ac-48f9-835d-d90ec5e1a617.png align="center")

**Apply the ReplicaSet:**

To deploy this ReplicaSet, save the above YAML content in a file named `my-replicaset.yaml` and apply it using the following command:

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724276240958/a00b3d4d-4462-43ea-8b12-3e5aef685012.png align="center")

##### **<mark>Verifying the ReplicaSet</mark>**

After creating the ReplicaSet, you can verify its status and check that the desired number of pods are running:

```plaintext
kubectl get replicaset
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724276330263/658b7bbd-2d81-4510-8179-cd07e0e3dd91.png align="center")

```plaintext
kubectl get pods -o wide
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724276690236/27e6cde8-c601-4b41-9693-cf987619f856.png align="center")

---

#### **Introduction to DaemonSet**

**DaemonSet** is another crucial Kubernetes object, but unlike a ReplicaSet, it ensures that a particular pod runs on all (or a specific subset of) nodes in your cluster. This is especially useful for running background tasks or system-level services, such as log collection or monitoring agents, across all nodes.

##### **<mark>Creating a DaemonSet</mark>**

Below is an example of a DaemonSet configuration that ensures an HTTPD container runs on all nodes:

```plaintext
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: my-daemonset
spec:
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: cont1
        image: httpd
        ports:
        - containerPort: 24224
```

In this example:

* The **selector** matches pods with the label `app: nginx`.
    
* The **template** defines the pod’s specifications, ensuring that an HTTPD container runs on every node with the specified port (`24224`).
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724276449861/02996bcd-6cc1-4801-ab4f-a37637a9c09f.png align="center")

Apply the DaemonSet using the following command:

```plaintext
kubectl apply -f daemonset.yaml
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724276500210/6e705ccd-d77a-40a6-99ae-86eea10922dc.png align="center")

##### **Verifying the DaemonSet**

To ensure the DaemonSet is correctly running on all nodes, you can use the following commands:

```plaintext
kubectl get daemonset
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724276560660/224c4671-b48f-4033-9b9e-55a0467ac2bd.png align="center")

```plaintext
kubectl get pods -o wide
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724276650608/36ad8241-8334-4ae6-a9e1-431d4d0dd238.png align="center")

---

### **Conclusion**

Kubernetes **ReplicaSet** and **DaemonSet** are vital components for maintaining the desired state of your applications across your cluster. By understanding how to create and manage these objects, you can ensure greater control, reliability, and scalability for your Kubernetes-based applications.
