kubectx: Switch Kubernetes Contexts Fast
kubectx <name> sets the current context, kubectx with no argument lists them, and kubectx - switches back to the previous one.
Typing kubectl config use-context over and over is tedious. kubectx is a one-word switch with tab completion and a back button.
What it does
kubectx is a thin wrapper over the kubeconfig current-context. With an argument it switches; with none it lists all contexts and highlights the active one; - toggles to the previous context, like cd -.
Common usage
kubectx # list contexts, current one highlighted
kubectx staging # switch to the staging context
kubectx - # back to the previous context
kubectx prod=arn:aws:eks:... # alias a long context name to "prod"Options
| Form | What it does |
|---|---|
| kubectx | List all contexts |
| kubectx <name> | Switch to that context |
| kubectx - | Switch to the previous context |
| kubectx -c, --current | Print the current context name only |
| kubectx <new>=<old> | Rename / alias a context |
| kubectx -d <name> | Delete a context from kubeconfig |
In CI
In a job that touches several clusters, set KUBECONFIG to a merged file and use kubectx <name> before each step. Prefer kubectx -c over parsing kubectl config current-context when you just need the name.
Common errors in CI
error: no context exists with the name: "staging" means the context is absent from the active KUBECONFIG; run kubectx to see the real names. kubectx - printing error: no previous context found happens on the first switch in a fresh shell, where no prior context was recorded.
Using this in CI
A runner has no kubeconfig, no cached context, and no interactive auth. Every kubectl invocation in CI needs the context supplied explicitly, and most confusing CI failures here are the command running against the wrong cluster or no cluster at all.
# never rely on the ambient context on a runner
kubectl --context "$KUBE_CONTEXT" -n "$NAMESPACE" get pods
# confirm what you are actually connected to before mutating anything
kubectl config current-context
kubectl cluster-info
# fail fast instead of hanging on an unreachable API server
kubectl --request-timeout=30s get nodes