Assigning a Kubernetes Pod to a Specific Node

Introduction

Kubernetes scheduling usually doesn’t need much help in order to determine which node to run a pod on. However, you may occasionally wish to have a little more control. In this lab, you will be able to practice the process of ensuring a pod runs on a specific node.

Solution

Log in to the lab server using the credentials provided:

ssh cloud_user@<PUBLIC_IP_ADDRESS>

Configure the auth-gateway Pod to Only Run on k8s-worker2

  1. Attach a label to k8s-worker2.

    kubectl label nodes k8s-worker2 external-auth-services=true
    
  2. Open auth-gateway.yml:

    vi auth-gateway.yml
    
  3. Add a nodeSelector to the auth-gateway pod descriptor:

    ...
    
    spec:
      nodeSelector:
        external-auth-services: "true"
    
      ...
    
  4. Save and exit the file by pressing Escape followed by :wq.

  5. Delete and re-create the pod:

    kubectl delete pod auth-gateway -n beebox-auth
    
    kubectl create -f auth-gateway.yml
    
  6. Verify the pod is scheduled on the k8s-worker2 node:

    kubectl get pod auth-gateway -n beebox-auth -o wide
    

Configure the auth-data Deployment’s Replica Pods to Only Run on k8s-worker2

  1. Open auth-data.yml:

    vi auth-data.yml
    
  2. Add a nodeSelector to the pod template in the deployment spec (it will be the second spec in the file):

    ...
    
    spec:
    
      ...
    
      template:
    
        ...
    
        spec:
          nodeSelector:
            external-auth-services: "true"
    
          ...
    
  3. Save and exit the file by pressing Escape followed by :wq.

  4. Update the deployment:

    kubectl apply -f auth-data.yml
    
  5. Verify the deployment’s replicas are all running on k8s-worker2:

    kubectl get pods -n beebox-auth -o wide