How to Schedule Kubernetes Pods Using Node Affinity
The DevOpscube team is running a Kubernetes cluster for a large e-commerce platform. The payment gateway service is latency-sensitive and must run only on nodes specifically designated for this workload.
A worker node has been selected for the payment gateway workload and needs to be labeled accordingly.
You task is to Configure the cluster so that the payment gateway workload is scheduled only onto the designated node.
Create a Deployment named devopscube-payment-gw using the nginx image with 3 replicas.
Configure the required node affinity using requiredDuringSchedulingIgnoredDuringExecution so that all Pods are scheduled onto the node labeled with payment-gateway=enabled
Use the appropriate node name from your cluster when applying the configuration.
After deploying the application, verify that:
- The designated node has the required label.
- All 3 Pods are in the Running state.
- All 3 Pods are scheduled on the designated node.
- The Deployment is successfully managing all 3 replicas.
Node Affinity, refer to the official Kubernetes documentation.Label the Node
Add the payment-gateway=enabled label to node01:
$ kubectl label node node01 payment-gateway=enabled
node/node01 labeledVerify the label:
$ kubectl get node node01 --show-labels
NAME STATUS ROLES AGE VERSION LABELS
node01 Ready worker 9d v1.37.0 beta.kubernetes.io/arch=arm64,beta.kubernetes.io/os=linux,kubernetes.io/arch=arm64,kubernetes.io/hostname=node01,kubernetes.io/os=linux,node-role.kubernetes.io/worker=worker,payment-gateway=enabledThe payment-gateway=enabled label identifies node01 as a node suitable for the payment gateway workload.
Create the Deployment Manifest
Generate the Deployment manifest with 3 replicas:
$ kubectl create deployment devopscube-payment-gw \
--image=nginx \
--replicas=3 \
--dry-run=client -o yaml > deploy.yamlEdit deploy.yaml and add the required node affinity under spec.template.spec. This ensures the Pods are scheduled only on nodes with the payment-gateway=enabled label:
WhyrequiredDuringSchedulingIgnoredDuringExecution?
requiredDuringSchedulingIgnoredDuringExecution makes the node affinity a hard scheduling requirement.
The Pod can only be scheduled on a node whose labels match:
payment-gateway: enabledIf no matching node is available, the Pod remains Pending rather than being scheduled elsewhere.
Important:nodeAffinityuses node labels, not the node name directly. The labelpayment-gateway=enabledis what determines where the Pods can be scheduled.
Apply the Deployment
Apply the manifest to create the Deployment:
$ kubectl apply -f deploy.yaml
deployment.apps/devopscube-payment-gw createdCheck the Pods and their node placement:
$ kubectl get pods -l app=devopscube-payment-gw -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
devopscube-payment-gw-7bbf98d4c-5mdc2 1/1 Running 0 73s 10.244.196.183 node01 <none> <none>
devopscube-payment-gw-7bbf98d4c-fdwcx 1/1 Running 0 73s 10.244.196.182 node01 <none> <none>
devopscube-payment-gw-7bbf98d4c-nfh24 1/1 Running 0 73s 10.244.196.181 node01 <none> <none>You should see 3 Pods in Running state, with node01 shown in the NODE column. This confirms that the node affinity is working correctly.
Verify the Affinity
Check the Deployment to confirm that the required node affinity is configured correctly:
$ kubectl get deployment devopscube-payment-gw \
-o jsonpath='{.spec.template.spec.affinity.nodeAffinity.requiredDuringSchedulingIgnoredDuringExecution}{"\n"}'
{"nodeSelectorTerms":[{"matchExpressions":[{"key":"payment-gateway","operator":"In","values":["enabled"]}]}]}This confirms that the Pods require a node with the payment-gateway=enabled label.
Delete the Deployment
After verification, delete the Deployment:
kubectl delete -f deploy.yamlRemove the Node Label
Remove the payment-gateway label from node01:
kubectl label node node01 payment-gateway-Verify the label has been removed:
$ kubectl get node node01 --show-labels
NAME STATUS ROLES AGE VERSION LABELS
node01 Ready worker 9d v1.37.0 beta.kubernetes.io/arch=arm64,beta.kubernetes.io/os=linux,kubernetes.io/arch=arm64,kubernetes.io/hostname=node01,kubernetes.io/os=linux,node-role.kubernetes.io/worker=workerThe payment-gateway=enabled label should no longer appear.
CKA Exam Tips
These tips will help you quickly identify the important Kubernetes concepts and avoid common mistakes during the exam.
1. Know the Two Node Affinity Types
requiredDuringSchedulingIgnoredDuringExecution is a hard requirement, meaning the Pod must be scheduled on a matching node. preferredDuringSchedulingIgnoredDuringExecution is a soft preference, meaning Kubernetes tries to schedule the Pod on a matching node when possible.
2. Remove a Label Quickly
To remove the payment-gateway label from node01, use:
kubectl label node node01 payment-gateway-