Skip to content
Latchkey

kubectl config: Usage, Options & Common CI Errors

Manage which cluster, user, and namespace kubectl talks to.

kubectl config reads and modifies your kubeconfig: the set of clusters, users, and contexts kubectl can use. In CI it is how you select the target cluster and pin a default namespace.

What it does

kubectl config get-contexts lists available contexts; use-context switches the active one; set-context --current --namespace=X pins a default namespace so you stop typing -n. current-context prints what you are pointed at - the first thing to log in a deploy pipeline to prove you are on the right cluster.

Common usage

Terminal
kubectl config get-contexts
kubectl config current-context
kubectl config use-context prod-cluster
kubectl config set-context --current --namespace=staging
kubectl config view --minify --flatten        # the active context only

Common errors in CI

"error: no context exists with the name: \"X\"" means the context is not in the kubeconfig that CI loaded - the runner often has no ~/.kube/config, so set KUBECONFIG to the path you wrote the credentials to, or pass --context. "The connection to the server localhost:8080 was refused" is the tell-tale of no kubeconfig at all: kubectl fell back to its default and found nothing. Always echo kubectl config current-context early so a wrong-cluster deploy fails loudly instead of silently targeting the default namespace of the wrong cluster.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →