DaemonSet控制器是用于保证在集群上精确运行的负载有多少个实例,如有10个节点那么其就会在每个节点上只运行一个。

DaemonSet可以实现在集群内所有节点上只运行一个,也可以在集群内拥有某个标签的节点上运行一个。

DaemonSet可以简称为DS.

1
2
3
4
5
# 获取当前系统上kube-system名称空间下的ds资源
root@k8s-master01:~/yaml/chapter08# kubectl get ds -n kube-system
NAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE NODE SELECTOR AGE
kube-flannel-ds 4 4 4 4 4 <none> 6d23h
kube-proxy 4 4 4 4 4 kubernetes.io/os=linux 6d23h

DaemonSet资源定义规范

1
2
3
4
5
6
7
8
9
10
11
12
13
14
apiVersion: apps/v1  # API群组及版本
kind: DaemonSet # 资源类型特有标识
metadata:
name <string> # 资源名称,在作用域中要唯一
namespace <string> # 名称空间;DaemonSet资源隶属名称空间级别
spec:
minReadySeconds <integer> # Pod就绪后多少秒内任一容器无crash方可视为“就绪”
selector <object> # 标签选择器,必须匹配template字段中Pod模板中的标签
template <object> # Pod模板对象;
revisionHistoryLimit <integer> # 滚动更新历史记录数量,默认为10;
updateStrategy <Object> # 滚动更新策略
type <string> # 滚动更新类型,可用值有OnDelete和RollingUpdate;
rollingUpdate <Object> # 滚动更新参数,专用于RollingUpdate类型
maxUnavailable <string> # 更新期间可比期望的Pod数量缺少的数量或比例

DaemonSet示例

1.编写资源清单

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
31
32
33
34
35
36
37
38
39
40
root@k8s-master01:~/yaml/chapter08# vim daemonset-demo.yaml
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: daemonset-demo
namespace: default
labels:
app: prometheus
component: node-exporter
spec:
selector:
matchLabels:
app: prometheus
component: node-exporter
template:
metadata:
name: prometheus-node-exporter
labels:
app: prometheus
component: node-exporter
spec:
containers:
- name: promethous-node-exporter
image: prom/node-exporter:v0.18.0
ports:
- name: prom-node-exp
containerPort: 9100
hostPort: 9100
livenessProbe:
tcpSocket:
port: prom-node-exp
initialDelaySeconds: 3
readinessProbe:
httpGet:
path: '/metrics'
port: prom-node-exp
scheme: HTTP
initialDelaySeconds: 5
hostNetwork: true
hostPID: true

2.应用配置清单

1
2
3
4
5
6
7
8
9
root@k8s-master01:~/yaml/chapter08# kubectl apply -f daemonset-demo.yaml
daemonset.apps/daemonset-demo created

root@k8s-master01:~/yaml/chapter08# kubectl get pods -l 'app=prometheus' -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
daemonset-demo-6qqmt 1/1 Running 0 4m5s 172.16.11.81 k8s-node01 <none> <none>
daemonset-demo-fvkhj 1/1 Running 0 4m5s 172.16.11.83 k8s-node03 <none> <none>
daemonset-demo-pxmrd 1/1 Running 0 4m5s 172.16.11.82 k8s-node02 <none> <none>
# 已经在每个node节点部署上了

3.测试访问

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
root@k8s-master01:~/yaml/chapter08# curl 172.16.11.81:9100/metrics
# HELP go_gc_duration_seconds A summary of the GC invocation durations.
# TYPE go_gc_duration_seconds summary
go_gc_duration_seconds{quantile="0"} 3.5862e-05
go_gc_duration_seconds{quantile="0.25"} 9.0353e-05
go_gc_duration_seconds{quantile="0.5"} 0.000111509
go_gc_duration_seconds{quantile="0.75"} 0.000156903
go_gc_duration_seconds{quantile="1"} 0.003124382
go_gc_duration_seconds_sum 0.006404565
go_gc_duration_seconds_count 25
# HELP go_goroutines Number of goroutines that currently exist.
# TYPE go_goroutines gauge
go_goroutines 6
# HELP go_info Information about the Go environment.
# TYPE go_info gauge
go_info{version="go1.12.5"} 1
# HELP go_memstats_alloc_bytes Number of bytes allocated and still in use.
# TYPE go_memstats_alloc_bytes gauge
go_memstats_alloc_bytes 2.99224e+06
# HELP go_memstats_alloc_bytes_total Total number of bytes allocated, even if freed.
# TYPE go_memstats_alloc_bytes_total counter
go_memstats_alloc_bytes_total 6.2666904e+07
# HELP go_memstats_buck_hash_sys_bytes Number of bytes used by the profiling bucket hash table.
# TYPE go_memstats_buck_hash_sys_bytes gauge
go_memstats_buck_hash_sys_bytes 1.465696e+06