Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?

Creating and Scheduling a Pod Manually

This tutorial demonstrates how to create a Pod and manually schedule it to a node using cURL. This guide assumes you don't have a scheduler running in your Kubernetes cluster.

Create a Pod

The following pod will run version 1.10.0 of nginx with a 100m CPU limit.

Create the nginx pod manifest and name it pod.json:

{
   "kind":"Pod",
   "apiVersion":"v1",
   "metadata":{
      "name":"nginx",
      "labels":{
         "run":"nginx"
      }
   },
   "spec":{
      "containers":[
         {
            "name":"nginx",
            "image":"nginx:1.10.0",
            "resources":{
               "requests":{
                  "cpu":"100m"
               }
            }
         }
      ],
      "imagePullPolicy":"Always"
   }
}

Use cURL to create the nginx Pod:

curl -X POST http://127.0.0.1:8080/api/v1/namespaces/default/pods \
  -H "Content-Type: application/json" \
  -d @pod.json

Scheduling a Pod

Examine Cluster Nodes

List all nodes using cURL

curl http://127.0.0.1:8080/api/v1/nodes

Chose one of the nodes from the response. You'll need the node name.

Bind a Pod to a Node

Create binding.json

{
   "apiVersion":"v1",
   "kind":"Binding",
   "metadata": {
      "name":"nginx"
   },
   "target": {
      "apiVersion":"v1",
      "kind":"Node",
      "name":"node1"
   }
}

Post the binding to the pod's binding subresource:

curl -X POST http://127.0.0.1:8080/api/v1/namespaces/default/pods/nginx/binding \
  -H "Content-Type: application/json" \
  -d @binding.json
@adrianludwin

This comment has been minimized.

Copy link

@adrianludwin adrianludwin commented Feb 6, 2019

Sorry for the low-level question, but why don't we simply set pod.spec.nodeName to the desired node? What does the Binding object add?

According to pkg/registry/core/pod/storage/storage.go, the apiserver appears to simply take the value from the Binding object and copy it to pod.spec.nodeName (after performing some checks) - is the Binding object simply there to ensure the checks get performed?

@weibeld

This comment has been minimized.

Copy link

@weibeld weibeld commented Aug 29, 2019

@adrianludwin According to this article, the scheduler is not allowed to change pod.spec.nodeName directly. I guess the Binding object is used to perform the required checks in an organised way, like if the pod is already assigned to a node.

@KevinWang15

This comment has been minimized.

Copy link

@KevinWang15 KevinWang15 commented Sep 12, 2019

Maybe it's related to RBAC too?
It's designed this way so that scheduler only needs permissions of the binding subresource, making it safer?

@Eebru-gzy

This comment has been minimized.

Copy link

@Eebru-gzy Eebru-gzy commented Oct 14, 2021

That's true @weibeld.

@adrianludwin

This comment has been minimized.

Copy link

@adrianludwin adrianludwin commented Oct 14, 2021

Thanks all (apologies for not thanking earlier)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment