Kubernetes: how to assign pods to all nodes with sepcific label

8/10/2018

I want to run certain job on every single node in specific node groups.
Can kubernetes do the same thing like Swarm global mode?

-- Milton
docker
kubernetes

2 Answers

8/10/2018

You're looking for a DaemonSet.

-- David Maze
Source: StackOverflow

8/10/2018

To complete @David Maze answer:

A DaemonSet is used to create a Pod on each Node. More information here.

Example:

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

If you want to schedule Pods not on every Node, you can use Taints and Tolerations concept. Taints and tolerations work together to ensure that pods are not scheduled onto inappropriate nodes. For more information, look through the link.

For example:

You can add a Taint to a Node:

kubectl taint nodes <Node_name> key=value:NoSchedule

After that, Pods will have no opportunity to schedule on that Node, even from a DaemonSet. You can add toleration to a Pod (or to a DaemonSet in your case) to allow it schedule on the Node with the toleration:

tolerations:
- key: "key"
  operator: "Equal"
  value: "value"
  effect: "NoSchedule"
-- Artem Golenyaev
Source: StackOverflow