Mastering Manual Pod Scheduling in Kubernetes

Mastering Manual Pod Scheduling in Kubernetes

In Kubernetes, pods are usually automatically scheduled onto nodes by the built-in scheduler. However, you may want more control over where certain pods are placed. You can manually schedule a pod to specify which node it should run on.

When To Manually Schedule Pods

Here are some common reasons you may want to manually schedule pods:

  • You want pods with high resource requirements to run on larger nodes.
  • You want to spread pods across nodes for high availability.
  • You want pods to run on nodes with special hardware like GPUs.
  • You want pod locality with services already running on a node.

How To Manually Schedule a Pod

1. Label the target node where you want to schedule the pod:

kubectl label nodes <node-name> special=true        

2. Create your pod specification YAML file. Add a nodeSelector section to only schedule on the labeled node:

apiVersion: v1
kind: Pod
metadata:
  name: nginx 
spec:
  containers:
  - name: nginx
    image: nginx
  nodeSelector:  
    special: "true"        

3. Create the pod in Kubernetes:

kubectl apply -f pod.yaml        

The pod should now be scheduled onto the node you labeled instead of letting the default scheduler place it.

You can continue to create more pods with similar nodeSelector sections to manually schedule groups of pods. Make sure the target nodes have enough resources for all the pods.

This gives you precise control to place specific pods on specific nodes in a Kubernetes cluster.

Manually Scheduling Pods using nodeName

The nodeName property in a pod specification allows you to directly schedule a pod onto a specific node without using labels and selectors.

The nodeName property

The nodeName property indicates the name of the target node where you want the pod scheduled. This overrides the default Kubernetes scheduler and binds the pod to that node.

Some key points about nodeName:

  • The named node must exist in your cluster or pod creation will fail.
  • Make sure the target node has enough resources for the pod.
  • Use this to precisely place a pod without relying on selectors.

Manually Scheduling a Pod by nodeName

1. Get your node names:

kubectl get nodes        

Note down the name of the node where you want to schedule your pod.

2. Create a pod YAML file, specifying nodeName:

apiVersion: v1
kind: Pod
metadata:  
  name: nginx
spec:
  containers:
  - name: nginx 
    image: nginx  
  nodeName: node-1        

3. Apply the YAML:

kubectl apply -f pod.yaml        

4. Verify the pod is running on the desired node:

kubectl get pods -o wide        

Using nodeName directly lets you schedule pods without having to label nodes and define match criteria. This gives fine-grained control over pod placement for specialized scheduling scenarios.

Conclusion

Manually scheduling pods in Kubernetes provides greater control over where workloads run compared to relying on the default scheduler. By directing pods to specific nodes using node selectors and nodeName properties, you can ensure that pods are running where they need adequate resources, match required hardware specifications, have locality to certain services, or meet other constraints you specify. The techniques covered in this tutorial—labeling and selecting nodes or directly specifying a nodeName—give you precise, predictable placement of pods across your cluster. When you need tailored scheduling requirements, remember these methods of manually scheduling pods based on the nodes where you know they should run. With the power to schedule pods manually, you gain flexibility to handle more complex and customized Kubernetes deployments.

 

To view or add a comment, sign in

More articles by Christopher Adamson

Insights from the community

Others also viewed

Explore topics