|
| 1 | +--- |
| 2 | +title: 用节点亲和性把 Pods 分配到节点 |
| 3 | +min-kubernetes-server-version: v1.10 |
| 4 | +content_type: task |
| 5 | +weight: 120 |
| 6 | +--- |
| 7 | +<!-- |
| 8 | +title: Assign Pods to Nodes using Node Affinity |
| 9 | +min-kubernetes-server-version: v1.10 |
| 10 | +content_type: task |
| 11 | +weight: 120 |
| 12 | +--> |
| 13 | + |
| 14 | +<!-- overview --> |
| 15 | +<!-- |
| 16 | +This page shows how to assign a Kubernetes Pod to a particular node using Node Affinity in a |
| 17 | +Kubernetes cluster. |
| 18 | +--> |
| 19 | +本页展示在 Kubernetes 集群中,如何使用节点亲和性把 Kubernetes Pod 分配到特定节点。 |
| 20 | + |
| 21 | +## {{% heading "prerequisites" %}} |
| 22 | + |
| 23 | + |
| 24 | +{{< include "task-tutorial-prereqs.md" >}} {{< version-check >}} |
| 25 | + |
| 26 | + |
| 27 | + |
| 28 | +<!-- steps --> |
| 29 | + |
| 30 | +<!-- |
| 31 | +## Add a label to a node |
| 32 | +
|
| 33 | +1. List the nodes in your cluster, along with their labels: |
| 34 | +--> |
| 35 | +## 给节点添加标签 |
| 36 | + |
| 37 | +1. 列出集群中的节点及其标签: |
| 38 | + |
| 39 | + ```shell |
| 40 | + kubectl get nodes --show-labels |
| 41 | + ``` |
| 42 | + <!-- |
| 43 | + The output is similar to this: |
| 44 | + --> |
| 45 | + 输出类似于此: |
| 46 | + |
| 47 | + ``` |
| 48 | + NAME STATUS ROLES AGE VERSION LABELS |
| 49 | + worker0 Ready <none> 1d v1.13.0 ...,kubernetes.io/hostname=worker0 |
| 50 | + worker1 Ready <none> 1d v1.13.0 ...,kubernetes.io/hostname=worker1 |
| 51 | + worker2 Ready <none> 1d v1.13.0 ...,kubernetes.io/hostname=worker2 |
| 52 | + ``` |
| 53 | + <!-- |
| 54 | + 1. Chose one of your nodes, and add a label to it: |
| 55 | + --> |
| 56 | +1. 选择一个节点,给它添加一个标签: |
| 57 | + |
| 58 | + ```shell |
| 59 | + kubectl label nodes <your-node-name> disktype=ssd |
| 60 | + ``` |
| 61 | + <!-- |
| 62 | + where `<your-node-name>` is the name of your chosen node. |
| 63 | + |
| 64 | + 1. Verify that your chosen node has a `disktype=ssd` label: |
| 65 | + --> |
| 66 | + |
| 67 | + 其中 `<your-node-name>` 是你所选节点的名称。 |
| 68 | + |
| 69 | +2. 验证你所选节点具有 `disktype=ssd` 标签: |
| 70 | + |
| 71 | + ```shell |
| 72 | + kubectl get nodes --show-labels |
| 73 | + ``` |
| 74 | + |
| 75 | + <!-- |
| 76 | + The output is similar to this: |
| 77 | + --> |
| 78 | + 输出类似于此: |
| 79 | + |
| 80 | + ``` |
| 81 | + NAME STATUS ROLES AGE VERSION LABELS |
| 82 | + worker0 Ready <none> 1d v1.13.0 ...,disktype=ssd,kubernetes.io/hostname=worker0 |
| 83 | + worker1 Ready <none> 1d v1.13.0 ...,kubernetes.io/hostname=worker1 |
| 84 | + worker2 Ready <none> 1d v1.13.0 ...,kubernetes.io/hostname=worker2 |
| 85 | + ``` |
| 86 | + |
| 87 | + <!-- |
| 88 | + In the preceding output, you can see that the `worker0` node has a |
| 89 | + `disktype=ssd` label. |
| 90 | + --> |
| 91 | + 在前面的输出中,可以看到 `worker0` 节点有一个 `disktype=ssd` 标签。 |
| 92 | + |
| 93 | +<!-- |
| 94 | +## Schedule a Pod using required node affinity |
| 95 | + |
| 96 | +This manifest describes a Pod that has a `requiredDuringSchedulingIgnoredDuringExecution` node affinity,`disktype: ssd`. |
| 97 | +This means that the pod will get scheduled only on a node that has a `disktype=ssd` label. |
| 98 | +--> |
| 99 | +## 依据强制的节点亲和性调度 Pod {#schedule-a-Pod-using-required-node-affinity} |
| 100 | + |
| 101 | +下面清单描述了一个 Pod,它有一个节点亲和性配置 `requiredDuringSchedulingIgnoredDuringExecution`,`disktype=ssd`。 |
| 102 | +这意味着 pod 只会被调度到具有 `disktype=ssd` 标签的节点上。 |
| 103 | + |
| 104 | +{{< codenew file="pods/pod-nginx-required-affinity.yaml" >}} |
| 105 | + |
| 106 | +<!-- |
| 107 | +1. Apply the manifest to create a Pod that is scheduled onto your |
| 108 | + chosen node: |
| 109 | +--> |
| 110 | +1. 执行(Apply)此清单来创建一个调度到所选节点上的 Pod: |
| 111 | + |
| 112 | + ```shell |
| 113 | + kubectl apply -f https://k8s.io/examples/pods/pod-nginx-required-affinity.yaml |
| 114 | + ``` |
| 115 | + |
| 116 | + <!-- |
| 117 | + 1. Verify that the pod is running on your chosen node: |
| 118 | + --> |
| 119 | +1. 验证 pod 已经在所选节点上运行: |
| 120 | + |
| 121 | + ```shell |
| 122 | + kubectl get pods --output=wide |
| 123 | + ``` |
| 124 | + |
| 125 | + <!-- |
| 126 | + The output is similar to this: |
| 127 | + --> |
| 128 | + 输出类似于此: |
| 129 | + |
| 130 | + ``` |
| 131 | + NAME READY STATUS RESTARTS AGE IP NODE |
| 132 | + nginx 1/1 Running 0 13s 10.200.0.4 worker0 |
| 133 | + ``` |
| 134 | + |
| 135 | +<!-- |
| 136 | +## Schedule a Pod using preferred node affinity |
| 137 | + |
| 138 | +This manifest describes a Pod that has a `preferredDuringSchedulingIgnoredDuringExecution` node affinity,`disktype: ssd`. |
| 139 | +This means that the pod will prefer a node that has a `disktype=ssd` label. |
| 140 | +--> |
| 141 | +## 使用首选的节点亲和性调度 Pod {#schedule-a-Pod-using-preferred-node-affinity} |
| 142 | + |
| 143 | +本清单描述了一个Pod,它有一个节点亲和性设置 `preferredDuringSchedulingIgnoredDuringExecution`,`disktype: ssd`。 |
| 144 | +这意味着 pod 将首选具有 `disktype=ssd` 标签的节点。 |
| 145 | + |
| 146 | +{{< codenew file="pods/pod-nginx-preferred-affinity.yaml" >}} |
| 147 | + |
| 148 | +<!-- |
| 149 | +1. Apply the manifest to create a Pod that is scheduled onto your |
| 150 | + chosen node: |
| 151 | +--> |
| 152 | +1. 执行此清单创建一个会调度到所选节点上的 Pod: |
| 153 | + |
| 154 | + ```shell |
| 155 | + kubectl apply -f https://k8s.io/examples/pods/pod-nginx-preferred-affinity.yaml |
| 156 | + ``` |
| 157 | + |
| 158 | + <!-- |
| 159 | + 1. Verify that the pod is running on your chosen node: |
| 160 | + --> |
| 161 | +1. 验证 pod 是否在所选节点上运行: |
| 162 | + |
| 163 | + ```shell |
| 164 | + kubectl get pods --output=wide |
| 165 | + ``` |
| 166 | + |
| 167 | + <!-- |
| 168 | + The output is similar to this: |
| 169 | + --> |
| 170 | + 输出类似于此: |
| 171 | + |
| 172 | + ``` |
| 173 | + NAME READY STATUS RESTARTS AGE IP NODE |
| 174 | + nginx 1/1 Running 0 13s 10.200.0.4 worker0 |
| 175 | + ``` |
| 176 | + |
| 177 | + |
| 178 | + |
| 179 | +## {{% heading "whatsnext" %}} |
| 180 | + |
| 181 | +<!-- |
| 182 | +Learn more about |
| 183 | +[Node Affinity](/docs/concepts/scheduling-eviction/assign-pod-node/#node-affinity). |
| 184 | +--> |
| 185 | +进一步了解 |
| 186 | +[节点亲和性](/zh/docs/concepts/scheduling-eviction/assign-pod-node/#node-affinity). |
0 commit comments