ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、视频、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
[TOC] # DaemonSet ### 用途 用来确保每个节点(或者指定部分节点),在节点上运行一个Pod副本,这个副本随着节点的加入,自动创建;若节点被移除,Pod也将会被移除。 **应用场景:** * 运行集群分布式存储,例如在每个 Node 上运行 glusterd、ceph节点 * 在每个 Node 上运行日志收集 daemon,例如fluentd、logstash * 在每个 Node 上运行监控 daemon,例如 Prometheus Node ### 组成结构详解 ~~~ ##########常规字段,省略了############ apiVersion: apps/v1 kind: DaemonSet metadata: name: example namespace: kube-system ##################################### #===========Pod模版部分=============== spec: selector: #如果指定了 .spec.selector,必须与.spec.template.metadata.labels 相匹配 matchLabels: name: example #matchExpressions:这个可以自定义更加复杂的Selector #key: value #如果 matchLabels 和 matchExpressions 都指定, #则两个结果之间为 AND 关系 template: # 必填字段 metadata: labels: name: example #这里的labels 必须要和 .spec.selector相匹配, #这个例子中,都是 name: example #++++++++++++部署容器设置部分++++++++++++++ spec: containers: - name: fluentd-elasticsearch image: k8s.gcr.io/fluentd-elasticsearch:1.20 volumeMounts: - name: varlog mountPath: /var/log - name: varlibdockercontainers mountPath: /var/lib/docker/containers readOnly: true volumes: - name: varlog hostPath: path: /var/log - name: varlibdockercontainers hostPath: path: /var/lib/docker/containers ~~~ ### 调度节点的选择 一共分为三种方式选择调度节点: * nodeSelector 根据 Node label 调度指定节点 * nodeAffinity 多种条件判断调度,支持集合操作 * podAffinity 调度到可以满足Pod条件的节点上 #### nodeSelector方式 首先给node打上标签 ~~~ kubectl label nodes node-01 disktype=ssd ~~~ 指定调度到 disktype=ssd 的节点 ~~~ spec: nodeSelector: disktype: ssd ~~~ #### nodeAffinity方式 nodeAffinity提供两种条件选择方式: * 必须满足条件 requiredDuringSchedulingIgnoredDuringExecution * 优选条件 preferredDuringSchedulingIgnoredDuringExecution ##### 示例 调度到包含标签key-1并且值为aaa或bbb的Node上**并且**优选带有标签key-2=ccc的Node(权重+1) ~~~ apiVersion: v1 kind: Pod metadata: name: example spec: affinity: nodeAffinity: requiredDuringSchedulingIgnoredDuringExecution: nodeSelectorTerms: - matchExpressions: - key: key-1 operator: In values: - aaa - bbb preferredDuringSchedulingIgnoredDuringExecution: #可以设置多个优选条件,不同的优选条件可以设置不一样的权重 - weight: 1 preference: matchExpressions: - key: key-2 operator: In values: - ccc containers: - name: with-node-affinity image: gcr.io/google_containers/pause:2.0 ~~~ #### podAffinity方式 podAffinity 提供两种条件选择方式: * 调度到某一条件的节点 podAffinity * 不调度到某一条件的节点 podAntiAffinity ##### 示例 * 如果一个 “ Node所在Zone中包含至少一个带有security=S1标签且运行中的Pod ” ,那么可以调度到该Node * 不调度到“包含至少一个带有security=S2标签且运行中Pod”的Node上 ~~~ apiVersion: v1 kind: Pod metadata: name: with-pod-affinity spec: affinity: podAffinity: requiredDuringSchedulingIgnoredDuringExecution: - labelSelector: matchExpressions: - key: security operator: In values: - S1 topologyKey: failure-domain.beta.kubernetes.io/zone podAntiAffinity: preferredDuringSchedulingIgnoredDuringExecution: - weight: 100 podAffinityTerm: labelSelector: matchExpressions: - key: security operator: In values: - S2 topologyKey: kubernetes.io/hostname containers: - name: with-pod-affinity image: gcr.io/google_containers/pause:2.0 ~~~