三、运行 DaemonSet
1,DaemonSet 与 Deployment 的区别
Deployment 部署的副本 Pod 会分布在各个 Node 上,每个 Node 都可能运行好几个副本。
DaemonSet 的不同之处在于:每个 Node 上最多只能运行一个副本。
2,DaemonSet 的典型应用场景
在集群的每个节点上运行存储 Daemon,比如:glusterd 或 ceph。
在每个节点上运行日志收集 Daemon,比如:flunentd 或 logstash。
在每个节点上运行监控 Daemon,比如:Prometheus Node Exporter或 collectd。
3,k8s 自己的 DaemonSet
(1)其实 Kubernetes 自己就在用 DaemonSet 运行系统组件。执行如下命令查看:
kubectl get daemonset --namespace=kube-system
(2)DaemonSet kube-flannel-ds 和 kube-proxy 分别负责每个节点上运行 flannel 和 kube-proxy 组件。由于我这里是三节点的集群,所以它们各有 3 个副本。
4,运行自己的 DaemonSet
(1)下面以 Prometheus Node Exporter 为例演示如何运行自己的 DaemonSet。
Prometheus 是流行的系统监控方案,Node Exporter是 Prometheus 的 agent,以 Daemon 的形式运行在每个被监控节点上。
(2)如果是直接在 Docker 中运行 Node Exporter容器,命令如下:
docker run -d \
-v "/proc:/host/proc" \
-v "/sys:/host/sys" \
-v "/:/rootfs" \
--net=host \
prom/node-exporter \
--path.procfs /host/proc \
--path.sysfs /host/sys \
--collector.filesystem.ignored-mount-points "^/(sys|proc|dev|host|etc)($|/)"
(3)我们将其转换为 DaemonSet 的 YAML 配置文件node_exporter.yml,内容如下:
apiVersion: extensions/v1beta1
kind: DaemonSet
metadata:
name: node-exporter-daemonset
spec:
template:
metadata:
labels:
app: promtheus
spec:
hostNetwork: true # 直接使用Host网络
containers:
- image: prom/node-exporter
name: node-exporter
imagePullPolicy: IfNotPresent
command: # 设置容器的启动命令
- /bin/node_exporter
- --path.procfs
- /host/proc
- --path.sysfs
- /host/sys
- --collector.filesystem.ignored-mount-points "^/(sys|proc|dev|host|etc)($|/)"
volumeMounts: # 通过 Volume 将 Host 路径 /proc、/sys 和 / 映射到容器中
- name: proc
mountPath: /host/proc
- name: sys
mountPath: /host/sys
- name: root
mountPath: /host/rootfs
volumes:
- name: proc
hostPath:
path: /proc
- name: sys
hostPath:
path: /sys
- name: root
hostPath:
path: /
(4)然后执行 kubectl apply命令进行部署即可,部署后在每一个节点上都会运行了一个 node exporter Pod。
kubectl apply -f node_exporter.yml