Installing kubectl in a Kubernetes Pod

tl;dr Use empty volume, initContainers, and subPath to copy and mount kubectl.

The Why

I needed access to the kubernetes API from within a pod so that the pod can self label itself.

For example, I am currently working with redis and redis-sentinel. When sentinel triggers a reconfigure script, I want the pod to re-label itself to role=masteror role=slave. I didn’t want to create a custom redis image that includes kubectl as it would be another component to maintain.

Also, what if I needed to work with other images requiring kubectl? Seemed like alot of maintenance going the custom image route.

The How

First, create an empty volume to hold the kubectl binary.

volumes:
- name: kubectl
emptyDir: {}

Next, using initContainers, copy out the kubectlbinary from a docker image into the volume. In this case, allanlei/kubectlis an image containing a static binary from kubernetes.

initContainers:
- name: install-kubectl
image: allanlei/kubectl
volumeMounts:
- name: kubectl
mountPath: /data
command: ["cp", "/usr/local/bin/kubectl", "/data/kubectl"]

Finally, mount the kubectl volume into the container using subPath. If you don’t use subPath, then the entire mount path will get overriden or gets mounted as a directory, which is not the goal. subPath allows us to specify certain paths in the volume to be mounted.

volumeMounts:
- name: kubectl
subPath: kubectl
mountPath: /usr/local/bin/kubectl

You’re ready to go! The container is now able to run kubectl which is automatically setup via Service Accounts.

Full Example:

The Adventures of Me

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

How LVM is used in real-world use cases, practical implementation of LVM on Hadoop Cluster.

Secure Kubernetic on-premise with Pomerium

Recovering Lost Files in Several Xcode Targets

System prep for app developers 1: Setting up your command-line interface

Oasis Infobyte Internship Experience

In Defense of XML

How to Assert Redirects to Signed URLs in Laravel Tests

How to create scheduled jobs in rails using sidekiq-cron

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Allan Lei

Allan Lei

The Adventures of Me

More from Medium

Kubernetes SSO using Keycloak

How to Diagnose OOMKilled Error in Kubernetes Application

Understanding Jobs in Kubernetes