Configuring Specified Node Scheduling (nodeSelector)
In Kubernetes, to schedule a workload to a specified node, simply configure the nodeSelector field in the workload. By setting the target node label in this field, Kubernetes will schedule the workload only to the node with the matching label.
Prerequisites
A custom label has been added to the target node so that workload pods can be scheduled based on the node label. For details, see Adding or Deleting a Node Label.
Creating a Workload Scheduled to a Specified Node
- Use kubectl to access the cluster. For details, see Accessing a Cluster Using kubectl.
- Create a YAML file named nginx.yaml. The file name can be customized.
Configure nodeSelector for the workload. 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. Example:
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
- Create a workload.
kubectl apply -f nginx.yaml
- Verify that all pods run on the target node.
kubectl get pod -o wide
In the following command output, node 192.168.0.103 is labeled with deploy_qa=true:
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES nginx-66859f4f48-xgp2g 1/1 Running 0 6h57m 172.16.3.0 192.168.0.103 <none> <none> nginx-66859f4f48-t9gqj 1/1 Running 0 6h57m 172.16.3.1 192.168.0.103 <none> <none> nginx-66859f4f48-2grhq 1/1 Running 0 6h57m 172.16.3.2 192.168.0.103 <none> <none>
Helpful Links
- Configuring Node Affinity Scheduling (nodeAffinity)
- Configuring Workload Affinity or Anti-affinity Scheduling (podAffinity or podAntiAffinity)
- If a pod is not scheduled to the specified node, rectify the fault by referring to Why Cannot a Pod Be Scheduled to a Node?
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot