How to run kubectl commands inside a container?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
In Kubernetes, kubectl is the command-line tool used to interact with Kubernetes clusters. Sometimes, there is a requirement to run kubectl commands from within a pod, whether for automation, debugging, or other operational tasks. This article explains how to achieve this, along with technical explanations and examples.
Prerequisites
Before running kubectl inside a container, the following prerequisites must be met:
- Kubernetes Cluster Access: Ensure you have a running Kubernetes cluster and have the necessary permissions to manage it.
- kubeconfig File: Access to the necessary
kubeconfigfile that stores cluster connection details and authentication credentials. - A Docker Image With kubectl: Use a base Docker image that comes with the
kubectlcommand-line tool pre-installed.
Different Approaches
There are various methods to run kubectl inside a pod. Below are some of the most common approaches:
1. Using a Pre-Built Image With kubectl
One straightforward way is to use a Docker image that already has kubectl installed. For instance, the official Google Cloud SDK image includes kubectl. Here's how you can start a simple pod with it:
This manifest creates a pod with a container running kubectl. It mounts a ConfigMap containing kubeconfig data into the container.
2. Custom Docker Image
If you want more control, you can build a custom Docker image that includes kubectl and any necessary tools.
Build and push this image to a container registry, then use it in your Kubernetes manifest.
3. Running kubectl Commands in Sidecars
Another method is using sidecar containers in an existing pod to execute kubectl commands:
Accessing kubeconfig
To access the Kubernetes API securely inside the container, you provide the kubeconfig file using ConfigMaps or Secrets:
Replace config with the appropriate configuration needed for cluster access.
Running the Pod
Deploy the pod using:
Then, you can access the running container to execute kubectl commands:
Once inside the container, you can run any kubectl commands you need.
Summary Table
| Approach | Description | Use Case |
| Pre-built Image With kubectl | Uses existing images like google/cloud-sdk | Quick and easy setup for temporary jobs |
| Custom Docker Image | Customizes the environment with specific tools | When specific tools or configurations are needed |
| Sidecar Containers | Adds an auxiliary container to existing pods | Debugging and multi-container applications |
Additional Considerations
- Security: Always keep in mind the security implications when accessing the Kubernetes API from within a container. Limit permissions to only what is necessary.
- Resource Limits: Ensure your pods have correct resource requests and limits to avoid unnecessary load on the cluster.
- Cleaning Up: Remember to delete any temporary resources using
kubectl delete <resource>after use to prevent dangling resources.
By following these methods, you can effectively run kubectl commands inside a container in Kubernetes, facilitating automation, debugging, or any other use case that might require internal API interaction.

