kubectl is the front door to every cluster you operate, and the most common production incident with it is mundane: someone ran the right command in the wrong context. This article walks through the parts of kubectl you actually use every day — the kubeconfig file, contexts, namespaces, and merging multiple cluster configs — and the small habits that stop “wrong cluster” mistakes before they happen.
How to verify
The current state of your client is fully described by three commands:
kubectl config current-context
kubectl config get-contexts
kubectl config view --minify --flatten
current-context answers “which cluster does the next command hit?” get-contexts shows every cluster you have loaded, with the namespace bound to each one. view --minify --flatten prints the kubeconfig that will actually be used, with referenced files inlined — useful when several KUBECONFIG paths are merged and you want to see the resolved view.
To confirm the API server is reachable and your credentials work:
kubectl cluster-info
kubectl auth can-i '*' '*' --all-namespaces
What’s happening
A kubeconfig is a YAML file with three lists — clusters, users, and contexts — plus a current-context pointer. A cluster entry is “where” (server URL, CA bundle). A user entry is “who” (client cert, token, or exec plugin like aws eks get-token). A context binds a cluster to a user plus an optional default namespace. The active context determines every API call until you switch.
kubectl reads $KUBECONFIG (a colon-separated list), falling back to ~/.kube/config. When KUBECONFIG contains multiple files, kubectl merges them in order: the first file with a given key wins, the rest are ignored for that key. This is how you keep prod and staging configs separate but use both from the same shell.
The namespace lives on the context, not the command, by default. kubectl get pods returns pods in context.namespace (defaults to default). -n <ns> overrides per-command; kubectl config set-context --current --namespace=<ns> makes it sticky.
The procedure
-
List and switch contexts. The basic loop is
get-contextsthenuse-context:kubectl config get-contexts kubectl config use-context staging-euTo rename a context to something you can recognize in a hurry:
kubectl config rename-context arn:aws:eks:eu-west-1:123:cluster/prod prod-eu -
Set a default namespace on a context. Stop typing
-nfor the namespace you live in:kubectl config set-context --current --namespace=team-payments -
Merge multiple kubeconfigs. Keep one file per cluster under
~/.kube/configs.d/, then export them as a colon-separated list:export KUBECONFIG=$(ls ~/.kube/configs.d/*.yaml | tr '\n' ':') kubectl config get-contextsTo produce a single merged file for tools that don’t support
KUBECONFIGlists:KUBECONFIG=$(ls ~/.kube/configs.d/*.yaml | tr '\n' ':') \ kubectl config view --flatten > ~/.kube/config.merged -
Add a “production” guard. Rename prod contexts with a prefix that makes the prompt obvious, and use
kubectx/shell helpers that colour the prompt by context. A minimal zsh snippet:k8s_prompt() { local ctx ns ctx=$(kubectl config current-context 2>/dev/null) || return ns=$(kubectl config view --minify -o jsonpath='{..namespace}' 2>/dev/null) case "$ctx" in prod-*) print -P "%F{red}(%f${ctx}/${ns:-default}%F{red})%f " ;; *) print -P "%F{cyan}(%f${ctx}/${ns:-default}%F{cyan})%f " ;; esac } PROMPT='$(k8s_prompt)'$PROMPT -
Run a one-off command against a non-current context without switching. Useful for read-only checks across many clusters:
kubectl --context=prod-us get nodes kubectl --context=staging-eu -n team-payments get pods -
Trim stale entries when a cluster is decommissioned. Leaving dead context entries around is how someone runs a command “against staging” that succeeds against a different cluster with the same name.
kubectl config delete-context staging-eu-old kubectl config delete-cluster staging-eu-old.example.com kubectl config delete-user staging-eu-old-admin
Common pitfalls
~/.kube/configis mode 0600 for a reason — it contains long-lived credentials or exec configs with permanent IAM access; never check it into a repo or copy it without thinking.- A merged
KUBECONFIGwith conflicting context names silently picks the first one — rename contexts before merging so collisions are visible. kubectl proxyandkubectl port-forwarduse the current context; if you forwarded prod ports yesterday and switched contexts since, the forwarded session still talks to prod until you kill it.--all-namespacesand-Aignore the context’s default namespace; double-check before running destructive commands with them.- Some exec plugins (
aws eks get-token,gcloud config config-helper) shell out on every call — slow shells with prompt integration that runskubectlper prompt are usually this.
We operate kubectl access for clients the way we’d want our own infrastructure handled: per-engineer kubeconfigs, short-lived credentials via OIDC, prod contexts visually distinct from everything else, and a paper trail of who did what. See /en/services/managed-operations/ for the operating model.