Find the Best Cosmetic Hospitals

Explore trusted cosmetic hospitals and make a confident choice for your transformation.

“Invest in yourself — your confidence is always worth it.”

Explore Cosmetic Hospitals

Start your journey today — compare options in one place.

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”

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.

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

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 a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I have worked at <a href="https://www.cotocus.com/">Cotocus</a>. I share tech blog at <a href="https://www.devopsschool.com/">DevOps School</a>, travel stories at <a href="https://www.holidaylandmark.com/">Holiday Landmark</a>, stock market tips at <a href="https://www.stocksmantra.in/">Stocks Mantra</a>, health and fitness guidance at <a href="https://www.mymedicplus.com/">My Medic Plus</a>, product reviews at <a href="https://www.truereviewnow.com/">TrueReviewNow</a> , and SEO strategies at <a href="https://www.wizbrand.com/">Wizbrand.</a> Do you want to learn <a href="https://www.quantumuting.com/">Quantum Computing</a>? <strong>Please find my social handles as below;</strong> <a href="https://www.rajeshkumar.xyz/">Rajesh Kumar Personal Website</a> <a href="https://www.youtube.com/TheDevOpsSchool">Rajesh Kumar at YOUTUBE</a> <a href="https://www.instagram.com/rajeshkumarin">Rajesh Kumar at INSTAGRAM</a> <a href="https://x.com/RajeshKumarIn">Rajesh Kumar at X</a> <a href="https://www.facebook.com/RajeshKumarLog">Rajesh Kumar at FACEBOOK</a> <a href="https://www.linkedin.com/in/rajeshkumarin/">Rajesh Kumar at LINKEDIN</a> <a href="https://www.wizbrand.com/rajeshkumar">Rajesh Kumar at WIZBRAND</a> <a href="https://www.rajeshkumar.xyz/dailylogs">Rajesh Kumar DailyLogs</a>

Related Posts

Ansible: Deep Dive into Jinja2 Ansible Template with example

Introduction to Ansible Templates and Jinja2 Ansible templates are powerful tools for creating dynamic configuration files and scripts based on variables and logic. Templates in Ansible use…

Read More

Complete User Guide for 404 Errors

Introduction A 404 error (also known as “404 Not Found”) is an HTTP status code indicating that a requested webpage cannot be found on the server. This…

Read More

What is SQL Server and use cases of SQL Server?

What is SQL Server? SQL Server, developed by Microsoft, is a relational database management system (RDBMS) that is designed to store and retrieve data requested by other…

Read More

Terrafrom – Example Code for remote-exec, local-exec & file provisioner

Rajesh Kumar I’m a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I have worked at Cotocus. I share tech blog at DevOps School, travel stories at…

Read More

What is SQL and use cases of SQL?

What is SQL? SQL stands for Structured Query Language. It is a powerful programming language that allows users to interact with relational databases. It provides a standardized…

Read More

What is Microsoft SQL Server Integration Services and use cases of Microsoft SQL Server Integration Services?

What are Microsoft SQL Server Integration Services? Microsoft SQL Server Integration Services (SSIS) is a platform for building high-performance data integration solutions, including extraction, transformation, and loading…

Read More