> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Manual Scheduling

> This article explains methods for manually scheduling pods on nodes in Kubernetes without using the built-in scheduler.

Welcome to this lesson on manually scheduling pods on a node. In this guide, we explore methods for assigning pods to nodes without relying on Kubernetes' built-in scheduler, which can help in scenarios where you need greater control over pod placement.

## Understanding Pod Scheduling

When you create a pod, its manifest typically contains a field called `nodeName`. By default, this field is left unset, allowing the Kubernetes scheduler to assign the pod automatically. Consider the following manifest:

```yaml theme={null}
apiVersion: v1
kind: Pod
metadata:
  name: nginx
  labels:
    name: nginx
spec:
  containers:
    - name: nginx
      image: nginx
      ports:
        - containerPort: 8080
  nodeName:
```

Under normal circumstances, the Kubernetes scheduler scans for pods without a specified `nodeName`, determines the appropriate node based on its scheduling algorithm, and creates a binding object to assign the pod to that node.

<Callout icon="lightbulb" color="#1CB2FE">
  Without an active scheduler, pods will remain in the **Pending** state. You can confirm this by executing:
</Callout>

```bash theme={null}
kubectl get pods
# Output:
# NAME     READY   STATUS    RESTARTS   AGE
# nginx    0/1     Pending   0          3s
```

## Approaches to Manual Pod Scheduling

There are two primary methods for manually scheduling a pod:

### 1. Specify `nodeName` During Pod Creation

The simplest approach is to set the `nodeName` in the pod's specification. When this field is explicitly defined, Kubernetes assigns the pod immediately to the designated node. See the updated manifest example below:

```yaml theme={null}
apiVersion: v1
kind: Pod
metadata:
  name: nginx
  labels:
    name: nginx
spec:
  containers:
    - name: nginx
      image: nginx
      ports:
        - containerPort: 8080
  nodeName: node02
```

### 2. Scheduling an Existing Pod Using a Binding Object

If the pod is already created and its `nodeName` cannot be modified, you need to simulate the scheduler's behavior by creating a Binding object. This involves the following two steps:

1. **Create the Binding Object**\
   Define a binding object that specifies the target node:

   ```yaml theme={null}
   apiVersion: v1
   kind: Binding
   metadata:
     name: nginx
   target:
     apiVersion: v1
     kind: Node
     name: node02
   ```

2. **Send the Binding Object via a POST Request**\
   Convert the YAML definition into JSON and use a `curl` command to send a POST request to the pod's binding API:

   ```bash theme={null}
   curl --header "Content-Type: application/json" \
        --request POST \
        --data '{"apiVersion":"v1", "kind": "Binding", "metadata": {"name": "nginx"}, "target": {"apiVersion": "v1", "kind": "Node", "name": "node02"}}' \
        http://$SERVER/api/v1/namespaces/default/pods/$PODNAME/binding/
   ```

<Callout icon="lightbulb" color="#1CB2FE">
  Ensure that you replace `$SERVER` and `$PODNAME` with your actual server address and pod name.
</Callout>

## Summary

In summary, you have two options for manually scheduling a pod in Kubernetes:

* **During Pod Creation:** Set the `nodeName` field in your pod's manifest to assign it directly to a node.
* **For Existing Pods:** Create a Binding object and use a POST request to assign the pod to your desired node.

This approach provides flexibility in environments where automated scheduling may not fit specific use cases. For more detailed information on Kubernetes pod management, visit the [Kubernetes Documentation](https://kubernetes.io/docs/).

Happy scheduling!

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/kubernetes-and-cloud-native-associate-kcna/module/4fab542c-3091-4f8e-ad7c-91d96d54b049/lesson/8268701e-0523-4d87-ae9d-35407df68073" />
</CardGroup>
