DaemonSet 是什么
DaemonSet: 守护进程集,缩写为 ds,在所有节点或者是匹配的节点上部署一个 Pod。
使用 DaemonSet 的场景:
- 运行集群存储的 daemon:比如 ceph,glusterd
- 节点的 CNI 网络插件:Calico
- 节点日志的收集,fluentd 或者 filebeat
- 节点的监控:node exporter
- 服务暴露:部署一个 ingress nginx
每个节点创建一个 DaemonSet
创建一个 DaemonSet 示例 nginx-ds.yaml 文件,如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22apiVersion: apps/v1
kind: DaemonSet
metadata:
labels:
app: nginx
name: nginx
spec:
revisionHistoryLimit: 10
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.15.2
imagePullPolicy: IfNotPresent
restartPolicy: Always
terminationGracePeriodSeconds: 30创建一个 ds
1
2# kubectl create -f nginx-ds.yaml
daemonset.apps/nginx created查看节点信息以及 ds 生成的 Pod 信息
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15# kubectl get nodes -owide
NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME
k8s-master-01 Ready master 57d v1.19.7 192.168.200.18 <none> CentOS Linux 7 (Core) 4.19.12-1.el7.elrepo.x86_64 docker://19.3.15
k8s-master-02 Ready master 57d v1.19.7 192.168.200.19 <none> CentOS Linux 7 (Core) 4.19.12-1.el7.elrepo.x86_64 docker://19.3.15
k8s-master-03 Ready master 57d v1.19.7 192.168.200.20 <none> CentOS Linux 7 (Core) 4.19.12-1.el7.elrepo.x86_64 docker://19.3.15
k8s-node-01 Ready node 57d v1.19.7 192.168.200.21 <none> CentOS Linux 7 (Core) 4.19.12-1.el7.elrepo.x86_64 docker://19.3.15
k8s-node-02 Ready node 57d v1.19.7 192.168.200.22 <none> CentOS Linux 7 (Core) 4.19.12-1.el7.elrepo.x86_64 docker://19.3.15
# kubectl get pod -owide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-7vhgv 1/1 Running 0 66s 172.19.98.152 k8s-node-02 <none> <none>
nginx-949mg 1/1 Running 0 66s 172.30.107.90 k8s-master-01 <none> <none>
nginx-jz5pl 1/1 Running 0 66s 172.21.176.218 k8s-master-02 <none> <none>
nginx-lzpwj 1/1 Running 0 66s 172.20.183.153 k8s-master-03 <none> <none>
nginx-pvg2p 1/1 Running 0 66s 172.31.44.33 k8s-node-01 <none> <none>
按照标签选择器部署 DS
给所有的 node 节点添加一个
ds=true
的标签1
2
3# kubectl label node k8s-node-01 k8s-node-02 ds=true
node/k8s-node-01 labeled
node/k8s-node-02 labeled查看节点信息
1
2
3
4
5
6
7# kubectl get nodes --show-labels
NAME STATUS ROLES AGE VERSION LABELS
k8s-master-01 Ready master 57d v1.19.7 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-master-01,kubernetes.io/os=linux,node-role.kubernetes.io/master=,node.kubernetes.io/node=
k8s-master-02 Ready master 57d v1.19.7 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-master-02,kubernetes.io/os=linux,node-role.kubernetes.io/master=,node.kubernetes.io/node=
k8s-master-03 Ready master 57d v1.19.7 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-master-03,kubernetes.io/os=linux,node-role.kubernetes.io/master=,node.kubernetes.io/node=
k8s-node-01 Ready node 57d v1.19.7 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,ds=true,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-node-01,kubernetes.io/os=linux,node-role.kubernetes.io/node=,node.kubernetes.io/node=
k8s-node-02 Ready node 57d v1.19.7 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,ds=true,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-node-02,kubernetes.io/os=linux,node-role.kubernetes.io/node=,node.kubernetes.io/node=修改 nginx-ds.yaml 文件在文件最下面添加 nodeSelector 属性,如下。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31apiVersion: 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:
- name: nginx
image: nginx:1.15.2
imagePullPolicy: IfNotPresent
resources: {}
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
dnsPolicy: ClusterFirst
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
terminationGracePeriodSeconds: 30
nodeSelector:
ds: "true"使用 replace 命令更新 ds
1
2# kubectl replace -f nginx-ds.yaml
daemonset.apps/nginx replaced再次查看运行的 Pod
1
2
3
4
5
6
7
8
9# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-5kgxb 1/1 Running 0 65s
nginx-6522j 1/1 Running 0 58s
# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-5kgxb 1/1 Running 0 2m18s 172.31.44.34 k8s-node-01 <none> <none>
nginx-6522j 1/1 Running 0 2m11s 172.19.98.153 k8s-node-02 <none> <none>
DaemonSet 扩容
新节点运行 ds,比如需要在 k8s-master-01 节点上部署 ds,只需要给 k8s-master-01 节点添加一个 ds=true 的标签即可
1
2# kubectl label nodes k8s-master-01 ds=true
node/k8s-master-01 labeled查看最新的 Pod 部署状态
1
2
3
4
5# kubectl get pods -owide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-5kgxb 1/1 Running 0 6m52s 172.31.44.34 k8s-node-01 <none> <none>
nginx-6522j 1/1 Running 0 6m45s 172.19.98.153 k8s-node-02 <none> <none>
nginx-vqdnv 1/1 Running 0 31s 172.30.107.91 k8s-master-01 <none> <none>
DaemonSet 的更新与回滚
StatefulSet,DaemonSet 的更新和回滚和 Deployment 一致;