kubernetes: Assigning a pod to a node

2 min read | by Jordi Prats

In a kubernetes cluster not all nodes must be identical, for example, some might have access to a disk that others don't, or belong to a different network segment that do have a public IP thus we might want to assign pods to specific nodes

Let's assume we have the following nodes available:

$ kubectl top nodes
NAME                        CPU(cores)   CPU%   MEMORY(bytes)   MEMORY%
canterbury.pet2cattle.com   254m         9%     1729Mi          5%
scopuli.pet2cattle.com      1047m        1%     1203Mi          15%
anubis.pet2cattle.com       154m         26%    2729Mi          9%

If we want, for whatever reason, to force the scheduling of a pod to a node, for example: scopuli.pet2cattle.com we just need to set nodeName to the node we want to use, for example:

apiVersion: v1
kind: Pod
  name: demo
  nodeName: scopuli.pet2cattle.com
    - name: demo
      image: busybox
      command: ["sleep"]
      args: ["1h"]

If we apply this yaml:

$ kubectl apply -f demo.yaml
pod/demo created

We will see that this pod is going to be scheduled to the pod we told kubernetes to do it. If we add -o wide to kubectl get we will be able to see the node:

$ kubectl get pod demo -o wide
NAME      READY   STATUS    RESTARTS   AGE    IP           NODE                                NOMINATED NODE   READINESS GATES
demo      1/1     Running   0          2d5h   scopuli.lolcathost.systemadmin.es   <none>           <none>

We must take into account that resource requests/limits still apply, as a rule of thumb, if a given pod wouldn't be scheduled to a given node for some reason, nodeName won't force it's scheduling: It just adds another restriction to the resource request

Posted on 04/02/2021