k8s daemonset 实验。
注:本文为笔者实验记录。
环境
# 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文件
# 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"
创建:
kubectl apply -f busybox-ds.yaml
查看:
# kubectl get ds
NAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE NODE SELECTOR AGE
busybox-ds 2 2 2 2 2 <none> 3m35s
查看:
# kubectl describe ds
运行在指定的 node 上。
# 添加节点的标签
kubectl label nodes edge-node ntype=hello
在yaml中指定nodeSelector:
spec:
containers:
- name: busybox
image: latelee/busybox
imagePullPolicy: IfNotPresent
command:
- sleep
- "3600"
nodeSelector:
ntype: hello
创建资源,验证只在指定label节点上运行。
删除:
kubectl delete ds busybox-ds
或
kubectl delete -f busybox-ds.yaml
注:删除pod会自动重建。删除ds,所有的pod都被删除。
参考:
kubernetes.io/zh/docs/con…
附: 为了预先将镜像下载到每个节点的demo:
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