kubectl
container
Kubernetes
commands
tutorial

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:

  1. Kubernetes Cluster Access: Ensure you have a running Kubernetes cluster and have the necessary permissions to manage it.
  2. kubeconfig File: Access to the necessary kubeconfig file that stores cluster connection details and authentication credentials.
  3. A Docker Image With kubectl: Use a base Docker image that comes with the kubectl command-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:

yaml
1apiVersion: v1
2kind: Pod
3metadata:
4  name: kubectl-pod
5spec:
6  containers:
7  - name: kubectl
8    image: google/cloud-sdk:latest
9    command: ['sh', '-c', 'while true; do sleep 3600; done']
10    volumeMounts:
11    - name: kubeconfig-volume
12      mountPath: /root/.kube
13  volumes:
14  - name: kubeconfig-volume
15    configMap:
16      name: kubeconfig

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.

dockerfile
1# Dockerfile
2FROM alpine:latest
3RUN apk add --no-cache curl && \
4    curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl" && \
5    chmod +x ./kubectl && \
6    mv ./kubectl /usr/local/bin/kubectl
7CMD ["sh"]

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:

yaml
1apiVersion: v1
2kind: Pod
3metadata:
4  name: example-pod
5spec:
6  containers:
7  - name: main-app
8    image: myapp:latest
9  - name: kubectl-sidecar
10    image: google/cloud-sdk:latest
11    command: ['kubectl', 'get', 'pods']
12    volumeMounts:
13    - name: kubeconfig-volume
14      mountPath: /root/.kube
15  volumes:
16  - name: kubeconfig-volume
17    configMap:
18      name: kubeconfig

Accessing kubeconfig

To access the Kubernetes API securely inside the container, you provide the kubeconfig file using ConfigMaps or Secrets:

yaml
1apiVersion: v1
2kind: ConfigMap
3metadata:
4  name: kubeconfig
5data:
6  config: |
7    apiVersion: v1
8    kind: Config
9    # Complete kubeconfig content here

Replace config with the appropriate configuration needed for cluster access.

Running the Pod

Deploy the pod using:

bash
kubectl apply -f pod.yaml

Then, you can access the running container to execute kubectl commands:

bash
kubectl exec -it kubectl-pod -- /bin/sh

Once inside the container, you can run any kubectl commands you need.

Summary Table

ApproachDescriptionUse Case
Pre-built Image With kubectlUses existing images like google/cloud-sdkQuick and easy setup for temporary jobs
Custom Docker ImageCustomizes the environment with specific toolsWhen specific tools or configurations are needed
Sidecar ContainersAdds an auxiliary container to existing podsDebugging 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.


Course illustration
Course illustration

All Rights Reserved.