Sub topic 14
Cluster-wide defaults with addedAffinity
This chapter assumes you can write pod-level nodeSelector and nodeAffinity rules and that you know the scheduler filters nodes and then scores them. It also assumes you can edit the control plane's kube-scheduler configuration. Many managed clusters don't allow that.
Why a scheduler-level default exists
Pod-level rules must be written into every pod spec. To keep a whole group of pods on one set of nodes, you would have to edit every manifest, or add an admission webhook that mutates them. The NodeAffinity scheduler plugin has an argument called addedAffinity. It adds a node affinity rule to every pod that a given scheduler profile places.
Where the setting lives
addedAffinity is part of the scheduler's own configuration file, not a Kubernetes object you apply with kubectl. You pass the file to kube-scheduler with the --config flag. The setting is under pluginConfig, in the arguments of the NodeAffinity plugin. Its structure is the same as spec.affinity.nodeAffinity in a pod.
apiVersion: kubescheduler.config.k8s.io/v1
kind: KubeSchedulerConfiguration
profiles:
- schedulerName: default-scheduler
- schedulerName: foo-scheduler
pluginConfig:
- name: NodeAffinity
args:
addedAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: scheduler-profile
operator: In
values:
- fooThis file defines two profiles. A profile is a named scheduler configuration, and one kube-scheduler process can run several. The default-scheduler profile is unchanged. The foo-scheduler profile requires the label scheduler-profile=foo on every node it considers.
Selecting the profile from a pod
A pod picks a profile with spec.schedulerName. Pods that leave the field empty use default-scheduler.
apiVersion: v1
kind: Pod
metadata:
name: nginx-foo
spec:
schedulerName: foo-scheduler
containers:
- name: nginx
image: nginx:1.27This pod has no affinity of its own. It can still only run on nodes labeled scheduler-profile=foo, because the profile adds that requirement.
How it combines with the pod's own rules
The added rule does not replace the pod's rules. Both apply:
| Part | Effect |
|---|---|
| Added required rule | A node must satisfy it and the pod's own required rules, which means `nodeSelector` and required node affinity. |
| Added preferred rule | Its weights are added to the pod's preferred weights when nodes are scored. |
Think of the added required rule as one more condition joined with AND. A pod cannot override or opt out of it. If the pod asks for disktype=ssd, the node needs both disktype=ssd and scheduler-profile=foo. If no node has both labels, the pod stays Pending.
Limits and gotchas
- Pods that set
spec.nodeNamedirectly skip the scheduler, soaddedAffinitynever applies to them. - Only the profile's pods are affected. Pods that name another scheduler are not.
- The added rule is invisible in the pod spec. When a pod is Pending,
kubectl describe podreports a node affinity mismatch, but the pod's manifest does not show where the extra rule came from. Check the scheduler configuration. - Changing the file means restarting kube-scheduler. Pods that are already running are not moved, because the rule is also "IgnoredDuringExecution".
Exercise
A profile has addedAffinity requiring scheduler-profile=foo. A pod using that profile sets nodeSelector disktype=ssd. Node A has only disktype=ssd. Node B has only scheduler-profile=foo. Node C has both labels. Which nodes can run the pod?
Show answer
Only node C. The added required rule and the pod's nodeSelector are both conditions that must hold, so a node needs both labels. Node A and node B each have only one.