Skip to main content

Chapter 2.5 - Scheduler

Learning Objectives

By the end of this chapter, you will be able to:

  • Understand the role of the Scheduler
  • Explain the scheduling process
  • Understand filters and scores
  • Identify scheduling constraints

What is the Scheduler?

The Scheduler (kube-scheduler) is responsible for assigning unscheduled Pods to appropriate nodes in the cluster. It takes into account resource constraints, affinities, and policies.


Scheduling Process

Scheduling Steps

1. Filtering

Eliminates nodes that cannot run the Pod:

  • Predicate 1: Sufficient resources (CPU, memory)
  • Predicate 2: Affinities/anti-affinities respected
  • Predicate 3: Taints and tolerations
  • Predicate 4: Node selectors

2. Scoring (Prioritization)

Scores each eligible node (0-10):

  • Priority 1: Resource balancing
  • Priority 2: Affinity with other Pods
  • Priority 3: Data locality
  • Priority 4: User preferences

3. Selection

The node with the highest score is selected.


Scheduling Example

Scenario

apiVersion: v1
kind: Pod
metadata:
name: my-pod
spec:
containers:
- name: app
image: nginx
resources:
requests:
cpu: "500m"
memory: "512Mi"

Process


Scheduling Constraints

1. Resources (Requests/Limits)

resources:
requests:
cpu: "500m"
memory: "512Mi"
limits:
cpu: "1000m"
memory: "1Gi"

The Scheduler verifies that the node has enough available resources.

2. Node Selectors

spec:
nodeSelector:
disktype: ssd
zone: us-east-1a

The Pod will only be scheduled on nodes with these labels.

3. Affinities (Affinity)

affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: zone
operator: In
values:
- us-east-1a

4. Taints and Tolerations

# Taint on the node
kubectl taint nodes node1 key=value:NoSchedule

# Toleration in the Pod
tolerations:
- key: "key"
operator: "Equal"
value: "value"
effect: "NoSchedule"

Scheduling Strategies

Resource Balancing

The Scheduler tries to balance load across nodes:

Locality Preferences

The Scheduler prefers scheduling Pods near their data (zone affinity).


Custom Scheduler

You can create your own scheduler for specific needs:

apiVersion: v1
kind: Pod
spec:
schedulerName: my-custom-scheduler
containers:
- name: app
image: nginx

Useful Commands

# View Scheduler logs
kubectl logs -n kube-system kube-scheduler-<node>

# See why a Pod is not scheduled
kubectl describe pod <pod-name>

# View scheduling events
kubectl get events --field-selector involvedObject.name=<pod-name>

Summary

In this chapter, you learned:

Scheduler: Assigns Pods to appropriate nodes
Process: Filtering -> Scoring -> Selection -> Bind
Constraints: Resources, selectors, affinities, taints
Balancing: Optimal load distribution
Customization: Custom schedulers are possible


Next Steps

Now that you understand the Scheduler:

Chapter 2.6: kubelet - Worker Node Agent
Chapter 2.7: kube-proxy


Chapter created: December 2024