kubernetes资源清单之DaemonSet

什么是 DaemonSet?

DaemonSet 确保全部(或者某些)节点上运行一个 Pod 的副本。当有节点加入集群时,也会为他们新增一个 Pod 。 当有节点从集群移除时,这些 Pod 也会被回收。删除 DaemonSet 将会删除它创建的所有 Pod。

如何调度 Daemon Pod?

正常情况下,Pod 运行在哪个机器上是由 Kubernetes 调度器来选择的。然而,由 Daemon Controller 创建的 Pod 已经确定了在哪个机器上(Pod 创建时指定了 .spec.nodeName),因此:

1.DaemonSet Controller 并不关心一个节点的 unschedulable 字段。

2.DaemonSet Controller 可以创建 Pod,即使调度器还没有启动,这对集群启动是非常有帮助的。

与 Daemon Pod 通信

与 DaemonSet 中的 Pod 进行通信,几种可能的模式如下:

1.Push:配置 DaemonSet 中的 Pod 向其它 Service 发送更新,例如统计数据库。它们没有客户端。

2.NodeIP 和已知端口:DaemonSet 中的 Pod 可以使用 hostPort,从而可以通过节点 IP 访问到 Pod。客户端能通过某种方法知道节点 IP 列表,并且基于此也可以知道端口。

3.DNS:创建具有相同 Pod Selector 的 Headless Service,然后通过使用 endpoints 资源或从 DNS 检索到多个 A 记录来发现 DaemonSet。

3.Service:创建具有相同 Pod Selector 的 Service,并使用该 Service 随机访问到某个节点上的 daemon(没有办法访问到特定节点)。

DaemonSet的一些典型用法是:

在每个节点上运行集群存储守护程序,例如glusterd,ceph。

在每个节点上运行日志收集守护程序,例如fluentd或logstash。

在每个节点上运行节点监视守护程序,例如Prometheus Node Exporter,Flowmill,Sysdig Agent,collected,Dynatrace OneAgent,AppDynamics Agent,Datadog代理,New Relic代理,Ganglia gmond或Instana代理。

示例

---
apiVersion: apps/v1
kind: DaemonSet
metadata:
   name: fluentd-elasticsearch
   namespace: kube-system
   labels:
     k8s-app: fluentd-logging
spec:
   selector:
     matchLabels:
       name: fluentd
   template:
     metadata:
       labels:
         name: fluentd
     spec:
       tolerations:
       - key: node-role.kubernetes.io/master
         effect: NoSchedule
       containers:
       - name: fluentd-elasticsearch
         image: fluentd:latest
         imagePullPolicy: IfNotPresent
         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

将清单提交给kubernetes集群,将创建定义的DaemonSet及其管理的pod

[root@master kubernetes]# kubectl apply -f DaemonSet.yaml

验证

[root@master kubernetes]# kubectl get pod -n kube-system -l name=fluentd --show-labels
NAME                          READY   STATUS    RESTARTS   AGE   LABELS
fluentd-elasticsearch-dzhxx   1/1     Running   0          11m   controller-revision-hash=758f794ffb,name=fluentd,pod-template-generation=1
fluentd-elasticsearch-npmtr   1/1     Running   0          11m   controller-revision-hash=758f794ffb,name=fluentd,pod-template-generation=1
fluentd-elasticsearch-s782s   1/1     Running   0          11m   controller-revision-hash=758f794ffb,name=fluentd,pod-template-generation=1
[root@master kubernetes]# kubectl get ds -n kube-system -l k8s-app=fluentd-logging --show-labels
NAME                    DESIRED   CURRENT   READY   UP-TO-DATE   AVAILABLE   NODE SELECTOR   AGE   LABELS
fluentd-elasticsearch   3         3         3       3            3           <none>          11m   k8s-app=fluentd-logging
[root@master kubernetes]# kubectl get pod -n kube-system -l name=fluentd -o wide
NAME                          READY   STATUS    RESTARTS   AGE   IP            NODE     NOMINATED NODE   READINESS GATES
fluentd-elasticsearch-dzhxx   1/1     Running   0          12m   10.244.0.5    master   <none>           <none>
fluentd-elasticsearch-npmtr   1/1     Running   0          12m   10.244.1.66   node01   <none>           <none>
fluentd-elasticsearch-s782s   1/1     Running   0          12m   10.244.2.61   node02   <none>           <none>

每个主机上都运行有flentd。并且留意labels和name对应的关系。

猜你喜欢

转载自www.cnblogs.com/mycloudedu/p/12039678.html