BuildOn pod NodeAffinity

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> 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:

kubectl run -i --tty ubuntu --rm --image=ubuntu --restart=Never --overrides='

{

   "apiVersion": "v1",

   "kind": "Pod",

   "metadata": {

      "name": "ubuntupoc"

   },

   "spec": {

      "affinity": {

         "nodeAffinity": {

            "requiredDuringSchedulingIgnoredDuringExecution": {

               "nodeSelectorTerms": [

                  {

                     "matchExpressions": [

                        {

                           "key": "labelnode1",

                           "operator": "In",

                           "values": [

                              "nodelabel"

                           ]

                        }

                     ]

                  }

               ]

            }

         }

      }

   }

}

'

===========================================================================

Enable Node Affinity for buildon pod. Change buildon.py "kubectl run" step as shown below.

===========================================================================

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": "labelnode1", "operator": "In", "values": [ "nodelabel"]}]}]}}}}}' --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/