The missing alias
command for kubectl
.
# "v" for version
kubectl alias v version
kubectl v --client
# "cd" to switch namespace
kubectl alias cd 'config set-context $(kubectl config current-context) --namespace'
kubectl cd my-namespace
# Get Pod's YAML spec and open it with less
kubectl alias --no-args gpyl 'get pod -o yaml $1 | less'
kubectl gpyl my-pod
# Make kubectl awesome!
kubectl alias avada-kedavra 'delete'
kubectl alias alohomora 'exec -it $1 -- bash'
-
Install Homebrew.
-
brew install predatorray/brew/kubectl-alias
-
Add this line to your rc file (e.g.:
~/.bashrc
,~/.zshrc
).
export PATH="$PATH:$(brew --prefix kubectl-alias)/alias"
-
Install Krew or upgrade to its latest version using
kubectl krew upgrade
. -
kubectl krew index add predatorray https://github.com/predatorray/my-krew-index.git
-
kubectl krew install predatorray/alias
-
Add this line to your rc file (e.g.:
~/.bashrc
,~/.zshrc
).
export PATH="$PATH:$(kubectl alias --prefix)"
-
Download the latest release.
-
Unpack the
kubectl-alias-*.tar.gz
file and copy all the files to a directory,/usr/local/kubectl-alias
for instance. -
Add both the
bin/
and thealias/
directories to thePATH
. For example, add this line to your rc file:
export PATH="$PATH:/usr/local/kubectl-alias/bin:/usr/local/kubectl-alias/alias"
- If it is not running on GNU-Linux, install the GNU
getopt
. After that, add this lineexport GNU_GETOPT_PREFIX="path/to/gnu-getopt"
to your rc file.
kubectl alias ALIAS COMMAND
kubectl alias -N ALIAS COMMAND
kubectl alias --no-args ALIAS COMMAND
The -N, --no-args
flag is used when the arguments should not be passed to the end of the commands. It is useful when the offset parameter is explicitly declared in the alias command. For example,
kubectl alias --no-args gpyl 'get pod -o yaml $1 | less'
If the flag is absent, by executing kubectl gpyl my-pod
, the my-pod
argument will also be passed to the less
commnd.
# WRONG
kubectl alias gpyl 'get pod -o yaml $1 | less'
kubectl get pod -o yaml my-pod | less my-pod
kubectl alias -d ALIAS
kubectl alias --delete ALIAS
kubectl alias -l
kubectl alias --list
This means that the alias/
directory is not correctly added to the PATH
environment variable.
Add this line to your rc file.
export PATH="$PATH:$(brew --prefix kubectl-alias)/alias"
# Or, if installed manually.
export PATH="$PATH:${PREFIX}/alias"
After that, run kubectl plugin list
to check if the aliases have been loaded successfully. If the alias is named v
, the output of the plugin list will be:
The following compatible plugins are available:
/usr/local/bin/kubectl-alias
/usr/local/opt/kubectl-alias/alias/kubectl-v
Please feel free to open an issue if you find any bug or have any suggestion.