Book source: Cloudman "Playing with Kubernetes in 5 minutes a day"
Organize the teacher's course content and test notes while studying, and share them with everyone. Any infringement will be deleted. Thank you for your support!
Attach a summary post: Play Kubernetes in 5 minutes a day | Summary_COCOgsta's Blog-CSDN Blog
The replica Pod deployed by Deployment will be distributed on each Node, and each Node may run several replicas. The difference of DaemonSet is that each Node can only run at most one copy.
Typical application scenarios of DaemonSet are:
(1) Run a storage Daemon, such as glusterd or ceph, on each node of the cluster.
(2) Run a log collection Daemon on each node, such as flunentd or logstash.
(3) Run a monitoring Daemon on each node, such as Prometheus Node Exporter or collectd.
In fact, Kubernetes itself uses DaemonSet to run system components. Execute the following command, as shown in the figure.
kubectl get daemonset --namespace=kube-system
DaemonSet kube-flannel-ds and kube-proxy are responsible for running flannel and kube-proxy components on each node, as shown in the figure.
Because flannel and kube-proxy are system components, you need to specify namespace kube-system through --namespace=kube-system on the command line. If not specified, only resources in the default namespace default will be returned.
5.2.1 kube-flannel-ds
Below we learn DaemonSet by analyzing kube-flannel-ds.
Remember how the flannel network was deployed before? We executed the following command: (actually, it is not deployed in this way, because the foreign website files cannot be downloaded)
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
Note: The complete content of the configuration file is more complicated. In order to better learn DaemonSet, only the most important content is kept here.
① The syntax and structure of the DaemonSet configuration file are almost the same as those of the Deployment, except that the kind is set to DaemonSet.
② hostName specifies that the Pod directly uses the Node network, which is equivalent to docker run - -network=host. This requirement is reasonable given that flannel needs to provide network connectivity to the cluster.
③ containers defines two containers running the flannel service.
Let's analyze another DaemonSet: kube-proxy.
5.2.2 be a proxy
Since the YAML file of kube-proxy cannot be obtained, you can only run the following command to view the configuration:
kubectl edit daemonset kube-proxy --namespace=kube-system
The result is shown in the figure.
Also for ease of understanding, only the most important information is kept here.
① kind: DaemonSet specifies that this is a DaemonSet type resource.
② containers define the container of kube-proxy.
③ status is the runtime status of the current DaemonSet, this part is unique to kubectl edit. In fact, each currently running resource in the Kubernetes cluster can view its configuration and running status through kubectl edit, such as kubectl edit deployment nginx-deployment.
5.2.3 Running your own DaemonSet
This section takes Prometheus Node Exporter as an example to demonstrate how users can run their own DaemonSet.
Prometheus is a popular system monitoring solution. Node Exporter is an agent of Prometheus, which runs on each monitored node in the form of Daemon.
If you run the Node Exporter container directly in Docker, the command is:
docker run -d \
-v "/proc:/host/proc" \
-v "/sys:/host/sys" \
-v "/:/rootfs" \
--net=host \
prom/node-exporter \
--path.procfs /host/proc \
--path.sysfs /host/sys \
--collector.filesystem.ignored-mount-points "^/(sys|proc|dev|host|etc)($|/)"
Convert it to DaemonSet's YAML configuration file node_exporter.yml as shown below.
[root@k8s-master ~]# cat node_exporter.yml
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: node-exporter-daemonset
spec:
selector:
matchLabels:
app: prometheus
template:
metadata:
labels:
app: prometheus
spec:
hostNetwork: true
containers:
- name: node-exporter
image: prom/node-exporter
imagePullPolicy: IfNotPresent
command:
- /bin/node_exporter
- --path.procfs
- /host/proc
- --path.sysfs
- /host/sys
- --collector.filesystem.ignored-mount-points
- ^/(sys|proc|dev|host|etc)($|/)
volumeMounts:
- name: proc
mountPath: /host/proc
- name: sys
mountPath: /host/sys
- name: root
mountPath: /rootfs
volumes:
- name: proc
hostPath:
path: /proc
- name: sys
hostPath:
path: /sys
- name: root
hostPath:
path: /
[root@k8s-master ~]#
① Use the host's network directly.
② Set the container startup command.
③ Map the Host path /proc, /sys and / to the container through Volume. We will discuss Volume in detail later.
Execute kubectl apply -f node_exporter.yml, as shown in the figure.
DaemonSet node-exporter-daemonset is successfully deployed, and a node exporter Pod is running on k8s-node1 and k8s-node2 respectively.