K8s - Kubernetes使用详解3(运行DaemonSet样例)

作者: hangge 发布时间: 2019-09-02 浏览: 2773 次 编辑

三、运行 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 运行系统组件。执行如下命令查看:

1
kubectl get daemonset --namespace=kube-system

原文:K8s - Kubernetes使用详解3(运行DaemonSet样例)


(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 容器,命令如下:

1
2
3
4
5
6
7
8
9
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,内容如下:

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
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

1
kubectl apply -f node_exporter.yml


原文链接:https://www.hangge.com/blog/cache/detail_2432.html