In k8s, how to let the nodes choose by itself what kind of pods they would accept

5/9/2018

I want one of my node only accepts some kind of pods. So I wonder, is there a way to make one node only accept those pods with some specific labels?

-- Jasonling
kubernetes

1 Answer

5/9/2018

You have two options:

  1. Node Affinity: property of Pods which attract them to set of nodes.
  2. Taints & Toleration : Taints are opposite of Node Affinity, they allow node to repel set of Pods.

Using Node Affinity

  1. You need to label your nodes: kubectl label nodes node1 mylabel=specialpods

  2. Then when you launch Pods specify the affinity:

apiVersion: v1 kind: Pod metadata: name: mypod spec: affinity: nodeAffinity: requiredDuringSchedulingIgnoredDuringExecution: nodeSelectorTerms: - matchExpressions: - key: mylabel operator: In values: - specialpods containers:

  • name: nginx-container image: nginx

Using Taint & Toleration

Taint & Toleration work together: you taint a node, and then specify the toleration for pod, only those Pods will be scheduled on node whose toleration "matches" taint:

  1. Taint: kubectl taint nodes node1 mytaint=specialpods:NoSchedule

  2. Add toleration in Pod Spec:

apiVersion: v1 kind: Pod metadata: name: mypod spec: tolerations:

  • key: "mytaint" operator: "Equal" value: "specialpods" effect: "NoSchedule" containers:
  • name: nginx-container image: nginx
-- bits
Source: StackOverflow