Day19-不是恶魔 介绍DaemonSet

在k8s内有着不同种类的pod组合,ReplicaSet StatefulSets,那麽剩下的DaemonSet到底是什麽呢?
这边就来介绍他。

什麽是DaemonSet

DaemonSet会确保所有(或者某些)nodes上运行着特定功能的pod。当nodes加入cluster时就会在它们上面新增一个pod,当nodes从cluster上被移除时,那些pod也会被移除。如果DaemonSet被删掉的话,pod也会跟着一起被删掉。
下面是某些daemonsets的使用范例:

  1. 在nodes上面运行cluster storage的daemon,例如glusterdceph
  2. 在node上运行收集log的daemon,例如Fluentdlogstash
  3. 在node上运行监控node的daemon,例如Prometheus Node Exporter collectd 或者 Datadog agent等等

DaemonSet范例

在这边使用官方提供的范例,首先建一个yaml名为daemonset.yaml,并将下面内容复制进去。

apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: fluentd-elasticsearch
  namespace: kube-system
  labels:
    k8s-app: fluentd-logging
spec:
  selector:
    matchLabels:
      name: fluentd-elasticsearch
  template:
    metadata:
      labels:
        name: fluentd-elasticsearch
    spec:
      tolerations:
      # this toleration is to have the daemonset runnable on master nodes
      # remove it if your masters can't run pods
      - key: node-role.kubernetes.io/master
        operator: Exists
        effect: NoSchedule
      containers:
      - name: fluentd-elasticsearch
        image: quay.io/fluentd_elasticsearch/fluentd:v2.5.2
        resources:
          limits:
            memory: 200Mi
          requests:
            cpu: 100m
            memory: 200Mi
        volumeMounts:
        - name: varlog
          mountPath: /var/log
        - name: varlibdockercontainers
          mountPath: /var/lib/docker/containers
          readOnly: true
      terminationGracePeriodSeconds: 30
      volumes:
      - name: varlog
        hostPath:
          path: /var/log
      - name: varlibdockercontainers
        hostPath:
          path: /var/lib/docker/containers

然後执行

kubectl apply -f daemonset.yaml

有几点要特别注意,当daemonset建立後,就不建议修改.spec.selector,一但修改了可能会造成daemonset无法认得pod,使pod变成孤儿。以及.spec.selector必须和.spec.template.metadata.labels对的上,不然会被API server 阻挡掉。

要检查是否有建立成功,可以用下面指令确认

kubectl get pods -l name=fluentd-elasticsearch -A

或者想要看到更细的资讯

kubectl get pods -l name=fluentd-elasticsearch -A -o wide

你就可以看到下图
https://ithelp.ithome.com.tw/upload/images/20210926/20129607QybuSbjLMN.png

特别的是,当你直接使用get pods是看不到的,这是因为get pods会去捞名为default的namespace内的资源,但是daemonset不在namespace内,而是在kube-system内,所以你必须用

kubectl get pods -n kube-system

才能够看到你刚刚建立的pod,而namespace也是下一章要讲解的议题


<<:  开放封闭原则 Open-Closed Principle

>>:  Day.19 认识索引 - 二级索引 (Secondary Index)

<Day7>以模拟帐户作示范 — 登入 Shioaji API

● 接下来几章都是先以模拟帐户作登入,尚未使用正式证券户帐户登入 如果尚未有永丰金证券帐户的朋友,但...

Day 30 - [结論] 总整理与建议

台湾老年人口的比例越来越高,随之而来的便是民众对长照的需求。政府提出了长照 2.0 的计画,资源与服...

Day 3 情报收集 - Information Gathering

在开始正式进入主题前,觉得可能有必要宣导一下国内刑法规范,根据全国法规资料库所查到的相关资料 刑法第...

Day_12 有线网路应用(四)

如果没有USB网卡但也想要扩充LAN网口数量呢? 那麽vlan这个强大功能一定要了解一下。 虚拟区域...

[DAY19]Ingress-k8s的海姆达尔

还有印象雷神索尔里面,管理着彩虹桥的海姆达尔吗~ 只有人从彩虹桥传送进来时,第一个面对的就是他。 在...