I use kubectl commands to switch between different contexts and namespaces while dealing with them or bringing them into usage. I constantly try to copy and paste commands because they used to be somewhat long. Repeatedly typing or copying the same instruction diminishes productivity and takes our attention away from the task at hand. I had no idea that I could give the commands an alias. I hope this post will be helpful to newcomers.
A quick explanation of contexts and namespaces is given for those who are not familiar with them. A context is kubectl’s connection to a particular cluster (username/apiserver host). That allows you to manage several clusters. A namespace is a logical division used to control resources and constraints inside a particular cluster.
By making aliases for frequently used commands, we can save some time. Aliases are used to represent a command (or set of commands) executed with or without custom options. They resemble custom shortcuts.
Syntax:
$ alias <name> =”your custom command here”
Example:
alias dev-uat-switch=”kubectl config use-context arn:aws:xxx:xx-zone-2:xxxx:cluster/dev-uat”
Please note that there is no space before and after “=”. Otherwise, you will end up getting
zsh: bad assignment error.