CodeBlanks Logo Icon CodeBlanks

kube-scheduler

Learn that kube-scheduler assigns Pods to nodes.

kube-scheduler

kube-scheduler watches pods whose spec.nodeName is empty, picks a node for each and writes it to the pod (binding). The selection has two phases:

  1. Filtering: nodes that can't take the pod are eliminated (insufficient resources, taints, nodeSelector, affinity, port conflicts).
  2. Scoring: the remaining ones are scored (balanced resource use, the image being on the node, spreading) and the highest-scoring is chosen.
$ kubectl get events --field-selector reason=Scheduled | tail -1
Normal  Scheduled  pod/api-7f8c9  Successfully assigned magaza/api-7f8c9 to worker-2

Steering placement

The pod spec gives the scheduler hints. podAntiAffinity spreads replicas of the same app across different nodes; topologySpreadConstraints balances replicas across zones:

    spec:
      affinity:
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            - labelSelector:
                matchLabels: {app: api}
              topologyKey: kubernetes.io/hostname
      topologySpreadConstraints:
        - maxSkew: 1
          topologyKey: topology.kubernetes.io/zone
          whenUnsatisfiable: ScheduleAnyway
          labelSelector:
            matchLabels: {app: api}

Practice Separate the replicas

Write a pod template's affinity section (starting with affinity:): under podAntiAffinity.requiredDuringSchedulingIgnoredDuringExecution, labelSelector.matchLabels app: api and topologyKey: kubernetes.io/hostname.

Starter code

terminal
# buraya yaz

Sample solution

terminal
affinity:
  podAntiAffinity:
    requiredDuringSchedulingIgnoredDuringExecution:
      - labelSelector:
          matchLabels:
            app: api
        topologyKey: kubernetes.io/hostname

Expected output

deployment.apps/api configured
$ kubectl get pods -l app=api -o wide
NAME             READY   STATUS    NODE
api-6b5c4-abcde  1/1     Running   worker-1
api-6b5c4-fghij  1/1     Running   worker-2
api-6b5c4-klmno  1/1     Running   worker-3

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 If no node remains after filtering, what happens to the pod?

  • It's placed on a random node
  • It's deleted
  • It's placed on the control plane
  • It stays Pending; a FailedScheduling event is written Correct answer

Explanation: The scheduler retries when resources free up or a node is added.

2 Write the key that spreads replicas across nodes.

Question
              topologyKey: kubernetes.io/____
Answer
              topologyKey: kubernetes.io/hostname

Explanation: The hostname label differs on every node.

3 Match.

Term Meaning
filtreleme Eliminate unfit ones
puanlama Pick the best
podAntiAffinity Separate replicas
topologySpreadConstraints Spread across zones

All lessons in Expert