kubectl config use-context: Select Clusters in CI
Set the active kubeconfig context so subsequent commands target the right cluster.
kubectl config use-context selects which cluster/user/namespace tuple kubectl talks to. In multi-cluster CI it is the safety step before any apply, ensuring you deploy to the intended cluster. The context name must already exist in kubeconfig (added by get-credentials).
Common flags
CONTEXT_NAME- the context to activate (positional)kubectl config get-contexts- list available contextskubectl config current-context- print the active one
Example in CI
Switch to the prod context, then confirm it.
shell
kubectl config use-context prod-cluster
kubectl config current-contextCommon errors in CI
- error: no context exists with the name: "X" - context not in kubeconfig (run get-credentials first)
- error: current-context is not set - no active context selected
- The connection to the server localhost:8080 was refused - kubeconfig empty/unset
Key takeaways
- Selects the active cluster context before any deploy command.
- In multi-cluster CI it prevents deploying to the wrong cluster.
- The context must already exist; get-credentials adds it.
Related guides
gcloud container clusters get-credentials: GKE kubeconfigFetch GKE cluster credentials into kubeconfig from CI: required flags, an example, and the auth-plugin and ac…
az aks get-credentials: AKS kubeconfig in CIFetch AKS cluster credentials into kubeconfig from CI: the key flags, a pipeline example, and the auth and ac…
kubectl apply: Deploy Manifests in CIApply Kubernetes manifests declaratively in CI: the flags you use most, a pipeline example, and the apply err…