Updated on 2024-08-16 GMT+08:00

Configuring Specified Node Scheduling (nodeSelector)

To select a node for scheduling in Kubernetes, simply configure the nodeSelector field in the workload. This field allows you to configure the label of the desired node to be scheduled. Kubernetes schedules pods only to nodes with specified labels.

Step 1: Adding a Label to a Node

  1. Log in to the CCE console and click the cluster name to access the cluster console.
  2. In the navigation pane, choose Nodes. On the displayed page, click the Nodes tab, select the target node and click Labels and Taints in the upper left corner.
  3. In the displayed dialog box, click Add operation under Batch Operation, and then choose Add/Update or Delete.

    Enter the key and value of the label to be added or deleted, and click OK.

    For example, the key is deploy_qa and the value is true, indicating that the node is used to deploy the QA (test) environment.

    Figure 1 Adding a node label

  4. After the label is added, check the added label in node data.

Step 2: Creating a Workload Scheduled to a Specified Node

  1. Use kubectl to access the cluster. For details, see Connecting to a Cluster Using kubectl.
  2. Create a YAML file named nginx.yaml. The file name can be customized.

    Configure nodeSelector for the workload.
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: nginx
    spec:
      replicas: 3
      selector:
        matchLabels:
          app: nginx
      template:
        metadata:
          labels:
            app: nginx
        spec:
          nodeSelector: 
            deploy_qa: "true"
          containers:
          - image: nginx:latest
            imagePullPolicy: IfNotPresent
            name: nginx
          imagePullSecrets:
          - name: default-secret

    For example, if the key is deploy_qa and the value is true, the pod will be scheduled to the node with the deploy_qa=true label.

  3. Create a workload.

    kubectl apply -f nginx.yaml

Step 3: Verifying the Scheduling

  1. Log in to the CCE console and click the cluster name to access the cluster console.
  2. In the navigation pane, choose Workloads.
  3. Click the workload name to enter its details page. In the pod list, verify that all pods are running on the target node, which is labeled with deploy_qa=true.