Sub topic 9
What IgnoredDuringExecution means for running pods
This chapter assumes you can write a pod manifest with nodeSelector or nodeAffinity, and that you can label nodes with kubectl. It covers what happens to a pod when its node stops matching the rules after the pod has started.
Reading the long field name
The affinity field names are long because each one has two halves.
DuringSchedulingdescribes what the rule does while the scheduler picks a node.IgnoredDuringExecutiondescribes what the rule does once the pod is running.
Together, requiredDuringSchedulingIgnoredDuringExecution means that the scheduler must find a matching node, and that Kubernetes stops checking the rule after the pod is placed. The same applies to preferredDuringSchedulingIgnoredDuringExecution. The nodeSelector field behaves the same way.
The scheduler evaluates these rules once per scheduling attempt. It does not re-evaluate them later.
Seeing it happen
Assume nginx-ssd from the nodeSelector chapter is running on worker-1, which carries the label disktype=ssd. Remove the label:
kubectl label nodes worker-1 disktype-
kubectl get pod nginx-ssd -o wide
kubectl get nodes -L disktypeThe trailing - deletes the label. The pod still shows Running on worker-1, and the node's disktype column is now empty. The pod no longer matches its own rule, and nothing happens to it.
Now recreate the pod:
kubectl delete pod nginx-ssd
kubectl apply -f nginx-ssd.yaml
kubectl get pod nginx-ssdThis time the scheduler runs again, finds no node with disktype=ssd, and leaves the pod in Pending. Only a new scheduling attempt applies the rule.
Consequences for operations
- Changing node labels does not move existing pods. Only pods created afterward see the new labels.
- A Deployment does not reschedule pods when you edit labels. Run
kubectl rollout restart deployment/<name>to create new pods that go through scheduling again. - A container restart inside a pod does not trigger rescheduling. The pod stays bound to its node for its whole life.
- A pod placed with a preference can run on a non-preferred node indefinitely. It will not move when a better node appears.
Evicting pods that no longer match
Kubernetes has no built-in way to evict pods that violate their node affinity. The field requiredDuringSchedulingRequiredDuringExecution was proposed for this, but it was never implemented. Two options exist:
- Delete or restart the pods yourself, as shown above.
- Run the Descheduler, a separate add-on. Its
RemovePodsViolatingNodeAffinitystrategy evicts pods that no longer satisfy their required node affinity. It relies on the owning controller to create replacements.
Contrast with taints
A taint with the NoExecute effect does act on running pods. The control plane evicts pods that do not tolerate it. Node affinity is only a scheduling-time filter and has no eviction behavior.
Exercise
A Deployment's pods use a required node affinity for disktype=ssd. You remove that label from the node they run on. What happens to the running pods, and what must you do to make the rule apply again?
Show answer
The running pods keep running, because the rule is ignored after scheduling. To apply it again, create new pods, for example with kubectl rollout restart deployment/<name>. The scheduler then evaluates the rule, and the new pods stay Pending if no node carries the label.