首页 > 解决方案 > Kubernetes:如何将 pod 分配给具有特定标签的所有节点

问题描述

我想在特定节点组中的每个节点上运行某些作业。
Kubernetes 可以像 Swarm 全局模式一样做同样的事情吗?

标签: dockerkubernetes

解决方案


要完成@David Maze 的回答:

DaemonSet用于在每个节点上创建一个 Pod。更多信息在这里

例子:

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: k8s.gcr.io/fluentd-elasticsearch:1.20
        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

如果您不想在每个节点上安排 Pod,您可以使用Taints 和 Tolerations概念。污点和容忍一起工作,以确保 Pod 不会被调度到不合适的节点上。有关更多信息,请查看链接

例如:

您可以将污点添加到节点:

kubectl taint nodes <Node_name> key=value:NoSchedule

之后,Pod 将没有机会在该节点上进行调度,即使从 DaemonSet 也是如此。您可以向 Pod(或在您的情况下为 DaemonSet)添加容忍度,以允许它在具有容忍度的节点上调度:

tolerations:
- key: "key"
  operator: "Equal"
  value: "value"
  effect: "NoSchedule"

推荐阅读