switch between contexts to work on different clusters

switch between contexts to work on different clusters

Kubectl Context Alias

If you regularly switch between contexts to work on different clusters, you can create an alias to list and set the context.

Here is the alias to list the contexts.

alias klist='kubectl config get-contexts -o=name'

You can add the following line to your .bashrc or .zshrc file, depending on the shell you use:

Create the following alias to set the context with a custom namespace.

kset() {
    local context="$1"
    local namespace="$2"
    kubectl config use-context "$context" --namespace "$namespace"
}

Here is how the aliases look in your bashrc or zshrc file.

alias k=kubectl

alias klist='kubectl config get-contexts -o=name'

kset() {
    local context="$1"
    local namespace="$2"
    kubectl config use-context "$context" --namespace "$namespace"
}

kdel() {
    kubectl config delete-context "$1"
}

Here is how you can use these aliases

  1. List the contexts with klist command.

  2. Set the context and namespace using the kset command