Skip to main content

Assigning Pods to Nodes

Assigning pods to nodes

You can constrain a Pod so that it is restricted to run on particular Node(s), or to prefer to run on particular Nodes. The way it works is by attaching a label to a Node, and then specifying a NodeSelector, which is a field of PodSpec, in the Pod. The NodeSelector field consists of the label name and the value you want to match. The kubernetes scheduler uses NodeSelector to select the best Node for the Pod. If the NodeSelector cannot be satisfied, the Pod will not be scheduled.

1. Node labels

Node labels are key-value pairs that are attached to Nodes. Labels can be used to identify a Nodegroup, or to specify attributes of a Nodegroup. In Argonaut, you can go to the Environment page and click on the specific kubernetes cluster to view the configuration. You can add labels to the Nodegroup by clicking on the More button. Any number of labels can be added to the Nodegroup.

The labels can be attached to the Nodegroup at the time of creation, or can be added to the Nodegroup at any time. A different set of labels can be attached to each Nodegroup in the same cluster.

An example of a Nodegroup with labels is shown below: Add labels to Nodegroups

In this example, we have added two labels to the Nodegroup. The labels are appGroup: frontend and appGroup: backend.

2. App configuration

Once the Nodegroups have the labels attached, you can specify the NodeSelector in the Pod configuration. The Pod configuration can be specified in the art.yaml file.

nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: "<key-of-matched-label>"
operator: In
values:
- "<value-of-matched-label>"

The full documentation of the nodeAffinity field can be found here

For example, if we want the backend pods to run on the Nodegroup with the label appGroup: backend, we can specify the following in the art.yaml file:

nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: "appGroup"
operator: In
values:
- "backend"
info

💡 Note: The app will be deployed to the Nodegroup with the label appGroup: backend if it is available. If the Nodegroup is not available, the app will be deployed to any other Nodegroup in the cluster.