MYHOSPITALNOW.COM

Find Trusted Doctors & Hospitals

Discover healthcare providers, compare hospitals, and explore treatment options worldwide.

Free Treatment Quotes • Doctor Search • Hospital Comparison

Find Healthcare Options

www.myhospitalnow.com

Understanding Node Selector and Node Affinity in Kubernetes

The Kubernetes scheduler can be constrained to place a pod on particular nodes using few different options. In your POD specification, there are many ways in which you declare a POD should be dedicated to specific nodes.

  • Node Selector
  • Node Affinity
  • Pod Affinity aka inter-pod affinity/anti-affinity
  • Taints and Tolerations

In this post, I am going to discuss on Node Selector and Node Affinity.

nodeSelector
nodeSelector is the simplest recommended form of node selection constraint. nodeSelector is a field of PodSpec. It specifies a map of key-value pairs. For the pod to be eligible to run on a node, the node must have each of the indicated key-value pairs as labels (it can have additional labels as well).

Interlude: built-in node labels

In addition to labels you attach, nodes come pre-populated with a standard set of labels. These labels are

kubernetes.io/hostname
failure-domain.beta.kubernetes.io/zone
failure-domain.beta.kubernetes.io/region
topology.kubernetes.io/zone
topology.kubernetes.io/region
beta.kubernetes.io/instance-type
node.kubernetes.io/instance-type
kubernetes.io/os
kubernetes.io/arch

The affinity feature consists of two types of affinity

  • node affinity” and
  • inter-pod affinity/anti-affinity”
Diagram of Kubernetes scheduler placing pod with nodeSelector vip true on node labeled vip=true

nodeAffinity

Kubernetes also has a more nuanced way of setting affinity called nodeAffinity and podAffinity.

nodeSelector provides a very simple way to constrain pods to nodes with particular labels. Node affinity is conceptually similar to nodeSelector but nodeAffinity allows users to more expressive way pods to nodes with particular labels.

The affinity/anti-affinity feature, greatly expands the types of constraints you can express. The key enhancements are

  1. The affinity/anti-affinity language is more expressive. The language offers more matching rules besides exact matches created with a logical AND operation;
  2. you can indicate that the rule is “soft”/”preference” rather than a hard requirement, so if the scheduler can’t satisfy it, the pod will still be scheduled;
  3. you can constrain against labels on other pods running on the node (or other topological domain), rather than against labels on the node itself, which allows rules about which pods can and cannot be co-located

The affinity feature consists of two types of affinity

  • node affinity – Node affinity is like the existing nodeSelector (but with the first two benefits listed above)
  • inter-pod affinity/anti-affinity – inter-pod affinity/anti-affinity constrains against pod labels rather than node labels, as described in the all three item listed above.

These are fields in under Pod metadata and take automatic or user-defined metadata to dictate where to schedule pods. affinity differs from nodeSelector in the following ways:

  • Schedule a pod based on which other pods are or are not running on a node.
  • Request without requiring that a pod be run on a node.
  • Specify a set of allowable values instead of a single value requirement.

Types of node affinity

There are currently two types of node affinity, called

requiredDuringSchedulingIgnoredDuringExecution

  • hard rule

preferredDuringSchedulingIgnoredDuringExecution

  • soft rule

nodeAffinity example

Lets take the above example of deploying a nginx and a httpd pod, except we have a more complicated set of requirements:

  • nginx cannot run on the same node as httpd
  • httpd should run on a node with the x-web:yes label, but can run anywhere.
  • nginx must run on a node with y-web:yes label and should fail if not.
Diagram of nodeAffinity matchExpressions placing pod on node labeled myKey=label1, not label3
Diagram of nodeAffinity zone rule scheduling pods on e2e-az1 and e2e-az2 nodes, not other-zone

inter-pod affinity/anti-affinity
Inter-pod affinity and anti-affinity allow you to constrain which nodes your pod is eligible to be scheduled based on labels on pods that are already running on the node rather than based on labels on nodes.

An example of a pod that uses pod affinity

Pod anti-affinity diagram: Pod 3 with security s1 is blocked from Node 1 and scheduled on Node 2 by hostname

Reference

https://coreos.com/fleet/docs/latest/affinity.html

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services — all in one place.

Explore Hospitals
I'm Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms. I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.

Related Posts

Openshif: Create a busybox pod

This page shows how to start a temporary BusyBox pod in OpenShift with oc run. The pod passes the restricted security policies, so you can test DNS,…

Read More

OpenShift Lab 10: View Pod, Workload, and Namespace Performance Metrics Using the OpenShift Web Console

This lab uses the OpenShift web console to view CPU, memory, network and pod metrics for one namespace and its workloads. It then checks the same numbers…

Read More

Kubernetes ReplicaSet Explained: API Resource, YAML Example, and Use Cases

Rajesh Kumar I’m Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have…

Read More

What Is GitHub Actions Runner Controller?

GitHub Actions is a CI/CD platform that automates tasks such as building applications, running tests, creating Docker images, and deploying software. Every GitHub Actions job requires a…

Read More

Kubernetes networking plugins/CNI plugins

1. Kubernetes CNI / Network Plugins Plugin Type AWS Azure GCP On-Prem Key Use Calico CNI + Network Policy ✅ ✅ ✅ ✅ Networking + security policies…

Read More

Technical Interview Questions for DevOps Engineers: Coding, Troubleshooting, and System Design

DevOps technical interviews have evolved significantly. Knowing how to configure a CI/CD pipeline or explain Kubernetes concepts is no longer enough for many engineering roles. Interviewers increasingly…

Read More