=====================================================================
Create label to run the buildon pod in a particular slave node using node affinity.
To get the Create label to run the buildon pod in a particular slave node using node affinity.
To get the slavenodename: kubectl get nodes
To create label for the k8s cluster node: kubectl label nodes <slavenodename> thisnode=slavelabel
To Show label: kubectl get nodes --show-labels | grep slavelabel
To delete label: kubectl label nodes <slavenodename> thisnode-
======================================================================
Below JSON format works fine in --overrides to run the pod in the particular slave node.
====================================================================== labelnode1=nodelabel
To Show label: kubectl get nodes --show-labels | grep slavelabel
To delete label: kubectl label nodes <slavenodename> labelnode1-
Example command using Node Affinity for ubuntu image:
...
"metadata": {
"name": "ubuntutestubuntupoc"
},
"spec": {
"affinity": {
...
"key": "thisnodelabelnode1",
"operator": "In",
"values": [
"slavelabelnodelabel"
]
...
===========================================================================
As-Is:
subprocess.Popen(['kubectl run '+ self.uid +' -i --tty --rm --restart=Never --image='+imagename+ ' --env="commitID='+self.uid+'" --env="branchName='+self.tuid+'" --env="gitURL='+self.httpurl+'" --env="KUBEMASTER='+self.host_ip+'" --env="UPLOADPORT='+self.upload_port+'" --env="scmServiceAccount='+self.scmServiceAccount+'"'], shell=True) |
To Be:
subprocess.Popen(['kubectl run '+ self.uid +' -i --tty --rm --restart=Never --image='+imagename+ ' --overrides='{ "apiVersion": "v1","kind":"Pod", "metadata": {"name": "buildonslave"},"spec": {"affinity": {"nodeAffinity": {"requiredDuringSchedulingIgnoredDuringExecution":{"nodeSelectorTerms": [ { "matchExpressions": [ { "key": "thisnodelabelnode1", "operator": "In", "values": [ "slavelabelnodelabel"]}]}]}}}}}' --env="commitID='+self.uid+'" --env="branchName='+self.tuid+'" --env="gitURL='+self.httpurl+'" --env="KUBEMASTER='+self.host_ip+'" --env="UPLOADPORT='+self.upload_port+'" --env="scmServiceAccount='+self.scmServiceAccount+'"'], shell=True)
Reference: https://kubernetes.io/docs/concepts/configuration/assign-pod-node/