All Snippets

Kubectl Context Switching Cheatsheet

Quickly switch between Kubernetes clusters and namespaces without editing kubeconfig manually.

Managing multiple Kubernetes clusters means constantly switching contexts. These commands save you from editing ~/.kube/config by hand.

List and switch contextsbash
1
# List all available contexts
2
kubectl config get-contexts
3
 
4
# Switch to a different context
5
kubectl config use-context <context-name>
6
 
7
# Show current context
8
kubectl config current-context
Set default namespace for current contextbash
1
kubectl config set-context --current --namespace=<namespace>

Install kubectx and kubens for even faster switching: brew install kubectx on macOS or grab the binaries from github.com/ahmetb/kubectx.

With kubectx/kubensbash
1
# Switch context interactively
2
kubectx
3
 
4
# Switch namespace interactively
5
kubens
6
 
7
# Switch to previous context
8
kubectx -
9
 
10
# Switch to previous namespace
11
kubens -