Kubernetes
Pod Distribution
Node Management
Cluster Management
Container Orchestration

Kubernetes pod distribution amongst nodes

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Overview

Kubernetes, often abbreviated as K8s, is an open-source platform for automating deployment, scaling, and operation of application containers. It groups containers that make up an application into logical units for easy management and discovery. A key feature of Kubernetes is how it orchestrates and distributes workloads across a cluster—a set of worker machines, called nodes.

Understanding Nodes and Pods

Nodes are the worker machines in Kubernetes. They can be either virtual or physical machines, and they run the pod definitions.

Pods are the smallest deployable units in Kubernetes. A Pod encapsulates one or more containers, storage resources, a unique network IP, and options for how the container(s) should run.

How Pods are Distributed Across Nodes

Pod distribution is crucial for ensuring application resilience, high availability, and optimal resource utilization. Kubernetes uses a control plane component called the Scheduler to decide where a Pod should be placed on the cluster.

Scheduler's Workflow

  1. Node Eligibility: When a new Pod needs to be scheduled, Kubernetes first narrows down the list of nodes that meet the Pod's resource requests such as CPU and memory requirements via a process called filtering.
  2. Ranking Nodes: In the subsequent phase known as scoring, eligible nodes are ranked to find the most suitable one. The Scheduler uses various algorithms and preferences to determine this ranking.
  3. Binding: Finally, the Scheduler assigns the Pod to the best-fit node and updates the cluster state to reflect this binding.

Factors Influencing Pod Placement

  1. Resource Requests and Limits: Pods have specific resource requirements defined such as CPU and memory. These demands must be met by the target node.
  2. Node Affinity/Anti-affinity: These are rules that allow Pods to specify desirable node characteristics or constraints against nodes.
  3. Pod Affinity/Anti-affinity: Allows the creation of rules about which Pods should (or should not) be co-located.
  4. Taints and Tolerations: Nodes can be "tainted," meaning Pods that don't tolerate those taints will not be scheduled on such nodes.
  5. Custom Schedulers: Users can implement custom schedulers to override Kubernetes' default policies.

Advanced Distribution Techniques

Horizontal Pod Autoscaler

The Horizontal Pod Autoscaler automatically scales the number of Pods in a deployment or replication controller based on observed CPU utilization or other select metrics.

Node Pools

Clusters can be broken into node pools where each pool can have different sizes, resource capacities, and labels to accommodate different workloads.

DaemonSets

DaemonSets ensure that there is a running instance of a Pod on each (or some) Pi node.

Example Scenario

Let's consider a cluster consisting of three nodes: `node-1`, `node-2`, and `node-3`. Suppose we have a Deployment that specifies a desired state of three replicas of a specific Pod and it requires minimal CPU and memory resources.

  1. Initial Distribution: All three nodes meet the resource requirements for the Pods. The Scheduler evenly distributes the Pods across the nodes for optimal load balancing:
    • Pod-A on `node-1`
    • Pod-B on `node-2`
    • Pod-C on `node-3`
  2. Node Affinity Application: Suppose Pod-B is preferred to run on nodes with GPU support (`node-2` and `node-3` have this capability labeled).
  3. Post-Distribution: If additional Pods are required, such as a 4th, and `node-1` achieves higher load, Pod-D would logically be placed on `node-2` or `node-3` if they have available resources.

Key Concepts Summary

FeatureDescription
PodThe smallest deployable units of computing in Kubernetes, which can encapsulate one or more containers.
NodeA worker machine in Kubernetes where Pods are scheduled.
SchedulerThe component responsible for placing Pods onto available Nodes.
Resource Requests & LimitsDefine necessary CPU and memory resources for Pods.
Affinity & Anti-affinityControls to encourage (or discourage) co-location of Pods or Pod allocation to specific Nodes.
Taints & TolerationsMechanism that prevents deployment of Pods on unsuitable Nodes unless they can tolerate them.
AutoscalerAutomatically adjusts the number of Pods in a deployment based on usage metrics.
DaemonSetEnsures Pods run on all or select Nodes in a DaemonSet configuration.

Conclusion

Understanding Pod distribution in Kubernetes is vital to optimizing the deployment of applications across a distributed system. This orchestration mechanism helps maintain high availability, optimize resource use, and ensure resilience against individual node failures, thereby enabling the smooth running of containerized applications at scale. Whether employing custom schedulers or default strategies like affinities and autoscalers, Kubernetes offers flexibility to adapt its scheduling policies to meet specific operational needs.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.