Quickly change k8s context using kubectl

test img

Quick post to list kubectl commands to view and change contexts, as I had to search around quite a bit.

Get current contexts

kubectl config current-context 

To list all available contexts

kubectl config get-contexts
# display list of contexts 

Set a context

kubectl config use-context your-cluster-name
# set the default context to your-cluster-name

Bash function to switch contexts if you do it often

function change-context()
{
  if [ "$1" = "dev" ]; then
  kubectl config use-context your-dev-cluster-name
  elif [ "$1" = "stage" ]; then
  kubectl config use-context your-staging-cluster-name
  elif [ "$1" = "prod" ]; then
  kubectl config use-context your-prod-cluster-name
  fi
}

For more Kubectl context and configuration options, click here.