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

Workload-Workload Anti-Affinity

Using the CCE Console

  1. When Creating a Deployment or Creating a StatefulSet, in the Scheduling Policies area on the Configure Advanced Settings page, choose Inter-Pod Affinity and Anti-affinity > Anti-affinity with Pods > Add.

    Figure 1 Anti-affinity between pods

  2. Select the workloads to which you want to deploy the target workload on a different node, and click OK.

    The workload to be created and the selected workloads will be deployed on different nodes.

Using kubectl

This section uses Nginx as an example to describe how to create a workload using kubectl.

Prerequisites

The ECS where the kubectl client runs has been connected to your cluster. For details, see Connecting to a Cluster Using kubectl.

Procedure

When using kubectl to create a Deployment or using kubectl to create a StatefulSet, configure workload-workload anti-affinity. The following is an example YAML file for workload-workload anti-affinity.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  strategy:
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - image: nginx 
        imagePullPolicy: Always
        name: nginx
      imagePullSecrets:
      - name: default-secret
      affinity:
        podAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
            - matchExpressions:
              - key: app          #workload's label key
                operator: NotIn        
                values:
                - test     #workload's label value

Setting the Object Type After Creating a Workload

  1. Log in to the CCE console and choose Workloads > Deployments or Workloads > StatefulSets in the navigation pane.
  2. Click the name of the workload for which you will add a scheduling policy. On the workload details page, choose Scheduling Policies > Add Simple Scheduling Policy > Add Anti-affinity Object.
  3. Set Object Type to Workload and select the workloads to be deployed on a different node from the created workload. The created workload and the selected workloads will be deployed on different nodes.

    Figure 2 Adding an anti-affinity object – Workload

    This method can be used to add, edit, or delete scheduling policies.