Sub topic 7
Soft preferences with weights
This chapter assumes you can write pod manifests, apply them with kubectl, and read node labels. You should also know how nodeSelector and required node affinity work. Required rules are hard filters. Preferred rules are soft: they change which node wins, but they never make a pod unschedulable.
The preferred rule
The field is preferredDuringSchedulingIgnoredDuringExecution. It is a list. Each entry has two parts:
weight: an integer from 1 to 100.preference: a node selector term, withmatchExpressionsand/ormatchFields.
This differs from the required rule. Required rules use nodeSelectorTerms directly. Preferred rules wrap each term in preference and attach a weight.
How the scheduler uses weights
The scheduler works in two phases. First it filters out nodes that fail hard constraints. Then it scores the remaining nodes. For each preferred entry whose preference matches a node, the scheduler adds that entry's weight to the node's score. The node with the highest total score wins, after the scheduler combines this with its other scoring plugins.
A preference that matches no node does not block scheduling. The pod simply lands on a feasible node with a lower score.
Example: require Linux, prefer SSD
apiVersion: v1
kind: Pod
metadata:
name: prefer-ssd
spec:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: kubernetes.io/os
operator: In
values:
- linux
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 80
preference:
matchExpressions:
- key: disktype
operator: In
values:
- ssd
containers:
- name: nginx
image: nginx:1.27The required block removes every non-Linux node. Among the Linux nodes, those labeled disktype=ssd gain 80 points. If no Linux node has that label, the pod still runs on a Linux node without it.
Several preferences
You can list more than one entry. Their weights add up for each node that matches them.
Consider two preferred entries: weight 80 for disktype=ssd and weight 20 for topology.kubernetes.io/zone=zone-a.
| Node | Matches ssd (80) | Matches zone-a (20) | Affinity score |
|---|---|---|---|
| node-1 | yes | yes | 100 |
| node-2 | yes | no | 80 |
| node-3 | no | yes | 20 |
| node-4 | no | no | 0 |
In this case node-1 wins. If it is full, node-2 is next, because the SSD preference outweighs the zone preference.
Common mistakes
- Treating a preference as a guarantee. Other scoring plugins also influence the result. Spreading and resource balancing can outweigh a small affinity weight. If the pod must land on a certain kind of node, use a required rule.
- Using a weight of 0 or above 100. The API server rejects values outside 1 to 100.
- Putting
weightinsidepreference. It belongs besidepreference, at the list entry level. - Expecting relative weights to matter in isolation. Only the totals across entries and the other plugins matter, so a single entry with weight 1 behaves like a single entry with weight 100 when nothing else differs.
Checking the result
After you apply the pod, see where it ran:
kubectl get pod prefer-ssd -o wide
kubectl get nodes -L disktypeCompare the NODE column with the DISKTYPE column. If the pod is on a node without the label, check whether the SSD nodes were full or failed the required rule.
Exercise
A pod has one required rule (kubernetes.io/os In linux) and two preferred entries: weight 50 for disktype=ssd, and weight 30 for zone=zone-b. Node A is Linux with disktype=ssd only. Node B is Linux with zone=zone-b only. Node C is Windows with both labels. Which node does the affinity rules favor, and what are the affinity scores?
Show answer
Node A is favored. Node C is removed by the required rule, so its labels do not matter. Node A scores 50 and Node B scores 30. If Node A cannot fit the pod, the scheduler uses Node B.