Compute
Elastic Cloud Server
Huawei Cloud Flexus
Bare Metal Server
Auto Scaling
Image Management Service
Dedicated Host
FunctionGraph
Cloud Phone Host
Huawei Cloud EulerOS
Networking
Virtual Private Cloud
Elastic IP
Elastic Load Balance
NAT Gateway
Direct Connect
Virtual Private Network
VPC Endpoint
Cloud Connect
Enterprise Router
Enterprise Switch
Global Accelerator
Management & Governance
Cloud Eye
Identity and Access Management
Cloud Trace Service
Resource Formation Service
Tag Management Service
Log Tank Service
Config
OneAccess
Resource Access Manager
Simple Message Notification
Application Performance Management
Application Operations Management
Organizations
Optimization Advisor
IAM Identity Center
Cloud Operations Center
Resource Governance Center
Migration
Server Migration Service
Object Storage Migration Service
Cloud Data Migration
Migration Center
Cloud Ecosystem
KooGallery
Partner Center
User Support
My Account
Billing Center
Cost Center
Resource Center
Enterprise Management
Service Tickets
HUAWEI CLOUD (International) FAQs
ICP Filing
Support Plans
My Credentials
Customer Operation Capabilities
Partner Support Plans
Professional Services
Analytics
MapReduce Service
Data Lake Insight
CloudTable Service
Cloud Search Service
Data Lake Visualization
Data Ingestion Service
GaussDB(DWS)
DataArts Studio
Data Lake Factory
DataArts Lake Formation
IoT
IoT Device Access
Others
Product Pricing Details
System Permissions
Console Quick Start
Common FAQs
Instructions for Associating with a HUAWEI CLOUD Partner
Message Center
Security & Compliance
Security Technologies and Applications
Web Application Firewall
Host Security Service
Cloud Firewall
SecMaster
Anti-DDoS Service
Data Encryption Workshop
Database Security Service
Cloud Bastion Host
Data Security Center
Cloud Certificate Manager
Edge Security
Situation Awareness
Managed Threat Detection
Blockchain
Blockchain Service
Web3 Node Engine Service
Media Services
Media Processing Center
Video On Demand
Live
SparkRTC
MetaStudio
Storage
Object Storage Service
Elastic Volume Service
Cloud Backup and Recovery
Storage Disaster Recovery Service
Scalable File Service Turbo
Scalable File Service
Volume Backup Service
Cloud Server Backup Service
Data Express Service
Dedicated Distributed Storage Service
Containers
Cloud Container Engine
SoftWare Repository for Container
Application Service Mesh
Ubiquitous Cloud Native Service
Cloud Container Instance
Databases
Relational Database Service
Document Database Service
Data Admin Service
Data Replication Service
GeminiDB
GaussDB
Distributed Database Middleware
Database and Application Migration UGO
TaurusDB
Middleware
Distributed Cache Service
API Gateway
Distributed Message Service for Kafka
Distributed Message Service for RabbitMQ
Distributed Message Service for RocketMQ
Cloud Service Engine
Multi-Site High Availability Service
EventGrid
Dedicated Cloud
Dedicated Computing Cluster
Business Applications
Workspace
ROMA Connect
Message & SMS
Domain Name Service
Edge Data Center Management
Meeting
AI
Face Recognition Service
Graph Engine Service
Content Moderation
Image Recognition
Optical Character Recognition
ModelArts
ImageSearch
Conversational Bot Service
Speech Interaction Service
Huawei HiLens
Video Intelligent Analysis Service
Developer Tools
SDK Developer Guide
API Request Signing Guide
Terraform
Koo Command Line Interface
Content Delivery & Edge Computing
Content Delivery Network
Intelligent EdgeFabric
CloudPond
Intelligent EdgeCloud
Solutions
SAP Cloud
High Performance Computing
Developer Services
ServiceStage
CodeArts
CodeArts PerfTest
CodeArts Req
CodeArts Pipeline
CodeArts Build
CodeArts Deploy
CodeArts Artifact
CodeArts TestPlan
CodeArts Check
CodeArts Repo
Cloud Application Engine
MacroVerse aPaaS
KooMessage
KooPhone
KooDrive
Help Center/ Cloud Container Engine/ User Guide/ Workloads/ Scheduling a Workload/ Configuring Node Affinity Scheduling (nodeAffinity)

Configuring Node Affinity Scheduling (nodeAffinity)

Updated on 2025-02-18 GMT+08:00

Kubernetes can schedule workload pods to affinity nodes based on their labels and label values. For example, some nodes support GPU computing, and node affinity scheduling can guarantee that high-performance computing pods run on these GPU nodes.

Configuring a Node Affinity Scheduling Policy

There are various ways to configure node affinity scheduling policies to schedule pods to nodes that meet specific requirements.

In this example, a GPU node labeled with gpu=true has been created in the cluster. You can use this label to schedule pods to this GPU node.

  1. Log in to the CCE console.
  2. Click the cluster name to go to the cluster console, choose Workloads in the navigation pane, and click the Create Workload in the upper right corner.
  3. In Advanced Settings, choose Scheduling and select a policy for Node Affinity. In this example, Custom policies is selected. For details about how to create a workload, see Creating a Workload.

    Table 1 Scheduling policies

    Parameter

    Description

    Example

    Node Affinity

    • Not configured: No node affinity policy is configured.
    • Node Affinity: Specify the nodes where workload pods are to be deployed. If no nodes are specified, the pods will be randomly scheduled according to the default cluster scheduling policy.
    • Specified Node Pool Scheduling: Specify the node pools where workload pods are to be deployed. If no node pools are specified, the pods will be randomly scheduled according to the default cluster scheduling policy.
    • Custom policies: allow flexible scheduling of workload pods based on node labels. For details about the supported affinity policies, see Table 2. Select a proper policy type and add a policy. For details about the parameters, see Table 3.

    Custom policies

  4. Select a proper node affinity rule and click to add a scheduling policy. In this example, a scheduling policy is added in the Required area under Node Affinity. This policy specifies that the newly created workload can only be scheduled to a specific node.

    Table 2 Affinity rule

    Parameter

    Description

    Example

    Node Affinity

    • Required: indicates the hard constraint (requiredDuringSchedulingIgnoredDuringExecution) that must be met.

      If multiple rules that must be met are added, scheduling will be performed when only one rule is met.

    • Preferred: indicates the soft constraint (preferredDuringSchedulingIgnoredDuringExecution) that need to be met as much as possible.

      If multiple rules that are preferentially met are added, scheduling will be performed even if one or none of the rules is met.

    Required

  5. In the dialog box that is displayed on the right, click Add Policy to configure rules for filtering node labels.

    You can also click Specify Node or Specify AZ to quickly select a node or AZ on the console for scheduling.

    Specifying a node or AZ is also implemented through labels. The console frees you from manually entering node labels. The kubernetes.io/hostname label is used when you specify a node, and the failure-domain.beta.kubernetes.io/zone label is used when you specify an AZ.

    Table 3 Parameters for configuring node affinity scheduling policies

    Parameter

    Description

    Example

    Weight

    This parameter is available only in a preferred scheduling policy. Weights range from 1 to 100 and are taken into account as an extra scoring factor during scheduling. The scheduler combines the weight with other priority functions of the node to determine the final score and then assigns pods to the node with the highest total score.

    None

    Label Key

    When configuring node affinity, enter the node label to be matched.

    Both default labels and custom labels are supported.

    gpu

    Operator

    The following operators are supported:

    • 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.
    • DoesNotExist: The affinity or anti-affinity object does not have a specified label key.
    • Gt: The label value of the scheduling node is greater than what is listed (character string comparison).
    • Lt: The label value of the scheduling node is less than what is listed (character string comparison).

    In

    Label Value

    When configuring node affinity, enter the value of the node label.

    true

  6. After the scheduling policy is added, click Create Workload.
  7. Verify that all pods run on the target node.

    1. In the navigation pane, choose Workloads.
    2. Click the workload name to enter its details page. On the Pods tab page, verify that all pods run on the target node, which is labeled with gpu=true.

Workload node affinity rules are implemented using node labels. When a node is created in a CCE cluster, it is automatically assigned certain labels. Here are some examples of commonly used node labels (for more labels, see Inherent Label of a Node):

  • topology.kubernetes.io/zone: the AZ where the node is located, which can be used for scheduling in a specified AZ.
  • kubernetes.io/hostname: hostname of a node, which can be used for specified node scheduling.
  • cce.cloud.com/cce-nodepool: node pool to which a node belongs, which can be used for scheduling in a specified node pool.

In this example, the rule that must be met indicates that the node to be scheduled must be labeled with key gpu and value true. The rule that needs to be met as much as possible indicates that pods are preferentially scheduled to nodes in AZ 1 based on topology.kubernetes.io/zone. The following is an example of configuring node affinity:

apiVersion: apps/v1
kind: Deployment
metadata:
  name:  gpu
  labels:
    app:  gpu
spec:
  selector:
    matchLabels:
      app: gpu
  replicas: 3
  template:
    metadata:
      labels:
        app:  gpu
    spec:
      containers:
      - image:  nginx:alpine
        name:  gpu
        resources:
          requests:
            cpu: 100m
            memory: 200Mi
          limits:
            cpu: 100m
            memory: 200Mi
      imagePullSecrets:
      - name: default-secret
      affinity:  # Configure a scheduling policy.
        nodeAffinity:  # Node affinity scheduling
          requiredDuringSchedulingIgnoredDuringExecution:  # Scheduling policy that must be met
            nodeSelectorTerms:     # Select a node that meets the requirements according to the node label.
              - matchExpressions:    # Node label matching rule
                - key: gpu   # The key of the node label is gpu.
                  operator: In  # The rule is met if a value exists in the value list.
                  values:   # The value of the node label is true.
                  - "true"
          preferredDuringSchedulingIgnoredDuringExecution:  # Scheduling policy that is met as much as possible
            - weight: 100  # Priority that can be configured when the best-effort policy is used. The value ranges from 1 to 100. A larger value indicates a higher priority.
              preference:  # Preferred node label matching rule when the best-effort policy is used
                matchExpressions:   # Node label matching rule
                  - key: topology.kubernetes.io/zone   # Nodes' AZs
                     operator: In  # The rule is met if a value exists in the value list.
                    values:   # The value of the node label is az1.
                    - "az1"
NOTE:

There is no anti-affinity policy for node affinity scheduling. For node anti-affinity, you can filter nodes by label using the NotIn and DoesNotExist operators.

In this example, a GPU node labeled with gpu=true has been created in the cluster. You can use this label to schedule pods to this GPU node.

  1. Log in to the CCE console.
  2. Click the cluster name to go to the cluster console, choose Workloads in the navigation pane, and click the Create Workload in the upper right corner.
  3. In Advanced Settings, choose Scheduling and select a policy for Node Affinity. In this example, Custom policies is selected. For details about how to create a workload, see Creating a Workload.

    Table 1 Scheduling policies

    Parameter

    Description

    Example

    Node Affinity

    • Not configured: No node affinity policy is configured.
    • Node Affinity: Specify the nodes where workload pods are to be deployed. If no nodes are specified, the pods will be randomly scheduled according to the default cluster scheduling policy.
    • Specified Node Pool Scheduling: Specify the node pools where workload pods are to be deployed. If no node pools are specified, the pods will be randomly scheduled according to the default cluster scheduling policy.
    • Custom policies: allow flexible scheduling of workload pods based on node labels. For details about the supported affinity policies, see Table 2. Select a proper policy type and add a policy. For details about the parameters, see Table 3.

    Custom policies

  4. Select a proper node affinity rule and click to add a scheduling policy. In this example, a scheduling policy is added in the Required area under Node Affinity. This policy specifies that the newly created workload can only be scheduled to a specific node.

    Table 2 Affinity rule

    Parameter

    Description

    Example

    Node Affinity

    • Required: indicates the hard constraint (requiredDuringSchedulingIgnoredDuringExecution) that must be met.

      If multiple rules that must be met are added, scheduling will be performed when only one rule is met.

    • Preferred: indicates the soft constraint (preferredDuringSchedulingIgnoredDuringExecution) that need to be met as much as possible.

      If multiple rules that are preferentially met are added, scheduling will be performed even if one or none of the rules is met.

    Required

  5. In the dialog box that is displayed on the right, click Add Policy to configure rules for filtering node labels.

    You can also click Specify Node or Specify AZ to quickly select a node or AZ on the console for scheduling.

    Specifying a node or AZ is also implemented through labels. The console frees you from manually entering node labels. The kubernetes.io/hostname label is used when you specify a node, and the failure-domain.beta.kubernetes.io/zone label is used when you specify an AZ.

    Table 3 Parameters for configuring node affinity scheduling policies

    Parameter

    Description

    Example

    Weight

    This parameter is available only in a preferred scheduling policy. Weights range from 1 to 100 and are taken into account as an extra scoring factor during scheduling. The scheduler combines the weight with other priority functions of the node to determine the final score and then assigns pods to the node with the highest total score.

    None

    Label Key

    When configuring node affinity, enter the node label to be matched.

    Both default labels and custom labels are supported.

    gpu

    Operator

    The following operators are supported:

    • 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.
    • DoesNotExist: The affinity or anti-affinity object does not have a specified label key.
    • Gt: The label value of the scheduling node is greater than what is listed (character string comparison).
    • Lt: The label value of the scheduling node is less than what is listed (character string comparison).

    In

    Label Value

    When configuring node affinity, enter the value of the node label.

    true

  6. After the scheduling policy is added, click Create Workload.
  7. Verify that all pods run on the target node.

    1. In the navigation pane, choose Workloads.
    2. Click the workload name to enter its details page. On the Pods tab page, verify that all pods run on the target node, which is labeled with gpu=true.

Workload node affinity rules are implemented using node labels. When a node is created in a CCE cluster, it is automatically assigned certain labels. Here are some examples of commonly used node labels (for more labels, see Inherent Label of a Node):

  • topology.kubernetes.io/zone: the AZ where the node is located, which can be used for scheduling in a specified AZ.
  • kubernetes.io/hostname: hostname of a node, which can be used for specified node scheduling.
  • cce.cloud.com/cce-nodepool: node pool to which a node belongs, which can be used for scheduling in a specified node pool.

In this example, the rule that must be met indicates that the node to be scheduled must be labeled with key gpu and value true. The rule that needs to be met as much as possible indicates that pods are preferentially scheduled to nodes in AZ 1 based on topology.kubernetes.io/zone. The following is an example of configuring node affinity:

apiVersion: apps/v1
kind: Deployment
metadata:
  name:  gpu
  labels:
    app:  gpu
spec:
  selector:
    matchLabels:
      app: gpu
  replicas: 3
  template:
    metadata:
      labels:
        app:  gpu
    spec:
      containers:
      - image:  nginx:alpine
        name:  gpu
        resources:
          requests:
            cpu: 100m
            memory: 200Mi
          limits:
            cpu: 100m
            memory: 200Mi
      imagePullSecrets:
      - name: default-secret
      affinity:  # Configure a scheduling policy.
        nodeAffinity:  # Node affinity scheduling
          requiredDuringSchedulingIgnoredDuringExecution:  # Scheduling policy that must be met
            nodeSelectorTerms:     # Select a node that meets the requirements according to the node label.
              - matchExpressions:    # Node label matching rule
                - key: gpu   # The key of the node label is gpu.
                  operator: In  # The rule is met if a value exists in the value list.
                  values:   # The value of the node label is true.
                  - "true"
          preferredDuringSchedulingIgnoredDuringExecution:  # Scheduling policy that is met as much as possible
            - weight: 100  # Priority that can be configured when the best-effort policy is used. The value ranges from 1 to 100. A larger value indicates a higher priority.
              preference:  # Preferred node label matching rule when the best-effort policy is used
                matchExpressions:   # Node label matching rule
                  - key: topology.kubernetes.io/zone   # Nodes' AZs
                     operator: In  # The rule is met if a value exists in the value list.
                    values:   # The value of the node label is az1.
                    - "az1"
NOTE:

There is no anti-affinity policy for node affinity scheduling. For node anti-affinity, you can filter nodes by label using the NotIn and DoesNotExist operators.

We use cookies to improve our site and your experience. By continuing to browse our site you accept our cookie policy. Find out more

Feedback

Feedback

Feedback

0/500

Selected Content

Submit selected content with the feedback