> ## Content Index
> Fetch the complete content index at: https://skills.devopscube.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# How to Schedule Kubernetes Pods Using Node Affinity
- URL: https://skills.devopscube.com/schedule-pods-using-node-affinity/
- Published: 2026-09-17T14:49:01.000Z
- Updated: 2026-10-14T14:13:30.000Z
- Author: Sulaiman
- Tags: #CKA-Exam-question, #blog, #docs, Workloads & Scheduling

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.

📚

For more details about `Node Affinity`, refer to the official [Kubernetes documentation.](https://kubernetes.io/docs/tasks/configure-pod-container/assign-pods-nodes-using-node-affinity/?ref=skills.devopscube.com)

---

### Label the Node

Add the `payment-gateway=enabled` label to `node01`:

```sh
$ kubectl label node node01 payment-gateway=enabled

node/node01 labeled
```

Verify the label:

```sh
$ 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=enabled
```

The `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:

```sh
$ kubectl create deployment devopscube-payment-gw \
  --image=nginx \
  --replicas=3 \
  --dry-run=client -o yaml > deploy.yaml
```

Edit `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:

deploy.yaml Copy 

apiVersion: apps/v1 kind: Deployment metadata:  name: devopscube-payment-gw spec:  replicas: 3  selector:  matchLabels:  app: devopscube-payment-gw  template:  metadata:  labels:  app: devopscube-payment-gw  spec:  containers:  \- name: nginx  image: nginx  affinity:  nodeAffinity:  requiredDuringSchedulingIgnoredDuringExecution:  nodeSelectorTerms:  \- matchExpressions:  \- key: payment-gateway  operator: In  values:  \- enabled 

## Why`requiredDuringSchedulingIgnoredDuringExecution`?

`requiredDuringSchedulingIgnoredDuringExecution` makes the node affinity a hard scheduling requirement.

The Pod can only be scheduled on a node whose labels match:

```
payment-gateway: enabled
```

If no matching node is available, the Pod remains Pending rather than being scheduled elsewhere.

> Important: `nodeAffinity` uses node labels, not the node name directly. The label `payment-gateway=enabled` is what determines where the Pods can be scheduled.

### Apply the Deployment

Apply the manifest to create the Deployment:

```sh
$  kubectl apply -f deploy.yaml

deployment.apps/devopscube-payment-gw created
```

Check the Pods and their node placement:

```sh
$ 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:

```sh
$ 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.

🤔

How to delete the Deployment and remove the label from the node.

### Delete the Deployment

After verification, delete the Deployment:

```sh
kubectl delete -f deploy.yaml
```

### Remove the Node Label

Remove the `payment-gateway` label from `node01`:

```sh
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=worker
```

The `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-
```