Skip to content

Instantly share code, notes, and snippets.

@kelseyhightower
Created May 4, 2016 21:06
  • Star 8 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save kelseyhightower/2349c9c645d32a3fcbe385082de74668 to your computer and use it in GitHub Desktop.

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
Copy link

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
Copy link

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
Copy link

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?

@Eeebru
Copy link

Eeebru commented Oct 14, 2021

That's true @weibeld.

@adrianludwin
Copy link

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