High availability and high performance are essential to distributed jobs. In an ACK Pro cluster, you can spread distributed jobs across zones based on Kubernetes-native scheduling semantics. You can also configure affinities to deploy distributed jobs in specific zones based on Kubernetes-native scheduling semantics. This improves the efficiency of job deployment. This topic describes how to spread Elastic Container Instance-based pods across zones and configure affinities.
To spread Elastic Container Instance-based pods across zones, submit a ticket to apply to be added to the whitelist.
Table of contents
Prerequisites
An ACK Pro cluster is created. The Kubernetes version of the cluster is later than 1.20. For more information, see Create an ACK managed cluster.
The kube-scheduler component in the ACK Pro cluster meets the requirements in the following table.
Kubernetes version
kube-scheduler version (minimum requirement)
1.26.x
1.26.3-aliyun-5.2
1.24.x
1.24.6-aliyun-5.2
1.22.x
1.22.15-aliyun-5.2
< 1.22
NoteTo spread Elastic Container Instance-based pods across zones and configure affinities, update the Kubernetes version of the cluster. For more information, see Update an ACK cluster.
Not supported
NoteTo spread Elastic Container Instance-based pods across zones and configure affinities, update the version of kube-scheduler. For more information, see kube-scheduler.
When you schedule Elastic Container Instance-based pods, make sure that you have specified the desired vSwitches in Elastic Container Instance Profile.
Elastic Container Instance-based pods can be spread across zones only when the
nodeAffinity
,podAffinity
,tolerations
, andtopologySpreadConstraints
parameters are configured for the pods or the pods match an existing resource policy.
Precautions
When you spread Elastic Container Instance-based pods across zones, you can set the
topologyKey
parameter only totopology.kubernetes.io/zone
.When you spread Elastic Container Instance-based pods across zones, you cannot add annotations to specify the priorities of the vSwitches to which pods are spread. If you have specified the priorities of vSwitches to which a pod can be spread, the pod cannot be spread across zones.
You cannot spread Elastic Container Instance-based pods across zones in FastFailed mode. If the FastFailed mode is enabled for a pod, the pod cannot be spread across zones.
Terms
For more information about pod topology spread and affinities, see Pod Topology Spread Constraints and Assigning Pods to Nodes.
Spread Elastic Container Instance-based pods across zones and configure affinities
The following examples show how to spread Elastic Container Instance-based pods across zones and configure affinities in an ACK Pro cluster that runs Kubernetes 1.22.
Example 1: Use topology spread constraints to spread Elastic Container Instance-based pods across zones
Add a topology spread constraint to the configuration of a workload.
Perform the following steps to specify a topology spread constraint in the
Spec
parameter in the configuration of a pod or theSpec
parameter in the configuration of a workload, such as a Deployment or Job.topologySpreadConstraints: - maxSkew: <integer> minDomains: <integer> # This parameter is optional and is in the Beta phase in Kubernetes 1.25 and later. topologyKey: <string> whenUnsatisfiable: <string> labelSelector: <object> matchLabelKeys: <list> # This parameter is optional and is in the Beta phase in Kubernetes 1.27 and later. nodeAffinityPolicy: [Honor|Ignore] # This parameter is optional and is in the Beta phase in Kubernetes 1.26 and later. nodeTaintsPolicy: [Honor|Ignore] # This parameter is optional and is in the Beta phase in Kubernetes 1.26 and later.
In this example, a Deployment whose pods are evenly distributed to multiple zones is created. The following code block shows the YAML template of the Deployment:
Parameter
Description
preferredDuringSchedulingIgnoredDuringExecution: - weight: 1 preference: matchExpressions: - key: type operator: NotIn values: - virtual-kubelet
The configuration specifies that the pods are preferentially scheduled to ECS nodes.
For more information about the parameters, see Node affinity.
topologySpreadConstraints: - maxSkew: 1 topologyKey: topology.kubernetes.io/zone whenUnsatisfiable: DoNotSchedule labelSelector: matchLabels: app: with-pod-topology-spread
The configuration specifies that the pods are evenly deployed across multiple zones.
For more information about the parameters, see topologySpreadConstraints field.
tolerations: - key: "virtual-kubelet.io/provider" operator: "Exists" effect: "NoSchedule"
kube-scheduler tolerates the taint of virtual nodes to schedule pods to the virtual nodes.
For more information about the parameters, see Taints and Tolerations.
NoteIf you want to schedule pods to ARM-based virtual nodes, you must add a toleration to the pods to tolerate the taint of the ARM-based virtual nodes.
Create a workload.
Create a file named
deployment.yaml
and copy the preceding YAML template to the file. Then, run the following command to create a Deployment in the cluster:kubectl apply -f deployment.yaml
Verify the scheduling result of the workload.
Run the following command to query the nodes on which the Deployment deploys the pods:
kubectl get po -lapp=with-pod-topology-spread -ocustom-columns=NAME:.metadata.name,NODE:.spec.nodeName --no-headers | grep -v "<none>"
Run the following command to query the number of pods that are created by the Deployment in each zone:
kubectl get po -lapp=with-pod-topology-spread -ocustom-columns=NODE:.spec.nodeName --no-headers | grep -v "<none>" | xargs -I {} kubectl get no {} -ojson | jq '.metadata.labels["topology.kubernetes.io/zone"]' | sort | uniq -c
Example 2: Configure pod affinities and node affinities to deploy pods in specific zones
Add affinities to the configuration of a workload.
In this example, a Deployment whose pods are deployed in a single zone is created. The following code block shows the YAML template of the Deployment:
Parameter
Description
podAffinity: requiredDuringSchedulingIgnoredDuringExecution: - labelSelector: matchExpressions: - key: app operator: In values: - with-affinity topologyKey: topology.kubernetes.io/zone
The configuration specifies that all the pods are deployed in a single zone.
For more information about the parameters, see Node affinity.
nodeAffinity: preferredDuringSchedulingIgnoredDuringExecution: - weight: 1 preference: matchExpressions: - key: type operator: NotIn values: - virtual-kubelet
The configuration specifies that the pods are preferentially scheduled to ECS nodes.
For more information about the parameters, see Node affinity.
tolerations: - key: "virtual-kubelet.io/provider" operator: "Exists" effect: "NoSchedule"
kube-scheduler tolerates the taint of virtual nodes to schedule pods to the virtual nodes.
For more information about the parameters, see Taints and Tolerations.
If you want to deploy the pods in a specific zone, delete the
podAffinity
parameter and add the following constraint to thenodeAffinity
parameter: The following configuration specifies that the pods must be deployed in Beijing Zone A.requiredDuringSchedulingIgnoredDuringExecution: nodeSelectorTerms: - matchExpressions: - key: topology.kubernetes.io/zone operator: In values: - cn-beijing-a
Create a workload.
Create a file named
deployment.yaml
and copy the preceding YAML template to the file. Then, run the following command to create a Deployment in the cluster:kubectl apply -f deployment.yaml
Verify the scheduling result of the workload.
Run the following command to query the nodes on which the Deployment deploys the pods:
kubectl get po -lapp=with-affinity -ocustom-columns=NAME:.metadata.name,NODE:.spec.nodeName --no-headers | grep -v "<none>"
Run the following command to query the number of pods that are created by the Deployment in each zone:
kubectl get po -lapp=with-affinity -ocustom-columns=NODE:.spec.nodeName --no-headers | grep -v "<none>" | xargs -I {} kubectl get no {} -ojson | jq '.metadata.labels["topology.kubernetes.io/zone"]' | sort | uniq -c
Strict Elastic Container Instance-based pod topology spread
By default, if you force the system to spread Elastic Container Instance-based pods across zones, kube-scheduler evenly deploys the pods of a workload across all zones. However, Elastic Container Instance-based pods may fail to be created in some zones. The following figure shows the scheduling result when the maxSkew parameter is set to 1. For more information about maxSkew, see maxSkew.
If the Elastic Container Instance-based pods in Zone B and Zone C fail to be created, two Elastic Container Instance-based pods will run in Zone A, whereas no Elastic Container Instance-based pod runs in Zone B or Zone C. This violates the constraint specified by the maxSkew parameter.
In an ACK Pro cluster, you can enable strict Elastic Container Instance-based pod topology spread to ensure that pods are strictly spread across zones. After you enable strict Elastic Container Instance-based pod topology spread, kube-scheduler first schedules a pod to each of Zone A, Zone B, and Zone C. kube-scheduler does not schedule pending pods until the scheduled pods are successfully created, as shown in the following figure.
Even if Pod A1 is successfully created, pending pods are not scheduled. This is because if the pod in Zone B or Zone C fails to be created, the constraint specified by the maxSkew parameter is violated. After Pod B1 is successfully created, kube-scheduler schedules a pod to Zone C. The following figure shows the scheduling result. Pods with orange shading are successfully created.
If you want to disable strict Elastic Container Instance-based pod topology spread, set the whenUnsatisfiable
parameter to ScheduleAnyway
. For more information, see Spread constraint definition.