All Products
Search
Document Center

Container Service for Kubernetes:Schedule application pods to a specific node pool

Last Updated:Mar 05, 2025

You can add a label to nodes in a node pool and schedule application pods to run on nodes that have the specified label in the node pool.

Procedure

  1. Add a label to nodes in a node pool.

    1. Log on to the ACK console. In the left-side navigation pane, click Clusters.

    2. On the Clusters page, find the cluster that you want to manage and click its name. In the left-side navigation pane, choose Nodes > Node Pools.

    3. In the upper-right corner of the Node Pools page, click Create Node Pool.

    4. In the Create Node Pool dialog box, click Show Advanced Options and click the节点池 icon to the right of the Node Label parameter to add a label to nodes in the node pool.

      In this example, the pod: nginx label is added.

    You can also find the node pool that you want to manage on the Node Pools page and click Edit in the Actions column to update or add labels to nodes in the node pool.Podnginx

  2. Configure a scheduling policy for application pods.

    After the preceding step is complete, the pod: nginx label is added to nodes in the node pool. To schedule application pods to run on nodes that have the specified label in the node pool, you can specify the nodeSelector or nodeAffinity field in pod configurations. Details:

    • Specify the nodeSelector field.

      nodeSelector is a field in the spec section of pod configurations. Specify the pod: nginx label in the nodeSelector field. Sample code:

      apiVersion: apps/v1 
      kind: Deployment
      metadata:
        name: nginx-deployment-basic
        labels:
          app: nginx
      spec:
        replicas: 2
        selector:
          matchLabels:
            app: nginx
        template:
          metadata:
            labels:
              app: nginx
          spec:
            nodeSelector:
              pod: nginx      # Add the label to nodes in the node pool to ensure that application pods run only on nodes that have the specified label in the node pool. 
            containers:
            - name: nginx
              image: nginx:1.7.9
              ports:
              - containerPort: 80
    • Specify the nodeAffinity field.

      nodeAffinity supports the following scheduling policies:

      • - requiredDuringSchedulingIgnoredDuringExecution

        If this policy is used, pods can be scheduled only to nodes that meet the specified conditions. If no node meets the specified conditions, the system retries until a node that meets the conditions is found. IgnoreDuringExecution indicates that if the label of the node on which a pod is deployed changes and the node no longer meets the specified conditions, the pod continues to run on the node.

      • - preferredDuringSchedulingIgnoredDuringExecution

        If this policy is used, pods are preferably scheduled to run on nodes that meet the specified conditions. If no node meets the conditions, the system ignores the conditions and schedules the pods based on the default logic.

      In the following example, the requiredDuringSchedulingIgnoredDuringExecution policy is used to ensure that application pods are scheduled to run only on nodes that have the specified label in the node pool.

      apiVersion: apps/v1
      kind: Deployment
      metadata:
        name: nginx-with-affinity
        labels:
          app: nginx-with-affinity
      spec:
        replicas: 2
        selector:
          matchLabels:
            app: nginx-with-affinity
        template:
          metadata:
            labels:
              app: nginx-with-affinity
          spec:
            affinity:
              nodeAffinity:
                requiredDuringSchedulingIgnoredDuringExecution:
                  nodeSelectorTerms:
                  - matchExpressions:
                    - key: pod         # The key of the label that is added to nodes. In this example, pod is used. 
                      operator: In      # Specifies that application pods run on nodes that have the pod: nginx label. 
                      values:
                      - nginx        # The value of the label that is added to nodes. In this example, nginx is used. 
            containers:
            - name: nginx-with-affinity
              image: nginx:1.7.9
              ports:
              - containerPort: 80

Verify the configuration

Click the name of the application. On the application details page, click the Pods tab. The application pods are scheduled to the xx.xx.33.88 and xx.xx.33.92 nodes that are added with the pod: nginx label in the node pool.2.jpg

References

  • For more information about nodeSelector and nodeAffinity, see Assigning Pods to Nodes.

  • The ResourcePolicy allows specifying the scheduling order of applications across heterogeneous node resources, enabling sequential scale-out and reverse-order scale-in. For more information, see Configure priority-based resource scheduling.

  • Free nodes that are not managed by node pools may exist in legacy clusters created before the node pool feature was released. You can add free nodes to a node pool.