k8s控制器:Daemonset


k8s控制器:Daemonset

一、DaemonSet概述

1.1、DaemonSet概述

DaemonSet控制器能夠確保k8s集群所有的節點都運行一個相同的pod副本,當向k8s集群中增加node節點時,這個node節點也會自動創建一個pod副本,當node節點從集群移除,這些pod也會自動刪除;刪除Daemonset也會刪除它們創建的pod

1.2、DaemonSet工作原理

daemonset的控制器會監聽kuberntes的daemonset對象、pod對象、node對象,這些被監聽的對象之變動,就會觸發syncLoop循環讓kubernetes集群朝着daemonset對象描述的狀態進行演進。

1.3、Daemonset典型的應用場景

1)在集群的每個節點上運行存儲,比如:glusterd 或 ceph。

2)在每個節點上運行日志收集組件,比如:flunentd 、 logstash、filebeat等。

3)在每個節點上運行監控組件,比如:Prometheus、 Node Exporter 、collectd等

1.4、DaemonSet 與 Deployment 的區別

Deployment 部署的副本 Pod 會分布在各個 Node 上,每個 Node 都可能運行好幾個副本。

DaemonSet 的不同之處在於:每個 Node 上最多只能運行一個副本

二、DaemonSet使用案例:部署日志收集組件fluentd

# 編寫一個DaemonSet資源清單
[root@k8s-master1 ~]# cat 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:
      - key: node-role.kubernetes.io/master
        effect: NoSchedule
      containers:
      - name: fluentd-elasticsearch
        image: xianchao/fluentd:v2.5.1
        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

[root@k8s-master1 ~]# kubectl apply -f daemonset.yaml 
daemonset.apps/fluentd-elasticsearch created

[root@k8s-master1 ~]# kubectl get ds -n kube-system
NAME                    DESIRED   CURRENT   READY   UP-TO-DATE   AVAILABLE   NODE SELECTOR            AGE
calico-node             3         3         3       3            3           kubernetes.io/os=linux   2d12h
fluentd-elasticsearch   3         3         3       3            3           <none>                   8s
kube-proxy              3         3         3       3            3           kubernetes.io/os=linux   2d13h

[root@k8s-master1 ~]# kubectl get pods -n kube-system -o wide -l name=fluentd-elasticsearch
NAME                          READY   STATUS    RESTARTS   AGE   IP               NODE          NOMINATED NODE   READINESS GATES
fluentd-elasticsearch-9hjqc   1/1     Running   0          83s   10.244.159.134   k8s-master1   <none>           <none>
fluentd-elasticsearch-bdfs6   1/1     Running   0          83s   10.244.36.124    k8s-node1     <none>           <none>
fluentd-elasticsearch-wh44b   1/1     Running   0          83s   10.244.169.152   k8s-node2     <none>           <none>

# 鏡像更新
# kubectl set image daemonsets fluentd-elasticsearch fluentd-elasticsearch=image_name -n kube-system


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM