The inter-pod affinity scheduling policy is used to express affinity preferences between pods. Compared with node affinity, inter-pod affinity scheduling limits the nodes to which pods can be scheduled based on the labels of pods that already run on nodes. In an Alibaba Cloud Container Compute Service (ACS) cluster, you can use Kubernetes-native scheduling semantics to implement inter-pod affinity scheduling. By specifying the topology domain and label rules in the podAffinity or podAntiAffinity field, you can schedule pods to the specified topology domain. This topic describes the limits and usage notes of inter-pod affinity scheduling in ACS.
Prerequisites
kube-scheduler is installed and its version meets the following requirements.
ACS cluster version
Scheduler version
1.31
v1.31.0-aliyun-1.2.0 and later
1.30
v1.30.3-aliyun-1.1.1 and later
1.28
v1.28.9-aliyun-1.1.0 and later
acs-virtual-node is installed and its version is v2.12.0-acs.4 or later.
Usage notes
Inter-pod affinity contains the affinity and anti-affinity modes. Both modes have the same protocol format and consist of the requiredDuringSchedulingIgnoredDuringExecution rule and the preferredDuringSchedulingIgnoredDuringExecution rule. The TopologyKey field is required, which corresponds to the label of the virtual node. The following table describes the topology labels supported by ACS for different types of nodes.
Virtual node type | Label | Description | Example |
Regular virtual node | topology.kubernetes.io/zone | Network zone | topology.kubernetes.io/zone: cn-shanghai-b |
GPU-HPN virtual node | topology.kubernetes.io/zone | Network zone | topology.kubernetes.io/zone: cn-shanghai-b |
alibabacloud.com/hpn-zone | High-performance network zone | alibabacloud.com/hpn-zone: B1 |
ACS supports various compute classes. The following constraints exist when you configure other fields of inter-pod affinity for pods of different compute classes.
requiredDuringSchedulingIgnoredDuringExecutionCompute class
Field
Description
Constraint
General-purpose
Performance-enhanced
LabelSelector
This field is used to find matching pods. Pods that match this label selector are counted to determine the number of pods in the topology domain.
GPU-accelerated pods and GPU-HPN pods are not counted.
Namespaces
This field is used to find matching namespaces and can be used together with the LabelSelector field.
NamespaceSelector
This field is similar to the Namespaces field, except it retrieves namespaces based on the namespace label.
GPU-accelerated
GPU-HPN
LabelSelector
This field is used to find matching pods. Pods that match this label selector are counted to determine the number of pods in the topology domain.
General-purpose and performance-enhanced pods are not counted.
Namespaces
This field is used to find matching namespaces and can be used together with the LabelSelector field.
Not supported
NamespaceSelector
This field is similar to the Namespaces field, except it retrieves namespaces based on the namespace label.
Not supported
preferredDuringSchedulingIgnoredDuringExecutionCompute class
Field
Description
Constraint
General-purpose
Performance-enhanced
LabelSelector
Namespaces
NamespaceSelector
None
GPU-accelerated pods and GPU-HPN pods are not counted.
GPU-accelerated
GPU-HPN
All fields
None
Not supported
For more information about the fields, see Inter-pod affinity and anti-affinity.
Examples
The following example shows how to schedule pods to a specific zone by configuring the podAffinity field.
Run the following command to view the nodes in the cluster:
kubectl get nodeExpected output:
NAME STATUS ROLES AGE VERSION virtual-kubelet-cn-hangzhou-i Ready agent 5h42m v1.28.3-xx virtual-kubelet-cn-hangzhou-j Ready agent 5h42m v1.28.3-xxCreate a file named with-affinity-pod.yaml and add the following content to the file:
apiVersion: v1 kind: Pod metadata: labels: pod-affinity-label: with-pod-affinity name: with-affinity-label-pod spec: containers: - args: - 'infinity' command: - sleep image: registry-cn-hangzhou.ack.aliyuncs.com/acs/stress:v1.0.4 imagePullPolicy: IfNotPresent name: stress resources: limits: cpu: '1' memory: 1Gi requests: cpu: '1' memory: 1GiRun the following command to deploy the with-affinity-label-pod file to the cluster:
kubectl apply -f with-affinity-pod.yamlRun the following command to view the distribution results of pods:
kubectl get pod -o wideExpected output:
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES with-affinity-label-pod 1/1 Running 0 75s 192.168.xx.xxx virtual-kubelet-cn-hangzhou-i <none> <none>The output indicates that the pods are scheduled to the cn-hangzhou-i zone.
Create a file named origin-affinity-pod.yaml and add the following content to the file:
apiVersion: apps/v1 kind: Deployment metadata: name: dep-pod-affinity labels: app: pod-affinity-demo spec: replicas: 4 selector: matchLabels: app: pod-affinity-demo template: metadata: labels: app: pod-affinity-demo spec: containers: - name: pod-affinity-demo image: registry-cn-hangzhou.ack.aliyuncs.com/acs/stress:v1.0.4 command: - "sleep" - "infinity" resources: limits: cpu: '1' memory: 1Gi requests: cpu: '1' memory: 1Gi # Specify the affinity between pods. This pod must be deployed in the same zone as the pod with the <pod-affinity-label:with-pod-affinity> label. affinity: podAffinity: requiredDuringSchedulingIgnoredDuringExecution: - labelSelector: matchExpressions: - key: pod-affinity-label operator: In values: - with-pod-affinity topologyKey: topology.kubernetes.io/zoneRun the following command to deploy the origin-affinity-pod.yaml file to the cluster:
kubectl apply -f origin-affinity-pod.yamlRun the following command to view the distribution results of pods:
kubectl get pod -o wideExpected results:
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES dep-pod-affinity-6b9d4f7c87-5jlfx 1/1 Running 0 3m26s 192.168.xx.xxx virtual-kubelet-cn-hangzhou-i <none> <none> dep-pod-affinity-6b9d4f7c87-hwdpc 1/1 Running 0 3m26s 192.168.xx.xxx virtual-kubelet-cn-hangzhou-i <none> <none> dep-pod-affinity-6b9d4f7c87-jfcrq 1/1 Running 0 3m26s 192.168.xx.xxx virtual-kubelet-cn-hangzhou-i <none> <none> dep-pod-affinity-6b9d4f7c87-xwbfr 1/1 Running 0 3m26s 192.168.xx.xxx virtual-kubelet-cn-hangzhou-i <none> <none> with-affinity-label-pod 1/1 Running 0 6m30s 192.168.xx.xxx virtual-kubelet-cn-hangzhou-i <none> <none>The output indicates that all pods are deployed in the same zone.