02 - Node Affinity
Node Affinity(節點親和性)
Node Affinity 是 Kubernetes 調度 Pod 到特定節點 (Node) 的機制。它比 nodeSelector 更靈活,允許你設定多種匹配條件。
Node Affinity 主要有 三種模式:
- requiredDuringSchedulingIgnoredDuringExecution
- Required 強制規則,Pod 只會調度到符合條件的節點,否則調度失敗。
- preferredDuringSchedulingIgnoredDuringExecution
- Preferred 軟性規則,Kubernetes 會嘗試將 Pod 調度到符合條件的節點,但如果沒有符合的節點,也會允許調度到其他節點。
- requiredDuringSchedulingRequiredDuringExecution
- 這種模式在 調度時和執行時 都強制應用。
常見 operator:
- In:允許特定值,例如 color=blue
- NotIn:禁止特定值,例如 color != blue
- Exists:只要該 key 存在即可,不管值是什麼
- DoesNotExist:只允許該 key 不存在的節點
Required(強制規則)
Pod 只能調度到 color=blue 的節點。
🚫 如果沒有符合的節點,Pod 會卡住 (Pending),不會被調度!
apiVersion: apps/v1
kind: Deployment
metadata:
name: blue-app
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: color
operator: In
values:
- blue
containers:
- name: nginx
image: nginx:latest
imagePullPolicy: Always
Preferred(軟性規則)
Pod 優先選擇 color=blue 的節點,但如果沒有,也可以跑在其他節點:
✅ 沒有符合的節點時,Pod 仍然可以跑在其他節點上!
affinity:
nodeAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 1
preference:
matchExpressions:
- key: color
operator: In
values:
- blue
多條件 Node Affinity
如果你想要 Pod 必須滿足以下條件才能調度:
摘要與總結:結合 Taints & Tolerations
- Node Affinity
- 決定 Pod 去哪裡
- 用途:偏好/強制 Pod 調度到指定節點
- Taints & Tolerations
- 決定 Pod 不能去哪裡
- 用途:排除不適合的節點
$ kubectl describe node node01
$ kubectl describe nodes | grep Labels -C 5
$ kubectl get node node01 --show-labels
Q: Apply a label color=blue to node `node01`
$ kubectl label node node01 color=blue
Q: Create a new deployment named `blue` with the `nginx` image and 3 replicas.
- Name: blue
- Replicas: 3
- Image: nginx
$ kubectl create deployment blue --image=nginx --replicas=3
$ kubectl get deploy
Q: Which nodes can the pods for the blue deployment be placed on?
- Note: Make sure to check taints on both nodes!
【方法一】
$ kubectl get pods -o wide
【方法二】
$ kubectl describe nodes | grep Taint
解釋:Check if `controlplane` and `node01` have any taints on them that will prevent the pods to be scheduled on them. If there are no taints, the pods can be scheduled on either node.
Check the taints on both nodes.
$ kubectl describe node controlplane | grep -i taints
$ kubectl describe node node01 | grep -i taints
Q: Set Node Affinity to the deployment to place the pods on `node01` only.
- Name: blue
- Replicas: 3
- Image: nginx
- NodeAffinity: requiredDuringSchedulingIgnoredDuringExecution
- Key: color
- value: blue
$ kubectl edit deployment blue
修改 Pod templates:Pod 只能調度到標有 color=blue 的 Node
kubectl edit 一經儲存,便直接套用修改。
apiVersion: apps/v1
kind: Deployment
metadata:
name: blue
spec:
replicas: 3
selector:
matchLabels:
run: nginx
template:
metadata:
labels:
run: nginx
spec:
containers:
- image: nginx
imagePullPolicy: Always
name: nginx
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: color
operator: In
values:
- blue
```
Q: Create a new deployment named `red` with the `nginx` image and 2 replicas, and ensure it gets placed on the `controlplane` node only.
Note: Use the label key - `node-role.kubernetes.io/control-plane` - which is already set on the `controlplane` node.- Name: red
- Replicas: 2
- Image: nginx
- NodeAffinity: requiredDuringSchedulingIgnoredDuringExecution
- Key: node-role.kubernetes.io/control-plane
- Use the right operator
【方法一】
【方法二】
$ kubectl create -f reddeploy.yaml
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: red
spec:
replicas: 2
selector:
matchLabels:
run: nginx
template:
metadata:
labels:
run: nginx
spec:
containers:
- image: nginx
imagePullPolicy: Always
name: nginx
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: node-role.kubernetes.io/control-plane
operator: Exists



留言
張貼留言