Kubernetes: Dynamically identify node and taint

10/25/2018

I have an application pod which will be deployed on k8s cluster But as Kubernetes scheduler decides on which node this pod needs to run

Now I want to add taint to the node dynamically where my application pod is running with NOschedule so that no new pods will be scheduled on this node

I know that we can use kubectl taint node with NOschedule if I know the node name but I want to achieve this dynamically based on which node this application pod is running

The reason why I want to do this is this is critical application pod which shouldn’t have down time and for good reasons I have only 1 pod for this application across the cluster

Please suggest

-- shiv455
kubernetes

2 Answers

10/25/2018

You can get the node where your pod is running with something like this:

$ kubectl get pod myapp-pod -o=jsonpath='{.spec.nodeName}'

Then you can taint it:

$ kubectl taint nodes <node-name-from-above> key=value:NoSchedule

or the whole thing in one command:

$ kubectl taint nodes $(kubectl get pod myapp-pod -o=jsonpath='{.spec.nodeName}') key=value:NoSchedule
-- Rico
Source: StackOverflow

10/25/2018

In addition to @Rico answer.

You can use feature called node affinity, this is still a beta but some functionality is already implemented.

You should add a label to your node, for example test-node-affinity: test. Once this is done you can Add the nodeAffinity of field affinity in the PodSpec.

spec:
...
  affinity:
    nodeAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
        nodeSelectorTerms:
        - matchExpressions:
          - key: test-node-affinity
            operator: In
            values:
            - test

This will mean the POD will look for a node with key test-node-affinity and value test and will be deployed there.

I recommend reading this blog Taints and tolerations, pod and node affinities demystified by Toader Sebastian.

Also familiarise yourself with Taints and Tolerations from Kubernetes docs.

-- Crou
Source: StackOverflow