How to quarantine Kubernetes pods?
To investigate and debug the containers within a pod you need to remove the pod from the load balancing pool, so none of the traffic gets sent to it. This tip shows you how to do that.
$ kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE
hello-world-5fd44c56d7-55hmc 1/1 Running 0 5m27s app=hello-world,pod-template-hash=5fd44c56d7
hello-world-5fd44c56d7-66rp6 1/1 Running 0 10m app=hello-world,pod-template-hash=5fd44c56d7
hello-world-5fd44c56d7-d8g4j 1/1 Running 0 2d23h app=hello-world,pod-template-hash=5fd44c56d7
hello-world-5fd44c56d7-fv7wg 1/1 Running 0 10m app=hello-world,pod-template-hash=5fd44c56d7
hello-world-5fd44c56d7-t7644 1/1 Running 0 10m app=hello-world,pod-template-hash=5fd44c56d7
app=hello-world
. To quarantine one of the pods we need to update that label.hello-world-5fd44c56d7-55hmc
and update the value of the app label to debug
:kubectl label pod hello-world-5fd44c56d7-55hmc app=debug --overwrite
label
command with the resource and the name of the resource. Next, I am specifying the label I want to add or update (app=debug
) and finally, I need to specify --overwrite
because label app
already exists.$ kubectl get pod --show-labels
NAME READY STATUS RESTARTS AGE
hello-world-5fd44c56d7-2ssww 1/1 Running 0 52s app=hello-world,pod-template-hash=5fd44c56d7
hello-world-5fd44c56d7-55hmc 1/1 Running 0 9m13s app=debug,pod-template-hash=5fd44c56d7
hello-world-5fd44c56d7-66rp6 1/1 Running 0 14m app=hello-world,pod-template-hash=5fd44c56d7
hello-world-5fd44c56d7-d8g4j 1/1 Running 0 2d23h app=hello-world,pod-template-hash=5fd44c56d7
hello-world-5fd44c56d7-fv7wg 1/1 Running 0 14m app=hello-world,pod-template-hash=5fd44c56d7
hello-world-5fd44c56d7-t7644 1/1 Running 0 14m app=hello-world,pod-template-hash=5fd44c56d7
hello-world-5fd44c56d7-55hmc
stays around. Now you can inspect and debug the pod without impacting anything.