Kubernetes + Jenkins: how to assign all jenkins slave to one specific node

9/7/2018

In my cluster, I have one node vm1, with label "kubernetes.io/hostname: vm-1". Can I configure to assign all Pod slaves to vm-1 node? I tries to set "Node Selector" in Jenkin > Configuration > cloud but it does not work.

Thanks,

-- Jacky Phuong
jenkins
jenkins-plugins
kubernetes

2 Answers

9/7/2018

All you need to do is specify this in the Deployment of your jenkins slave with nodeAffinity, like so:

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: jenkins-slave
  namespace: ci
  labels:
    app: jenkins
    role: slave
spec:
  selector:
    matchLabels:
      app: jenkins
      role: slave
  template:
    metadata:
      labels:
        app: jenkins
        role: slave
    spec:
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
              - matchExpressions:
                  - key: kubernetes.io/hostname
                    operator: In
                    values:
                      - vm-1

You can see some examples here

However, I am not sure if kubernetes.io/hostname is a valid label to be used when selecting node affinity, maybe you will need to create one, such as role, dedicated or type.

-- Urosh T.
Source: StackOverflow

9/10/2018

Use the Kubernetes plugin yaml syntax and add affinity section as described in https://kubernetes.io/docs/concepts/configuration/assign-pod-node/

-- csanchez
Source: StackOverflow