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.
Prerequisites
A Container Service for Kubernetes (ACK) managed cluster or an ACK dedicated cluster is created.
A stateless application is created by using a Deployment, or a stateful application is created by using a StatefulSet.
Procedure
Add a label to nodes in a node pool.
ACK allows you to manage a group of cluster nodes by using a node pool. For example, you can manage the labels and taints of nodes in a node pool in a centralized manner. For more information about how to create a node pool, see Create a node pool.
Log on to the ACK console. In the left-side navigation pane, click Cluster.
On the Clusters page, find the cluster that you want to manage and click its name. In the left-side pane, choose .
In the upper-right corner of the Node Pools page, click Create Node Pool.
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.
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.
References
For more information about
nodeSelector
andnodeAffinity
, see Assigning Pods to Nodes.When you deploy or scale out an application, you can customize a ResourcePolicy to specify the priorities of different node types to which application pods are scheduled. When the system scales in application pods, pods are removed from nodes based on the priorities of the nodes in ascending order. For more information, see Configure priority-based resource scheduling.