Pod as DaemonSets

If a Kubernetes DaemonSet is created, the defined Pod will be deployed in every single node. It is guaranteed that the running containers occupy equal resources in each node. In this scenario, the container usually works as the daemon process.

For example, the following template has an Ubuntu image container that keeps checking its memory usage half a minute at a time:

  1. To build it as a DaemonSet, execute the following code block:
$ cat daemonset-free.yaml
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: ram-check
spec:
selector:
matchLabels:
name: checkRam
template:
metadata:
labels:
name: checkRam
spec:
containers:
- name: ubuntu-free
image: ubuntu
command: ["/bin/bash","-c","while true; do free; sleep 30; done"]
restartPolicy: Always

As the Job, the selector could be ignored, but it takes the values of the labels. We will always configure the restart policy of the DaemonSet as Always, which makes sure that every node has a Pod running.

  1. The abbreviation of the daemonset is ds in kubectl command, use this shorter one in the CLI for convenience:
$ kubectl create -f daemonset-free.yaml
daemonset.apps "ram-check" created

$ kubectl get ds
NAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE NODE SELECTOR AGE
ram-check 2 2 2 2 2 <none> 5m
  1. Here, we have two Pods running in separated nodes. They can still be recognized in the channel of the pod:
$ kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE
ram-check-6ldng 1/1 Running 0 9m 10.233.102.130 node1
ram-check-ddpdb 1/1 Running 0 9m 10.233.75.4 node2
  1. It is good for you to evaluate the result using the subcommand kubectl logs:
$ kubectl logs ram-check-6ldng
total used free shared buff/cache available
Mem: 3623848 790144 329076 9128 2504628 2416976
Swap: 0 0 0
total used free shared buff/cache available
Mem: 3623848 786304 328028 9160 2509516 2420524
Swap: 0 0 0
total used free shared buff/cache available
Mem: 3623848 786344 323332 9160 2514172 2415944
Swap: 0 0 0
.
.

Whenever, you add a Kubernetes node onto your existing cluster, DaemonSets will recognize and deploy a Pod automatically.

  1. Let's check again current status of DaemonSets, there are two Pods that have been deployed due to having two nodes as follows:
$ kubectl get ds
NAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE NODE SELECTOR AGE
ram-check 2 2 2 2 2 <none> 14m

$ kubectl get nodes
NAME STATUS ROLES AGE VERSION
node1 Ready master,node 6h v1.10.2
node2 Ready node 6h v1.10.2
  1. So, now we are adding one more node onto the cluster through either kubespray or kubeadm, based on your setup:
$ kubectl get nodes
NAME STATUS ROLES AGE VERSION
node1 Ready master,node 6h v1.10.2
node2 Ready node 6h v1.10.2
node3 Ready node 3m v1.10.2
  1. A few moments later, without any operation, the DaemonSet's size become 3 automatically, which aligns to the number of nodes:
$ kubectl get ds
NAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE NODE SELECTOR AGE
ram-check 3 3 3 3 3 <none> 18m


$ kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE
ram-check-6ldng 1/1 Running 0 18m 10.233.102.130 node1
ram-check-ddpdb 1/1 Running 0 18m 10.233.75.4 node2
ram-check-dpdmt 1/1 Running 0 3m 10.233.71.0 node3
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.168.203