|
| 1 | +--- |
| 2 | +title: Assign Pods to Nodes using Node Affinity |
| 3 | +min-kubernetes-server-version: v1.10 |
| 4 | +content_type: task |
| 5 | +weight: 120 |
| 6 | +--- |
| 7 | + |
| 8 | +<!-- overview --> |
| 9 | +This page shows how to assign a Kubernetes Pod to a particular node using Node Affinity in a |
| 10 | +Kubernetes cluster. |
| 11 | + |
| 12 | + |
| 13 | +## {{% heading "prerequisites" %}} |
| 14 | + |
| 15 | + |
| 16 | +{{< include "task-tutorial-prereqs.md" >}} {{< version-check >}} |
| 17 | + |
| 18 | + |
| 19 | + |
| 20 | +<!-- steps --> |
| 21 | + |
| 22 | +## Add a label to a node |
| 23 | + |
| 24 | +1. List the nodes in your cluster, along with their labels: |
| 25 | + |
| 26 | + ```shell |
| 27 | + kubectl get nodes --show-labels |
| 28 | + ``` |
| 29 | + The output is similar to this: |
| 30 | + |
| 31 | + ```shell |
| 32 | + NAME STATUS ROLES AGE VERSION LABELS |
| 33 | + worker0 Ready <none> 1d v1.13.0 ...,kubernetes.io/hostname=worker0 |
| 34 | + worker1 Ready <none> 1d v1.13.0 ...,kubernetes.io/hostname=worker1 |
| 35 | + worker2 Ready <none> 1d v1.13.0 ...,kubernetes.io/hostname=worker2 |
| 36 | + ``` |
| 37 | +1. Chose one of your nodes, and add a label to it: |
| 38 | + |
| 39 | + ```shell |
| 40 | + kubectl label nodes <your-node-name> disktype=ssd |
| 41 | + ``` |
| 42 | + where `<your-node-name>` is the name of your chosen node. |
| 43 | + |
| 44 | +1. Verify that your chosen node has a `disktype=ssd` label: |
| 45 | + |
| 46 | + ```shell |
| 47 | + kubectl get nodes --show-labels |
| 48 | + ``` |
| 49 | + |
| 50 | + The output is similar to this: |
| 51 | + |
| 52 | + ``` |
| 53 | + NAME STATUS ROLES AGE VERSION LABELS |
| 54 | + worker0 Ready <none> 1d v1.13.0 ...,disktype=ssd,kubernetes.io/hostname=worker0 |
| 55 | + worker1 Ready <none> 1d v1.13.0 ...,kubernetes.io/hostname=worker1 |
| 56 | + worker2 Ready <none> 1d v1.13.0 ...,kubernetes.io/hostname=worker2 |
| 57 | + ``` |
| 58 | + |
| 59 | + In the preceding output, you can see that the `worker0` node has a |
| 60 | + `disktype=ssd` label. |
| 61 | + |
| 62 | +## Schedule a Pod using required node affinity |
| 63 | + |
| 64 | +This manifest describes a Pod that has a `requiredDuringSchedulingIgnoredDuringExecution` node affinity,`disktype: ssd`. |
| 65 | +This means that the pod will get scheduled only on a node that has a `disktype=ssd` label. |
| 66 | + |
| 67 | +{{< codenew file="pods/pod-nginx-required-affinity.yaml" >}} |
| 68 | + |
| 69 | +1. Apply the manifest to create a Pod that is scheduled onto your |
| 70 | + chosen node: |
| 71 | + |
| 72 | + ```shell |
| 73 | + kubectl apply -f https://k8s.io/examples/pods/pod-nginx-required-affinity.yaml |
| 74 | + ``` |
| 75 | + |
| 76 | +1. Verify that the pod is running on your chosen node: |
| 77 | + |
| 78 | + ```shell |
| 79 | + kubectl get pods --output=wide |
| 80 | + ``` |
| 81 | + |
| 82 | + The output is similar to this: |
| 83 | + |
| 84 | + ``` |
| 85 | + NAME READY STATUS RESTARTS AGE IP NODE |
| 86 | + nginx 1/1 Running 0 13s 10.200.0.4 worker0 |
| 87 | + ``` |
| 88 | + |
| 89 | +## Schedule a Pod using preferred node affinity |
| 90 | + |
| 91 | +This manifest describes a Pod that has a `preferredDuringSchedulingIgnoredDuringExecution` node affinity,`disktype: ssd`. |
| 92 | +This means that the pod will prefer a node that has a `disktype=ssd` label. |
| 93 | + |
| 94 | +{{< codenew file="pods/pod-nginx-preferred-affinity.yaml" >}} |
| 95 | + |
| 96 | +1. Apply the manifest to create a Pod that is scheduled onto your |
| 97 | + chosen node: |
| 98 | + |
| 99 | + ```shell |
| 100 | + kubectl apply -f https://k8s.io/examples/pods/pod-nginx-preferred-affinity.yaml |
| 101 | + ``` |
| 102 | + |
| 103 | +1. Verify that the pod is running on your chosen node: |
| 104 | + |
| 105 | + ```shell |
| 106 | + kubectl get pods --output=wide |
| 107 | + ``` |
| 108 | + |
| 109 | + The output is similar to this: |
| 110 | + |
| 111 | + ``` |
| 112 | + NAME READY STATUS RESTARTS AGE IP NODE |
| 113 | + nginx 1/1 Running 0 13s 10.200.0.4 worker0 |
| 114 | + ``` |
| 115 | + |
| 116 | + |
| 117 | + |
| 118 | +## {{% heading "whatsnext" %}} |
| 119 | + |
| 120 | +Learn more about |
| 121 | +[Node Affinity](/docs/concepts/scheduling-eviction/assign-pod-node/#node-affinity). |
| 122 | + |
0 commit comments