How to exclude a node in kubernets using podAntiAffinity?
To exclude a set of nodes when submitting a job in kubernetes.


you should use Node affinity which is conceptually similar to nodeSelector –and will allow you to constrain which nodes your pod is eligible to be scheduled on, based on labels on the node and you should be able to use hostname.

On below example i run a job to 5 completions and all are started on node-01 of my cluster with node affinity defined.

Example : job.yaml

apiVersion: batch/v1
kind: Job
metadata:
  creationTimestamp: null
  labels:
    run: busybox
  name: busybox
spec:
  completions: 5
  template:
    metadata:
      creationTimestamp: null
      labels:
        run: busybox
    spec:
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
            - matchExpressions:
              - key: kubernetes.io/hostname
                operator: In
                values:
                - k8s-node01
      containers:
      - image: busybox
        name: busybox
        resources: {}
      restartPolicy: OnFailure


Result :

ubuntu@k8s-master:~$ kubectl get all -o wide
NAME                READY   STATUS      RESTARTS   AGE     IP               NODE         NOMINATED NODE   READINESS GATES
pod/busybox-2gdp6   0/1     Completed   0          3m20s   192.168.85.204   k8s-node01   &lt;none&gt;           &lt;none&gt;
pod/busybox-l654d   0/1     Completed   0          3m55s   192.168.85.201   k8s-node01   &lt;none&gt;           &lt;none&gt;
pod/busybox-n7dbr   0/1     Completed   0          3m29s   192.168.85.203   k8s-node01   &lt;none&gt;           &lt;none&gt;
pod/busybox-pfc79   0/1     Completed   0          4m11s   192.168.85.200   k8s-node01   &lt;none&gt;           &lt;none&gt;
pod/busybox-r4xln   0/1     Completed   0          3m16s   192.168.85.205   k8s-node01   &lt;none&gt;           &lt;none&gt;

NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE   SELECTOR
service/kubernetes   ClusterIP   10.96.0.1    &lt;none&gt;        443/TCP   31h   &lt;none&gt;

NAME                COMPLETIONS   DURATION   AGE     CONTAINERS   IMAGES    SELECTOR
job.batch/busybox   5/5           60s        4m11s   busybox      busybox   controller-uid=e12f136b-d828-4f7b-a49b-303499496a8a


I had two worker nodes on my cluster on job yaml i defined affinity to node01 only.

$ kubectl get nodes
NAME         STATUS   ROLES    AGE   VERSION
k8s-master   Ready    master   31h   v1.17.2
k8s-node01   Ready    &lt;none&gt;   31h   v1.17.2
k8s-node02   Ready    &lt;none&gt;   31h   v1.17.2