Tuesday, August 29, 2023

Schedule and unschedule Pods on Kubernetes Nodes

 Lets learn on how to make a node unschedulable

Note: I'm using an alias for kubectl as k. In your case you can use kubectl or set an alias

$ k get pods
NAME                     READY   STATUS      RESTARTS      AGE
nginx                    1/1     Running     0             3m2s
nginx-85b98978db-7brsd   1/1     Running     0             13s

$ k get nodes
NAME           STATUS   ROLES                  AGE     VERSION
controlplane   Ready    control-plane,master   4m6s    v1.23.4
node01         Ready    <none>                 3m44s   v1.23.4

$ k cordon node01
node/node01 cordoned

Now that the node01 is in Ready and SchedulingDisabled state, lets try to create a deployment and see what happens


$ k get nodes
NAME           STATUS                     ROLES                  AGE     VERSION
controlplane   Ready                      control-plane,master   4m45s   v1.23.4
node01         Ready,SchedulingDisabled   <none>                 4m23s   v1.23.4

Create a deployment by the name nginx1

$ k create deployment nginx1 --image=nginx
deployment.apps/nginx1 created

List the pods and you can see that nginx1* pod is in Pending state

$ k get pods
NAME                      READY   STATUS             RESTARTS      AGE
nginx                     1/1     Running            0             4m9s
nginx-85b98978db-7brsd    1/1     Running            0             80s
nginx1-5c9f6bbd8c-b57xs   0/1     Pending            0             3s

Lets make the node01 as schedulable

$ k uncordon node01
node/node01 uncordoned

You can see that node01 is in Ready state now

$ k get nodes
NAME           STATUS   ROLES                  AGE     VERSION
controlplane   Ready    control-plane,master   5m50s   v1.23.4
node01         Ready    <none>                 5m28s   v1.23.4

Lets list the Pods and you should see that nginx1 pod should be in Running state


$ k get pods
NAME                      READY   STATUS             RESTARTS      nginx                     1/1     Running            0             4m54s
nginx-85b98978db-7brsd    1/1     Running            0             2m5s
nginx1-5c9f6bbd8c-b57xs   1/1     Running            0             48s