# ReplicaSets and DaemonSets

#### Compare ReplicaSets and Replication Controllers <a href="#block-9e281bc4ac904209a4584508677893f5" id="block-9e281bc4ac904209a4584508677893f5"></a>

Replication Controllers (RC) have been replaced by ReplicaSets (RS) in the Kubernetes release .`1.9`

RS and RC are two resources that work similarly. But RS is more flexible in the _Label Selector_ property. With RC it can only select Pods whose Label is exactly the same as its Label. RS allows using a number of expressions in the Label Selector section.

For example, RC cannot select both Pods at the same time, while RS can, by specifying a label selector with the value . In addition, RS also supports using Operators with the following properties:`env=productionenv=testingenv=***matchExpressions`

Copy

```yaml
selector:
  matchExpressions:
    - key: app
      operator: In
      values:
        - kubia
```

There are four basic Operators: In, NotIn, Exists, DoesNotExist. Allows one RS to select many different Pods.

### Use ReplicaSets <a href="#block-7bbcc85889f74c319143d60df38bf606" id="block-7bbcc85889f74c319143d60df38bf606"></a>

We will use ReplicaSets (RS) to deploy Pods instead of using RC. The RS configuration will be the same as the RC, just with different properties . Create a file named :`selectorhello-rs.yaml`

Copy

```yaml
apiVersion: apps/v1 # change version API
kind: ReplicaSet # change resource name
metadata:
  name: hello-rs
spec:
  replicas: 2
  selector:
    matchLabels: # change here
      app: hello-kube
  template:
    metadata:
      labels:
        app: hello-kube
    spec:
      containers:
      - image: 080196/hello-kube
        name: hello-kube
        ports:
          - containerPort: 3000
```

Run the command :`apply`

Copy

```
kubectl apply -f hello-rs.yaml
```

Check if RS was created successfully:

Copy

```
kubectl get rs
```

Copy

```
NAME       DESIRED   CURRENT    READY   AGE
hello-rs   2         2          2       3s
```

Check if the number of Pods created by RS is equal to the field :`replicas`

Copy

```
kubectl get pod
```

Copy

```
NAME             READY   STATUS    RESTARTS   AGE
hello-rs-gmxfp   1/1     Running   0          21s
hello-xhtkg      1/1     Running   0          21s
```

If there are 2 pods created, we have run RS successfully. As we can see, the way RS works is similar to RC, the only difference is the attribute configuration . This may be a question that appears in an interview if we meet someone who likes to ask about theory.`selector`

To delete RS, we run the command :`delete`

Copy

```
kubectl delete rs hello-rs
```

### DaemonSets <a href="#block-0649d8508a294548a4a21158156192bd" id="block-0649d8508a294548a4a21158156192bd"></a>

Unlike RS, RS-managed Pods can be deployed in any Node, and in one Node, many Pods can be run. DaemonSets are used when we want to deploy a Pod for each Node, the number of Pods will correspond to the number of selected Nodes, it will not have the .`replicas`

<figure><img src="https://images.spr.so/cdn-cgi/imagedelivery/j42No7y-dcokJuNgXeA0ig/ec945b7a-0f5a-4f2a-ac8e-4f48f445608e/Untitled/w=1200,quality=80,fit=scale-down" alt=""><figcaption></figcaption></figure>

One of the applications of DaemonSets is in log collection and system monitoring. When we need to collect logs from Node, we only need one Pod for each Node, because if there are more than one Pod collecting logs from Node, there will be duplicate logs when storing.

Another example is that we need to monitor all Nodes using SSD drives. We will type the label into the Node as follows:

Copy

```
kubectl label nodes <your-node-name> disk=ssd
```

Then specify the DaemonSets properties:`nodeSelector`

Copy

```yaml
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: ssd-monitor
spec:
  selector:
    matchLabels:
      app: ssd-monitor
  template:
    metadata:
      labels:
        app: ssd-monitor
    spec:
      nodeSelector: # config here
        disk: ssd
      containers:
        - name: main
          image: luksa/ssd-monitor
```

When creating DaemonSets, it only deploys Pods to Nodes labeled as .`disk=ssd`

<figure><img src="https://images.spr.so/cdn-cgi/imagedelivery/j42No7y-dcokJuNgXeA0ig/ee21eeb4-2e32-464d-a914-2582e1359a5f/Untitled/w=1200,quality=80,fit=scale-down" alt=""><figcaption></figcaption></figure>

Learn more about DaemonSets [here](https://kubernetes.io/docs/concepts/workloads/controllers/daemonset/) .

### Conclude <a href="#block-26a05f67463c46eda1be59c4c6f59d08" id="block-26a05f67463c46eda1be59c4c6f59d08"></a>

We have finished learning about ReplicaSets and DaemonSet. RS is an advanced resource of RC and is used in practice. Use DaemonSet when you want to deploy a Pod for each Node.
