Scheduling in Kubernetes

How scheduling works?

Every pod specification file has a field called nodeName that, by default, is not set. You do not typically specify the field when you create a pod, Kubernetes adds it automatically. The scheduler goes through all the pods and looks for these pods which pods do not have properly set. These are the candidates for scheduling. It then identifies the right node for the pod, by running the scheduling algorithm. Once identified it schedules the pods on the node by setting the node name property to the name of node by creating a binding object.

If there is no scheduler to monitor and schedule nodes what happens?

The pods continue to be in a pending status. So, you can manually assign pods to node yourself.

Without a scheduler,

  1. the easiest way to schedule a pod is to simply set the node name field to the name of the node in your pod specification file while creating the pod. The pod then gets assigned to the specified node. You can only specify the node name at creation time.
apiVersion: v1
kind: Pod
metadata:
  name: nginx-pod
  labels:
    app: nginx
spec:
  containers:
    - name: nginx-container
      image: nginx
      ports:
        - containerPort: 80
          protocol: TCP
  nodeName: minikube

What if the pod is already created and you want to assign the pod to a node?

Kubernetes will not allow you to modify the node name property of a pod.

{
  "apiVersion": "v1",
  "kind": "Binding",
  "metadata": {
    "name": "apple-binding"
  },
  "target": {
    "apiVersion": "v1",
    "kind": "Node",
    "name": "kubenode01"
  }
}

So, another way to assign a node to an existing pod is to create a binding object and send POST request to the pod binding APIs. That is mimicking what the actual scheduler does.

Send a POST request to the pods binding API with the data set to the binding object in a JSON format. (You should convert the YAML file into its equivalent JSON format.)

$ curl --header "Content-Type:application json" --request POST --data { "apiVersion": "v1", "kind": "Binding", "metadata": { "name": "apple-binding" }, "target": "{ "apiVersion": "v1", "kind": "Node", "name": "kubenode01" } }"
http://`{$server}`/api/v1/namespaces/default/pods/`{$podname}`/binding/

Leave a Reply

Your email address will not be published.

ANOTE.DEV