How to use Kubectl Plugin to extend kubectl

This article will showcase how to extend kubectl using kubectl plugins. Sometimes the out-of-the-box kubectl is not enough to provide the desired interaction with the API server. Many times users end up writing scripts, getting complex desired results. Therefore, Kubectl plugins will introduce a new kubectl subcommand to add custom features.
Several open-source plugins are available for installation via Krew(plugin manager). This article consists of two parts.

  • Part-A How to Use the pre-existing plugins?
  • Part-B How to write your custom plugin?
 
kubectl

How to Use the pre-existing Kubectl plugins?

Step-1: Install Krew

To use the pre-existing plugins provided by Krew(plugin manager), we first need to install the krew. Paste the entire code block.

(
set -x; cd "$(mktemp -d)" &&
OS="$(uname | tr '[:upper:]' '[:lower:]')" &&
ARCH="$(uname -m | sed -e 's/x86_64/amd64/' -e 's/\(arm\)\(64\)\?.*/\1\2/' -e 's/aarch64$/arm64/')" &&
KREW="krew-${OS}_${ARCH}" &&
curl -fsSLO "https://github.com/kubernetes-sigs/krew/releases/latest/download/${KREW}.tar.gz" &&
tar zxvf "${KREW}.tar.gz" &&
./"${KREW}" install krew
)
echo 'export PATH="${KREW_ROOT:-$HOME/.krew}/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc

Ste-2: List the available Kubectl plugins using Krew

To list the plugins via command line run the following command, else you may check it online here

ps@kube-master:~$ kubectl krew search
NAME DESCRIPTION INSTALLED
access-matrix Show an RBAC access matrix for server resources no
accurate Manage Accurate, a multi-tenancy controller no
advise-policy Suggests PodSecurityPolicies and OPA Policies f... no
advise-psp Suggests PodSecurityPolicies for cluster. no
allctx Run commands on contexts in your kubeconfig no
apparmor-manager Manage AppArmor profiles for cluster. no
............
......................
................skipped many for clean output.............
.................................................................
assert Assert Kubernetes resources no
example Prints out example manifest YAMLs no
exec-as Like kubectl exec, but offers a `user` flag to ... no
images Show container images used in the cluster. no #<---This one is installed as an example here.
ps@kube-master:~$

Step-3: Installing a Kubectl plugin using Krew

ps@kube-master:~$ kubectl krew install images
Updated the local copy of plugin index.
Installing plugin: images
Installed plugin: images
\
| Use this plugin:
| kubectl images
| Documentation:
| https://github.com/chenjiandongx/kubectl-images
/
WARNING: You installed plugin "images" from the krew-index plugin repository.
These plugins are not audited for security by the Krew maintainers.
Run them at your own risk.
ps@kube-master:~$

Step-4:  Example Using the installed Kubectl plugin

ps@kube-master:~$ kubectl images -n default
[Summary]: 1 namespaces, 3 pods, 3 containers and 3 different images
+------------------+---------------+--------------------------+
| PodName | ContainerName | ContainerImage |
+------------------+---------------+--------------------------+
| busybox | busybox | busybox |
+------------------+---------------+--------------------------+
| kube-bench-s8vqt | kube-bench | aquasec/kube-bench:0.6.3 |
+------------------+---------------+--------------------------+
| nginx | nginx | nginx |
+------------------+---------------+--------------------------+
ps@kube-master:~$

How to write your custom plugin?

Writing your own custom plugin is easy and gives huge power to the administrator and developers to have a custom outcome that is consistent with everyone.  You can choose the language of your choice like python, go, perl, bash, etc. Here is an example 

Example Creation of Custom plugin:

Created a bash script to print the number of pods running. I know it’s a lame example.  Note that the name of the executable(script) must start with “kubectl-“.  The name of the file is “kubectl-podcount”. When calling the plugin later, we will only be using “podcount” by ignoring the prefix. You may write complex logic here as per your requirements. 

vi ./kubectl-podcount
#!/bin/bash
number_of_pod=$(kubectl get pod -A --no-headers |grep Running|wc -l)
echo "Total number of pods in running state: $number_of_pod"

Making it an executable:

sudo chmod +x ./kubectl-podcount

Move the Executable to any directory of $PATH

Here moved the file to /usr/bin, you may move it to ~/.krew/bin for enabling plugin limited to your user.

sudo mv ./kubectl-podcount /usr/bin/

Execute the custom plugin

ps@kube-master:~$ kubectl podcount
Total number of pods in running state: 17
ps@kube-master:~$
0 0 votes
Please, Rate this post
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
Scroll to Top