Learning Library

← Back to Library

Kubernetes Pod Scheduling with Affinity

Key Points

  • Kubernetes automatically schedules pods across worker (virtual or physical) nodes using its built‑in scheduler to balance resources.
  • Each pod is visualized by a “drone” that changes color to match the node it’s placed on, illustrating pod‑to‑node assignment.
  • Affinity and anti‑affinity policies let you control pod placement; an anti‑affinity rule can prevent multiple instances of the same app from running on the same node.
  • When scaling from one to three replicas with an anti‑affinity rule and three nodes, Kubernetes distributes each pod to a different node as intended.
  • Adding a fourth replica with only three nodes causes the extra pod to remain pending, showing that hard anti‑affinity can block scheduling unless you use softer rules that allow fallback to default placement.

Full Transcript

# Kubernetes Pod Scheduling with Affinity **Source:** [https://www.youtube.com/watch?v=pAO7300nluc](https://www.youtube.com/watch?v=pAO7300nluc) **Duration:** 00:02:57 ## Summary - Kubernetes automatically schedules pods across worker (virtual or physical) nodes using its built‑in scheduler to balance resources. - Each pod is visualized by a “drone” that changes color to match the node it’s placed on, illustrating pod‑to‑node assignment. - Affinity and anti‑affinity policies let you control pod placement; an anti‑affinity rule can prevent multiple instances of the same app from running on the same node. - When scaling from one to three replicas with an anti‑affinity rule and three nodes, Kubernetes distributes each pod to a different node as intended. - Adding a fourth replica with only three nodes causes the extra pod to remain pending, showing that hard anti‑affinity can block scheduling unless you use softer rules that allow fallback to default placement. ## Sections - [00:00:00](https://www.youtube.com/watch?v=pAO7300nluc&t=0s) **Visualizing Kubernetes Pod Scheduling** - The segment explains how Kubernetes’s scheduler assigns pods to worker nodes, uses anti‑affinity rules to keep replicas off the same node, and demonstrates scaling replicas with colored lights and drone icons representing nodes and pods. ## Full Transcript
0:01[Music] 0:09kubernetes makes an application running 0:12in a cluster both available and 0:14responsive by effectively scheduling the 0:16use of infrastructure let's talk 0:18specifically about what makes up our 0:20kubernetes cluster as a reminder the 0:23container for our online travel 0:25application is encapsulated in a pod and 0:27a drone is associated with each pod 0:30kubernetes deploys now worker nodes are 0:33where our application containers 0:34actually run to demonstrate how pods 0:37interact with worker nodes will use 0:39these three lights each light represents 0:41a worker node first keep in mind that a 0:44worker node can be either a virtual or 0:46physical machine kubernetes manages this 0:49underlying infrastructure by default a 0:51scheduler automatically distributes pods 0:53across worker nodes to balance the 0:56available resources appropriately 0:57kubernetes allows you to define affinity 0:59and anti affinity policies to allow a 1:02simple way to assign pods to nodes to 1:05see that in action we'll create a policy 1:07that tells kubernetes to not run 1:09multiple instances of my application on 1:12the same note we'll associate our three 1:14kubernetes worker nodes with the colors 1:16green red and blue when kubernetes 1:19creates a pod and assigns it to a worker 1:21node the drone will change its color to 1:23match the light box and then move to 1:26hover above it we can apply our 1:28deployment with one replica 1:33[Music] 1:38as expected one drone takes off and 1:41moves to the worker node it was assigned 1:43let's scale to three all we have to do 1:46is change our deployment to have three 1:48replicas 1:49[Music] 1:58because we had an anti affinity rule in 2:01place each pod was scheduled to a 2:03different worker node what happens if we 2:05scale to four when we only have three 2:07worker nodes let's find out 2:15[Music] 2:21looks like our fourth drone is a bit 2:24lost 2:25that's because kubernetes is not able to 2:27schedule this pod and it's stuck in a 2:30pending state it's important to 2:33understand and apply your affinity 2:35policies carefully kubernetes allows for 2:38both hard and soft affinity he should 2:40rely and creating soft affinity rules if 2:42you want kubernetes to fall back to its 2:44default scheduling when it's not able to 2:46meet your affinity rules 2:55you