• Cloud Container Engine

cce
  1. Help Center
  2. Cloud Container Engine
  3. User Guide 2.0
  4. Application Management
  5. Affinity and Anti-Affinity Scheduling

Affinity and Anti-Affinity Scheduling

Overview

CCE provides a variety of scheduling policies, including static global scheduling policies and dynamic runtime scheduling policies. You can select or combine these strategies as required. CCE provides the following affinity scheduling modes:

When setting Application-Node Affinity and Anti-Affinity and Application-Application Affinity and Anti-Affinity, ensure that the affinity relationships are not mutually exclusive; otherwise, application deployment will fail. For example, application deployment will fail in the following cases:

  • Anti-affinity is configured for two applications. That is, one application is deployed on one node and a second application is deployed on another node.
  • When a third application is deployed on a third node and goes online, affinity is configured between this application and the second application.
  • Application-AZ Affinity and Anti-Affinity
    • Affinity with AZs: Applications can be deployed in specific AZs.
    • Anti-Affinity with AZs: Applications cannot be deployed in specific AZs.
  • Application-Node Affinity and Anti-Affinity
    • Affinity with Nodes: Applications can be deployed on specific nodes.
    • Anti-Affinity with Nodes: Applications cannot be deployed on specific nodes.
  • Application-Application Affinity and Anti-Affinity: Determines whether applications are deployed on the same or different nodes.
    • Affinity with Applications: Applications are deployed on the same node. You can deploy applications based on service requirements. The nearest route between containers is used to reduce network consumption. For example, Figure 1 shows affinity deployment, in which all apps are deployed on the same node.
      Figure 1 Application affinity
    • Anti-Affinity with Applications: Different applications or multiple instances of the same application are deployed on different nodes. Anti-affinity deployment for multiple instances of the same application reduces the impact of system breakdowns. Anti-affinity deployment for applications can prevent interference between the applications.
      In Figure 2, four apps are deployed on four different nodes. The four applications are deployed in anti-affinity mode.
      Figure 2 Application anti-affinity

Deploying an Application on a Specified Node

Affinity settings are configured during application creation. For details on the application creation procedure, see Creating a Stateless Application or Creating a Stateful Application.

  1. During the application creation process, in the Scheduling Policy area on the Configure Advanced Settings page, choose Application-Node Affinity and Anti-Affinity > Affinity with Nodes. Click Add.
  2. Select the node on which you want to deploy the application, and click OK.

    If multiple nodes are selected, the system automatically chooses one of them during application deployment.

Example YAML for Deploying an Application on a Specified Node

This section uses an Nginx application as an example to describe how to deploy an application on a specified node using kubectl.

Prerequisites

You have configured the kubectl commands to connect an ECS server to your cluster. For details, see Connecting to a Kubernetes Cluster Using kubectl.

Procedure

Create an application and set the affinity attributes for the application as follows. For more information about how to create an application, see Creating a Stateless Application Using kubectl or Creating a Stateful Application Using kubectl.

apiVersion: extensions/v1beta1
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:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
            - matchExpressions:
              - key: nodeName          #Label key of a node
                operator: In
                values:
                - test-node-1          #Key value of a node

Deploying an Application with Node Anti-Affinity

Affinity settings are configured during application creation. For details on the application creation procedure, see Creating a Stateless Application or Creating a Stateful Application.

  1. During the application creation process, in the Scheduling Policy area on the Configure Advanced Settings page, choose Application-Node Affinity and Anti-Affinity > Anti-Affinity with Nodes. Click Add.
  2. Select the node on which you do not want to deploy the application, and click OK.

    If multiple nodes are selected, the application will not be deployed on any of these nodes.

Example YAML for Deploying an Application with Node Anti-Affinity

This section uses an Nginx application as an example to describe how to deploy an application with node anti-affinity using kubectl.

Procedure

You have configured the kubectl commands to connect an ECS server to your cluster. For details, see Connecting to a Kubernetes Cluster Using kubectl.

Procedure

Create an application and set the affinity attributes for the application as follows. For more information about how to create an application, see Creating a Stateless Application Using kubectl or Creating a Stateful Application Using kubectl.

apiVersion: extensions/v1beta1
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:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
            - matchExpressions:
              - key: nodeName          # Label key of a node.
                operator: NotIn        # Indicates that the application will not be deployed on the node. 
                values:
                - test-node-1          # Key value of a node.

Deploying Applications on the Same Node

Affinity settings are configured during application creation. For details on the application creation procedure, see Creating a Stateless Application or Creating a Stateful Application.

  1. During the application creation process, in the Scheduling Policy area on the Configure Advanced Settings page, choose Application-Application Affinity and Anti-Affinity > Affinity with Applications. Click Add.
  2. Select the applications that you want to deploy on the same node as the created application, and click OK.

    The created application will be deployed on the same node as the selected applications.

Example YAML for Deploying Applications on the Same Node

This section uses an Nginx application as an example to describe how to deploy an application using kubectl.

Procedure

You have configured the kubectl commands to connect an ECS server to your cluster. For details, see Connecting to a Kubernetes Cluster Using kubectl.

Procedure

Create an application and set the affinity attributes for the application as follows. For more information about how to create an application, see Creating a Stateless Application Using kubectl or Creating a Stateful Application Using kubectl.

apiVersion: extensions/v1beta1
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          # Label key of an application.
                operator: In        
                values:
                - test     # Label value of an application.

Deploying Applications on Different Nodes

Affinity settings are configured during application creation. For details on the application creation procedure, see Creating a Stateless Application or Creating a Stateful Application.

  1. During the application creation process, in the Scheduling Policy area on the Configure Advanced Settings page, choose Application-Application Affinity and Anti-Affinity > Anti-Affinity with Applications. Click Add.
  2. Select the applications that you do not want to deploy on the same node as the created application, and click OK.

    The created application and the selected applications will be deployed on different nodes.

Example YAML for Deploying Applications on Different Nodes

This section uses an Nginx application as an example to describe how to deploy an application using kubectl.

Procedure

You have configured the kubectl commands to connect an ECS server to your cluster. For details, see Connecting to a Kubernetes Cluster Using kubectl.

Procedure

Create an application and set the affinity attributes for the application as follows. For more information about how to create an application, see Creating a Stateless Application Using kubectl or Creating a Stateful Application Using kubectl.

apiVersion: extensions/v1beta1
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          # Label key of an application.
                operator: NotIn        
                values:
                - test     # Label value of an application.

Deploying an Application in a Specified AZ

Affinity settings are configured during application creation. For details on the application creation procedure, see Creating a Stateless Application or Creating a Stateful Application.

  1. During the application creation process, in the Scheduling Policy area on the Configure Advanced Settings page, choose Application-AZ Affinity and Anti-Affinity. Click next to Affinity with AZs.
  2. Click the AZ in which you want to deploy the application.

    The created application will be deployed in the selected AZ.

Example YAML for Deploying an Application in a Specified AZ

This section uses an Nginx application as an example to describe how to deploy an application using kubectl.

Procedure

You have configured the kubectl commands to connect an ECS server to your cluster. For details, see Connecting to a Kubernetes Cluster Using kubectl.

Procedure

Create an application and set the affinity attributes for the application as follows. For more information about how to create an application, see Creating a Stateless Application Using kubectl or Creating a Stateful Application Using kubectl.

apiVersion: extensions/v1beta1
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:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
            - matchExpressions:
              - key: kubernetes.io/availablezone        # Label key of a node.
                operator: NotIn        
                values:
                - eu-de-01             # Key value of a node.

Deploying an Application with AZ Anti-Affinity

Affinity settings are configured during application creation. For details on the application creation procedure, see Creating a Stateless Application or Creating a Stateful Application.

  1. During the application creation process, in the Scheduling Policy area on the Configure Advanced Settings page, choose Application-AZ Affinity and Anti-Affinity. Click next to Anti-Affinity with AZs.
  2. Click the AZ in which you do not want to deploy the application.

    The created application will not be deployed in the selected AZ.

Example YAML for Deploying an Application with AZ Anti-Affinity

This section uses an Nginx application as an example to describe how to deploy an application with AZ anti-affinity using kubectl.

Procedure

You have configured the kubectl commands to connect an ECS server to your cluster. For details, see Connecting to a Kubernetes Cluster Using kubectl.

Procedure

Create an application and set the affinity attributes for the application as follows. For more information about how to create an application, see Creating a Stateless Application Using kubectl or Creating a Stateful Application Using kubectl.

apiVersion: extensions/v1beta1
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:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
            - matchExpressions:
              - key: kubernetes.io/availablezone       # Label key of a node.  
                operator: NotIn        
                values:
                - eu-de-01           # Key value of a node.