ReplicaSets and DaemonSets

Compare ReplicaSets and Replication Controllers

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

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

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

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

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

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

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

Learn more about DaemonSets here .

Conclude

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.

Last updated