Updated on 2024-11-12 GMT+08:00

Overview

Kubernetes schedules workloads based on pods. After you create a workload, the scheduler automatically assigns pods. For example, the scheduler distributes pods to nodes that have enough resources.

While the default scheduler behavior is sufficient for most needs, there may be situations where you require more precise control over where pods are deployed. To address this, Kubernetes enables you to configure scheduling policies when creating workloads. For example, you may need to:

  • Deploy a frontend application and a backend application on the same node. This reduces latency because the pods of both applications can use the physical resources of the node.
  • Deploy a certain type of applications on specific nodes to ensure that these critical applications always run on the best hardware or configuration.
  • Deploy different applications on separate nodes to isolate them from each other. This prevents any issues with one application from affecting others.

Use the methods listed in the following table to select a pod scheduling policy in Kubernetes.

Table 1 Workload scheduling policies

Scheduling Policy

YAML Field

Description

Reference

Node selector

nodeSelector

A basic scheduling mode, in which Kubernetes selects the target node according to node labels. This means that pods are only scheduled to the node that has the specific label.

Configuring Specified Node Scheduling (nodeSelector)

Node affinity

nodeAffinity

Node affinity is more expressive than nodeSelector. It allows you to use Label Selectors to filter nodes that require affinity based on their labels and choose between Required and Preferred for Affinity Rules.

NOTE:

When both nodeSelector and nodeAffinity are specified, a pod can only be scheduled to a candidate node if both conditions are met.

Configuring Node Affinity Scheduling (nodeAffinity)

Workload affinity or anti-affinity scheduling

podAffinity/podAntiAffinity

You can use Label Selectors to filter pods that require affinity or anti-affinity based on workload labels. You can then determine whether to schedule new workload pods to the same node (or node group) as the target pod or not. Additionally, you can choose between Required and Preferred for Affinity Rules.

NOTE:

Workload affinity and anti-affinity require a certain amount of computing time, which significantly slows down scheduling in large-scale clusters. Do not enable workload affinity and anti-affinity in a cluster that contains hundreds of nodes.

Configuring Workload Affinity or Anti-affinity Scheduling (podAffinity or podAntiAffinity)

Affinity Rules

Scheduling policies that use node affinity or workload affinity/anti-affinity can include both hard and soft constraints to meet complex scheduling requirements. Hard constraints must be met, while soft constraints should be met as much as possible.

Table 2 Affinity rules

Rule Type

YAML Field

Description

Example

Required

requiredDuringSchedulingIgnoredDuringExecution

Hard constraint that must be met. The scheduler can perform scheduling only when the rule is met.

Preferred

preferredDuringSchedulingIgnoredDuringExecution

Soft constraint. The scheduler tries to locate the target object that satisfies the target rule. The scheduler will schedule the pod even if it cannot find a matching target object that satisfies the target rule.

When using preferred affinity, you can set a weight field ranging from 1 to 100 for each pod. Assigning a higher weight to a pod will increase its priority in the scheduling process.

The YAML field requiredDuringScheduling or preferredDuringScheduling in the affinity rules above indicates that a label rule must be forcibly met or needs to be met as much as possible during scheduling. IgnoredDuringExecution indicates that any changes to the node label after Kubernetes schedules the pod will not affect the pod's running or cause it to be rescheduled. However, if kubelet on the node is restarted, kubelet will recheck the node affinity rule, and the pod will still be scheduled to another node.

Label Selectors

When creating a scheduling policy use the logical operators of a label selector to filter label values and identify the objects that need affinity or anti-affinity.

Table 3 Label selectors

Parameter

Description

YAML Example

key

Label key. The objects that meet the filter criteria must contain the label of the key, and the label value must meet the operation relationship between the label value list (values field) and logical operators.

In the example below, objects that meet the filter criteria must have a label with a key of topology.kubernetes.io/zone and a value of either az1 or az2.

matchExpressions:
  - key: topology.kubernetes.io/zone
    operator: In
    values:
    - az1
    - az2

operator

Logical operator that can be used to determine filtering rules for label values. Options:

  • In: The label of the affinity or anti-affinity object is in the label value list (values field).
  • NotIn: The label of the affinity or anti-affinity object is not in the label value list (values field).
  • Exists: The affinity or anti-affinity object has a specified label key. There is no need to configure the label value list (values field).
  • DoesNotExist: The affinity or anti-affinity object does not have a specified label key. There is no need to configure the label value list (values field).
  • Gt: (available only for node affinity) The label value of the scheduled node is greater than what is listed (string comparison).
  • Lt: (available only for node affinity) The label value of the scheduled node is less than what is listed (string comparison).

values

Label values