Scheduling and Placement - kubernetes/kubernetes

Kubernetes is an open-source platform for automating deployment, scaling, and management of containerized applications. It has a built-in scheduler called kube-scheduler that is responsible for assigning Pods to Nodes, taking into account resource availability, constraints, and other factors. This process of scheduling and placement can be influenced by various parameters and settings.

Node Affinity and Anti-Affinity

Node affinity and anti-affinity are used to specify preferences or requirements for scheduling Pods on specific nodes. Node affinity allows you to define rules for scheduling Pods on nodes with particular labels, while node anti-affinity helps you avoid scheduling Pods on the same node or in the same availability zone.

Here’s an example of a Pod specification using node affinity:

spec:
affinity:
nodeAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- preference:
matchExpressions:
- key: kubernetes.io/e2e-az-name
operator: In
values:
- e2e-az1
weight: 1

Resource Requests and Limits

Pods can specify resource requests and limits for CPU and memory. These values are used by the scheduler to determine if a node has sufficient resources to run the Pod. If a node does not have enough resources, the Pod will not be scheduled on that node.

Here’s an example of a Pod specification using resource requests and limits:

spec:
containers:
- name: my-container
resources:
requests:
cpu: 100m
memory: "128Mi"
limits:
cpu: 200m
memory: "256Mi"

Custom Schedulers

Kubernetes allows you to create custom schedulers that can implement more advanced scheduling strategies. Custom schedulers can be used to schedule Pods based on custom criteria, such as hardware requirements, application dependencies, or data locality.

Here’s an example of a custom scheduler configuration:

apiVersion: v1
kind: Pod
metadata:
name: my-pod
spec:
schedulerName: my-custom-scheduler
containers:
- name: my-container
image: my-image

Multi-Cluster Scheduling

Kubernetes can also be used to schedule Pods across multiple clusters. This can be useful for geographically distributed applications, disaster recovery, or load balancing.

Here’s an example of a Placement specification for multi-cluster scheduling:

apiVersion: placement.open-cluster-management.io/v1alpha1
kind: Placement
metadata:
name: my-placement
spec:
clusterConditions:
- status: "True"
type: "Normal"
clusterSelector:
matchExpressions:
- key: "topology.kubernetes.io/region"
operator: In
values:
- us-east-1
- us-west-2
clusterSets:
- name: my-cluster-set

Sources: