Detailed explanation and example of daemonset of kubernetes resource controller
Host configuration planning
Server name (host name) | System version | to configure | Intranet IP | Internet IP (Analog) |
---|---|---|---|---|
k8s-master | CentOS7.7 | 2C/4G/20G | 172.16.1.110 | 10.0.0.110 |
k8s-node01 | CentOS7.7 | 2C/4G/20G | 172.16.1.111 | 10.0.0.111 |
k8s-node02 | CentOS7.7 | 2C/4G/20G | 172.16.1.112 | 10.0.0.112 |
What is a controller
Kubernetes has many controllers built in, which are equivalent to a state machine to control the specific state and behavior of pod.
- Replicationcontroller and replicaset
- Deployment
- DaemonSet
- StatefulSet
- Job/CronJob
- HorizontalPodAutoscaler
DaemonSet
Daemonset ensures that a copy of the pod runs on all (or some) nodes. When a node joins the cluster, a pod is added for them. When a node is removed from the cluster, the pods are also recycled. Deleting daemonset will delete all pods it creates.
- Run cluster storage daemonset on each node, such as glusterd and CEPH.
- Run the journal collection daemonset on each node, such as fluent, logstash.
- Run a monitoring daemonset on each node, such as Prometheus node exporter, flowmill, sysdig agent, collected, dynatrace oneagent, appdynamics agent, datadog agent, new relic agent, ganglia gmond, or instana agent.
A simple usage is to start a daemonset on all nodes and use it as a daemon of each type.
A slightly more complex usage is to use a daemonset for each of the daemon types individually. In this way, there are multiple daemonsets with different identities, and different memory and CPU requirements for different hardware types.
Note: the pods in daemonset can use hostport, so the pod can be accessed through the node IP; because the pod will not be scheduled to other nodes in daemonset mode. Examples are as follows:
ports:
- name: httpd
containerPort: 80
#Do not specify a hostport for the pod unless absolutely necessary. When you bind a pod to a hostport, it limits the number of locations that the pod can schedule, except for daemonset
#Generally, containerport has the same value as hostport
Hostport: 8090 ා the pod can be accessed by host + hostport. For example, if the pod is scheduled to k8s-node02 [172.16.1.112], the pod can be accessed through 172.16.1.112:8090.
protocol: TCP
Please refer to “kubernetes k8s’s creation of pod through yaml and details of common fields of pod file”
Daemonset example
Yaml file
[[email protected] controller]# pwd
/root/k8s_practice/controller
[[email protected] controller]# cat daemonset.yaml
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: fluentd-elasticsearch
namespace: default
labels:
k8s-app: fluentd-logging
spec:
selector:
matchLabels:
name: fluentd-elasticsearch
template:
metadata:
labels:
name: fluentd-elasticsearch
spec:
tolerations:
#Allow to run in master node
- key: node-role.kubernetes.io/master
effect: NoSchedule
containers:
- name: fluentd-elasticsearch
image: registry.cn-beijing.aliyuncs.com/google_registry/fluentd:v2.5.2
resources:
limits:
cpu: 1
memory: 200Mi
requests:
cpu: 100m
memory: 200Mi
volumeMounts:
- name: varlog
mountPath: /var/log
- name: varlibdockercontainers
mountPath: /var/lib/docker/containers
readOnly: true
#Elegant close application, time setting. After this time, the [optional] will be forced off, and the default is 30 seconds
terminationGracePeriodSeconds: 30
volumes:
- name: varlog
hostPath:
path: /var/log
- name: varlibdockercontainers
hostPath:
path: /var/lib/docker/containers
Run daemonset and view the status
[[email protected] controller]# kubectl apply -f daemonset.yaml
daemonset.apps/fluentd-elasticsearch created
[[email protected] controller]#
[[email protected] controller]# kubectl get daemonset -o wide
NAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE NODE SELECTOR AGE CONTAINERS IMAGES SELECTOR
fluentd-elasticsearch 3 3 3 3 3 92s fluentd-elasticsearch registry.cn-beijing.aliyuncs.com/google_registry/fluentd:v2.5.2 name=fluentd-elasticsearch
[[email protected] controller]#
[[email protected] controller]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
fluentd-elasticsearch-52b8z 1/1 Running 0 95s 10.244.2.92 k8s-node02
fluentd-elasticsearch-fps95 1/1 Running 0 95s 10.244.0.46 k8s-master
fluentd-elasticsearch-pz8j7 1/1 Running 0 95s 10.244.4.83 k8s-node01
It can be seen from the above that all nodes in the k8s cluster, including the master node, run the daemonset pod.
Related reading
1. Kubernetes k8s create pod through yaml and explain the common fields of pod file
2. Resource controller RC, RS and deployment of kubernetes k8s
3. Detailed explanation of resource controller statefulsets of kubernetes k8s
complete!
———END———
If you think it’s good, pay attention to it!