Extending scope of kubectl with plugins
Before diving into the topic, lets have look at few terminologies.
Kubernetes and kubectl
Kubernetes, also known as k8s, is opensource container-orchestration tool which is generally used manage the containers, application deployments, services. If you run your workload on containers then kubernetes is best option to choose. Best thing about kubernetes is its opensource. You can deploy it on your on-premise servers or cloud managed server, you don't have to pay for anything for software. Once you cluster is up and running, kubectl, is tool with whom you spend your most of the time. Its a command line tool which used to manage kubernetes cluster in terms of deploying application, services, inspecting containers, checking logs etc. kubectl comes with lots of sub-command. Each sub-command has its own use. In this blog, we are going to create our own sub-command, which is known as plugin.
kubectl Plugins
Kubectl plugins are standalone scripts. Scripts can be written in any programming language. These executable scripts are stored in your PATH
and whose names start with kubectl-
. PATH is where all you commands are present. You can see PATH by following command :
# echo $PATH
Just place your executable script anywhere in your PATH and you are good to go.
How to write Plugin
Create file starting with kubectl-
. For example, kubectl-greetings
which will create kubectl greetings
command. To install plugin, you must save this file anywhere in you PATH. Following is example of plugin that will greet user
Save this in kubectl-greetings and run following command to make it executable. After that move this file anywhere in you PATH
with mv
command.
# chmod +x kubectl-greetings
Now you are all set to run kubectl greetings
command which will output Hello
with username. Output of command from above script shown below
You can check all installed plugins by running following command:
# kubectl plugin list
Limitations
It is not possible to create plugins which will overwrite existing kubectl command. For example, if you create kubectl-create
script and try to run it, kubectl create
command will always take precedence over it and your script will be ignored.