1.概念阐述
DaemonSet:守护进程集,缩写为ds,在所有节点或者是匹配的节点上都部署一个Pod。
DaemonSet确保全部(或者某些)节点上运行一个Pod副本。当有新节点加入集群时,也会为它们新增一个Pod。当节点从集群中移除时,这些Pod也会被回收,删除DaemonSet将会删除它创建的所有Pod
使用DaemonSet的一些典型用法:
运行集群存储daemon(守护进程),例如在每个节点上运行Glusterd、Ceph等
在每个节点运行日志收集daemon,例如Fluentd、Logstash
在每个节点运行监控daemon,比如Prometheus Node Exporter、Collectd、Datadog代理、New Relic代理或 Ganglia gmond
2.创建一个DaemonSet
[root@k8s-master01 ~]# cat > nginx-ds.yaml << EFO
apiVersion: apps/v1
kind: DaemonSet
metadata:
labels:
app: nginx
name: nginx
spec:
revisionHistoryLimit: 10
selector:
matchLabels:
app: nginx
template:
metadata:
creationTimestamp: null
labels:
app: nginx
spec:
containers:
- image: nginx:1.15.2
imagePullPolicy: IfNotPresent
name: nginx
resources: {}
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
dnsPolicy: ClusterFirst
restartPolicy: Alwaysyaml
schedulerName: default-scheduler
securityContext: {}
terminationGracePeriodSeconds: 30
EFO
3.创建一个ds
# 创建一个ds
[root@k8s-master01 ~]# kubectl create -f nginx-ds.yaml
daemonset.apps/nginx created
# 查看ds信息,个个节点都有一个
[root@k8s-master01 ~]# kubectl get node -owide
NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME
k8s-master01 Ready matser 43h v1.20.0 192.168.1.100 <none> CentOS Linux 7 (Core) 4.19.12-1.el7.elrepo.x86_64 docker://19.3.14
k8s-master02 Ready <none> 43h v1.20.0 192.168.1.101 <none> CentOS Linux 7 (Core) 4.19.12-1.el7.elrepo.x86_64 docker://19.3.14
k8s-master03 Ready <none> 43h v1.20.0 192.168.1.102 <none> CentOS Linux 7 (Core) 4.19.12-1.el7.elrepo.x86_64 docker://19.3.14
k8s-node01 Ready <none> 43h v1.20.0 192.168.1.103 <none> CentOS Linux 7 (Core) 4.19.12-1.el7.elrepo.x86_64 docker://19.3.14
k8s-node2 Ready <none> 43h v1.20.0 192.168.1.104 <none> CentOS Linux 7 (Core) 4.19.12-1.el7.elrepo.x86_64 docker://19.3.14
4.给node节点打lable
[root@k8s-master01 ~]# kubectl label node k8s-node01 k8s-node02 ds=true
[root@k8s-master01 ~]# kubectl get node --show-labels ### 查看标签
5.更新
[root@k8s-master01 ~]# cat -n nginx-ds.yaml
1 apiVersion: apps/v1
2 kind: DaemonSet
3 metadata:
4 labels:
5 app: nginx
6 name: nginx
7 spec:
8 revisionHistoryLimit: 10
9 selector:
10 matchLabels:
11 app: nginx
12 template:
13 metadata:
14 creationTimestamp: null
15 labels:
16 app: nginx
17 spec:
18 nodeSelector: # 新增
19 ds: "ture" # 定义我们打label的节点上起DS pod
20 containers:
21 - image: nginx:1.15.2
22 imagePullPolicy: IfNotPresent
23 name: nginx
24 resources: {}
25 terminationMessagePath: /dev/termination-log
26 terminationMessagePolicy: File
27 dnsPolicy: ClusterFirst
28 restartPolicy: Always
29 schedulerName: default-scheduler
30 securityContext: {}
31 terminationGracePeriodSeconds: 30
[root@k8s-master01 ~]# kubectl replace -f nginx-ds.yaml
6.验证查看
[root@k8s-master01 ~]# kubectl get pod -owide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-cpbpb 1/1 Running 0 7m51s 172.16.85.242 k8s-node01 <none> <none>
nginx-hmwxf 1/1 Running 0 7m37s 172.16.169.137 k8s-node2 <none> <none>