Kubectl CLI Plugins Installation and Usage
Table of Contents
- Introduction
- Kubectl plugin install steps
- krew kubectl plugin manager
- Install krew
- Install kubectl plugins
- Plugin usage
- Plugin Customisation
- Further Reference
Introduction
Kubectl is the command line interface to interact with the Kubernetes cluster. As kubectl users, we often find it difficult to switch between contexts and namespaces.
To resolve this difficulty there are plugins available for kubectl CLI, that makes our life easier.
In this blog, we'll see how to install the plugins for kubectl in Ubuntu OS and what is the usage of those plugins.
Kubectl plugin install steps
krew kubectl plugin manager
- We can install
Krewas a kubectl plugin manager to get the kubectl plugin installed in our system. It is similar topipforPythonand Ubuntuaptpackage manager.
What does Krew do?
Krew is a tool that makes it easy to use kubectl plugins. Krew helps you discover plugins, install and manage them on your machine. It is similar to tools like apt, dnf or brew. Today, over 200 kubectl plugins are available on Krew.
Install krew
- Make sure you've git is installed in your system.
- Execute the command in your terminal window
(
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
)
- Export the
krewbinpath to ENVPATHvariable
export PATH="${KREW_ROOT:-$HOME/.krew}/bin:$PATH"
- We have now installed
krewas kubectl plugin manager, so let's see how to install plugins using.
Install kubectl plugins
- Update the
krewlocal packages:
kubectl krew update
- Install
ctxplugin to managecontexts:
kubectl krew install ctx
ctxcan be used for kubectl contexts setting and switching.Install
nsplugin to managenamespaces:
kubectl krew install ns
- Keep your plugins up-to-date by running the command:
kubectl krew upgrade
Plugin usage
- Check the current context by running the command,
kubectl ctx -c
- List all the contexts using the command,
kubectl ctx
- Check the current
namespaceby running the command,
kubectl ns -c
- List all the
namespaceusing the command,
kubectl ns
- Switch to previous
nsorctxby running the command,
kubectl ns -
kubectl ctx -
Plugin Customisation
- We can customize the the colors indicating the current
namespaceorcontext, by setting color values in the ENV variablesKUBECTX_CURRENT_FGCOLORandKUBECTX_CURRENT_BGCOLOR - For example:
export KUBECTX_CURRENT_FGCOLOR=$(tput setaf 6) # blue text
export KUBECTX_CURRENT_BGCOLOR=$(tput setab 7) # white background
- Refer color codes here
Note
- Add the env variables in
.bashrcto access the customised values in terminal sessions permanently,
export PATH="${KREW_ROOT:-$HOME/.krew}/bin:$PATH"
export KUBECTX_CURRENT_FGCOLOR=$(tput setaf 6)
export KUBECTX_CURRENT_BGCOLOR=$(tput setab 0)
Further Reference
Community and Social Footprints
Reach out to me, if you've any questions and mentioring sessions:
Top comments (0)