Machine Learning & Big Data Blog

How To Write Kubectl Subcommands

Merron Dan
by Merron Dan
3 minute read

Since the release of Kubernetes v1.12 it has been possible to extend kubectl with subcommands (plugins) to make automating repetitive Kubernetes tasks seem more kubectl native and easier to use for developers.

Before you can start to make use of kubectl subcommands you’re going to need to upgrade your version of kubectl cli to at least v1.12. Follow the official Kubernetes documentation for your platform here. (link:

What are the benefits of kubectl subcommands?

Using subcommands doesn’t really add any benefit that you can’t introduce by writing your own scripts and distributing them as you would normally. However they do allow your scripts to look like they are built right into kubectl and feel more natural for users if you are distributing these scripts to developer machines.

Kubectl plugins suit 2 major languages each with their own benefits:

Go: The go to choice for cloud native/kubernetes which can be distributed as a single binary.

Bash: Cross platform and can be relied upon due to users invoking kubectl which is a bash command.

As an example this guide is going to focus on building a kubectl subcommand executed with `kubectl cmd` that is going to run an argument based command on all pods in a given namespace that match a `| grep` filter. This example will use `env` as the command to run on pods to simply print out all environment variables however can be invoked with any command you desire.

Save the following script anywhere in your `$PATH`.

Make the script executable with `chmod +x /usr/local/bin/kubectl-cmd`

To confirm that kubectl is aware of your new plugin type

`kubectl plugin list`
Now that the plugin is listed as available inside kubectl there is nothing left to do. You can now invoke your new kubectl subcommand with `kubectl cmd <arg1> <arg2> <arg3>`.

Invoking kubectl plugins

As seen in the example above, arguments that your plugin is invoked with will be passed to your executable in the same way they would if you were to run this in any other way.

You can read more about the kubectl plugin mechanisms on the official Kubernetes GitHub

Aside from writing your own kubectl plugins you can use krew (link: to find and install community plugins written by other developers to extend the built in functionality of kubectl. You can also publish your own plugins to krew for others to use.

The code used in this guide can be found on GitHub so you can quickly get started. I’ve also included a simple Ansible Playbook to simplify installing any custom plugins you have written which can be found

E-Book: Avoid Sticker Shock—How to Determine the True Cost of Clouds

Cost reduction is one of the main reasons for moving to the cloud. Cost reduction is not a guarantee – but is achievable with the right plan. Get insight into the right steps to take for migrating workloads to the cloud and reducing costs as a result.
Read the E-Book ›

These postings are my own and do not necessarily represent BMC's position, strategies, or opinion.

See an error or have a suggestion? Please let us know by emailing

About the author

Merron Dan

Merron Dan

Dan Merron is a seasoned DevOps Consulting Professional with experience in private, public and financial sectors working with popular container, automation and scripting tools. You can visit his website or find him on Github or LinkedIn.