K8s & Docker – Gain root access on container using node-shell

Docker & Kubernetes

So you’ve followed best practices and configured your container to use a user other than root for its requirements.  Nice job.   The security experts are convinced this will keep your container, pod, and cluster all more secure.

However, sometime in the near future, a completely unforeseen and impossible issue pops up and you need root to troubleshoot.  This is where node-shellcomes into play.

Node-shell is a kubectlplugin that will give you shell access on the node.  Once on the node, you can use docker to gain access to the root user on your container.  I’m sure there are 1000 uses for this, but this was mine.

  1. Install node-shellhttps://github.com/kvaps/kubectl-node-shell
  2. List pod and node information
    kubectl get pods -n <namespace> -o wide
  3. Jump onto the desired node
    kubectl node-shell <node_name>
  4. List containers
    docker ps
  5. Exec into the container
    docker exec -it -u 0 <container_name> /bin/bash
  6. Verify root status

One quick note about exiting;

  1. Type exit once to exit the container
  2. Type exit a second time to exit the node.

Leave a Reply

Your email address will not be published. Required fields are marked *