kubernetes: NodeAffinity preferredDuringSchedulingIgnoredDuringExecution doesn't work well

What happened:

I have 2 nodes, one label is foo=bar, annother label is foo=car, both of them have enough resources. And I want to use nodeAffinity’s preferredDuringSchedulingIgnoredDuringExecution feature to make my pods be assigned in target node. I use deployment to manage pods, which setting the pod replica is 2. But when pods are created, I found one pod is created on node (foo=bar), and another pod is created on node (foo=car)

Here is my config content.

      affinity:
        nodeAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
            - preference:
                matchExpressions:
                  - key: foo
                    operator: In
                    values:
                      - bar
              weight: 100

What you expected to happen:

All pods are created on node which label is foo=bar.

How to reproduce it (as minimally and precisely as possible):

Created a deployment, assigned replica is 2 and add the preferredDuringSchedulingIgnoredDuringExecution.

Anything else we need to know?:

Before I report this problem, I read the k8s source code and found all scorePlugin’s weight is 1, except nodepreferavoidpods and podtopologyspread. And I noticed the podtopologyspread comment explained This is a score coming from user preference, so this scorePlugin’s weight is 2. So I wonder know why nodeaffinity coming from user preference too, but Its weight is still set to 1. I think noderesources.BalancedAllocationName plugin and noderesources.LeastAllocatedName plugin affected the behavior of nodeAffinity. So Is there any plan to change nodeaffinity’s weight from 1 to 2?

source code (path: pkg/scheduler/algorithmprovider/registry.go)

		Score: schedulerapi.PluginSet{
			Enabled: []schedulerapi.Plugin{
				{Name: noderesources.BalancedAllocationName, Weight: 1},
				{Name: imagelocality.Name, Weight: 1},
				{Name: interpodaffinity.Name, Weight: 1},
				{Name: noderesources.LeastAllocatedName, Weight: 1},
				{Name: nodeaffinity.Name, Weight: 1},
				{Name: nodepreferavoidpods.Name, Weight: 10000},
				// Weight is doubled because:
				// - This is a score coming from user preference.
				// - It makes its signal comparable to NodeResourcesLeastAllocated.
				{Name: podtopologyspread.Name, Weight: 2},
				{Name: tainttoleration.Name, Weight: 1},
			},
		}

Environment:

  • Kubernetes version (use kubectl version): v1.18 (but even the code on master branch, nodeaffinity’s weight is still set to 1)
  • Cloud provider or hardware configuration: EKS
  • OS (e.g: cat /etc/os-release):
NAME="Amazon Linux"
VERSION="2"
ID="amzn"
ID_LIKE="centos rhel fedora"
VERSION_ID="2"
PRETTY_NAME="Amazon Linux 2"
ANSI_COLOR="0;33"
CPE_NAME="cpe:2.3:o:amazon:amazon_linux:2"
HOME_URL="https://amazonlinux.com/"
  • Kernel (e.g. uname -a): x86_64 x86_64 x86_64 GNU/Linux
  • Install tools:
  • Network plugin and version (if this is a network-related bug):
  • Others:

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 1
  • Comments: 20 (10 by maintainers)

Most upvoted comments

And not only is there a competition between topology spreading and node affinity, but also all scorePlugins. So I think it’s necessary assign a property priority. In my opinion, the user setting’s priority should be higher than the priority of the system’s internal scheduling.

@alculquicondor Thanks for your response. All my test pods coming from a deployment.

Indeed, pods are tend to spread by default. But I think that this default behavior should change as expected after the preferredDuringSchedulingIgnoredDuringExecution is configured. If this change is not obvious, it will give users a feeling as if this configuration has not taken effect.