Kubernetes入门实验:daemonset

作者注:本文仅为笔者学习记录,不具任何参考意义。

k8s daemonset 实验。
注:本文为笔者实验记录,非教程,另会不定时更新。

环境

1
2
3
4
5
# kubectl get node
NAME STATUS ROLES AGE VERSION
edge-node Ready <none> 15m v1.17.0
edge-node2 Ready <none> 16m v1.17.0
ubuntu Ready master 67d v1.17.0

daemonset

DaemonSet 确保全部(或者某些)节点上运行一个 Pod 的副本。当有节点加入集群时, 也会为他们新增一个 Pod 。当有节点从集群移除时,这些 Pod 也会被回收。删除 DaemonSet 将会删除它创建的所有 Pod。
DaemonSet 的一些典型用法:

  • 在每个节点上运行集群存储 DaemonSet,例如 glusterd、ceph。
  • 在每个节点上运行日志收集 DaemonSet,例如 fluentd、logstash。
  • 在每个节点上运行监控 DaemonSet,例如 Prometheus Node Exporter、Flowmill、Sysdig 代理、collectd、Dynatrace OneAgent、AppDynamics 代理、Datadog 代理、New Relic 代理、Ganglia gmond 或者 Instana 代理。

技术总结

适用守护进程,每个节点只一份(也可指定某一些节点,下同),自适应,新加节点则加,反之亦然。
守护进程可直接在物理节点上运行(如通过systemd)。
ds与deploy相似,适应于无状态服务。但Deploy只关注提供的服务,不关心服务其哪个节点上。而ds要确认每节点都有一副本,并且一般ds先启动,再到其它pod。

测试yaml文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# vim busybox-ds.yaml

apiVersion: apps/v1
kind: DaemonSet # 声明DaemonSet
metadata:
name: busybox-ds
spec:
selector:
matchLabels:
app: busybox
release: stable
template:
metadata:
labels:
app: busybox
release: stable
spec:
containers:
- name: busybox
image: latelee/busybox
imagePullPolicy: IfNotPresent
command:
- sleep
- "3600"

创建:

1
kubectl apply -f busybox-ds.yaml

查看:

1
2
3
4
5

# kubectl get ds
NAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE NODE SELECTOR AGE
busybox-ds 2 2 2 2 2 <none> 3m35s

查看:

1
# kubectl describe ds

运行在指定的 node 上。

1
2
# 添加节点的标签
kubectl label nodes edge-node ntype=hello

在yaml中指定nodeSelector:

1
2
3
4
5
6
7
8
9
10
spec:
containers:
- name: busybox
image: latelee/busybox
imagePullPolicy: IfNotPresent
command:
- sleep
- "3600"
nodeSelector:
ntype: hello

创建资源,验证只在指定label节点上运行。

删除:

1
2
3
kubectl delete ds busybox-ds

kubectl delete -f busybox-ds.yaml

注:删除pod会自动重建。删除ds,所有的pod都被删除。

参考:
https://kubernetes.io/zh/docs/concepts/workloads/controllers/daemonset/

附:
为了预先将镜像下载到每个节点的demo:

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
apiVersion: apps/v1
kind: DaemonSet # 声明DaemonSet
metadata:
name: mysql-ds
spec:
selector:
matchLabels:
app: mysql
release: stable
template:
metadata:
labels:
app: mysql
release: stable
spec:
containers:
- name: mysql
image: mysql:5.7
imagePullPolicy: IfNotPresent

---

apiVersion: apps/v1
kind: DaemonSet # 声明DaemonSet
metadata:
name: wordpress-ds
spec:
selector:
matchLabels:
app: wordpress
release: stable
template:
metadata:
labels:
app: wordpress
release: stable
spec:
containers:
- name: wordpress
image: wordpress
imagePullPolicy: IfNotPresent

---

apiVersion: apps/v1
kind: DaemonSet # 声明DaemonSet
metadata:
name: redis-ds
spec:
selector:
matchLabels:
app: redis
release: stable
template:
metadata:
labels:
app: redis
release: stable
spec:
containers:
- name: redis
image: redis:alpine #latelee/redis
imagePullPolicy: IfNotPresent