current-context is not set: A kubeconfig exists but no context is selected
The file is present and lists clusters, and none is chosen. kubectl does not pick one for you, even when there is only a single option.
Applies to: All Kubernetes versions
What it means
A kubeconfig contains clusters, users, and contexts binding the two, plus a current-context naming which to use. If that field is empty or names a context that does not exist, kubectl has no cluster to talk to and says so. This happens after merging kubeconfig files, after hand-editing one, and after a context is deleted while it was the current one. It is also common in automation that writes a kubeconfig programmatically and omits the field, since everything else looks complete.
Most common causes
- The
current-contextfield is empty. - It names a context that has been deleted or renamed.
- Several kubeconfigs merged via
KUBECONFIGwhere none sets a current context. - A hand-edited or generated kubeconfig missing the field.
- A context deleted with
kubectl config delete-contextwhile it was current.
How to diagnose it
- List the contexts available:
kubectl config get-contexts. The current one is marked, and no mark confirms the problem. - Read the field directly:
kubectl config current-context. - Check whether several files are merged:
echo $KUBECONFIG. - Inspect the merged result:
kubectl config view.
How to fix it
- Select a context:
kubectl config use-context NAME. - Pass one per command with
--context NAMEif switching is undesirable. - Set the field explicitly in any generated kubeconfig.
- Use
kubectl config set-context --current --namespace=NSto set a default namespace once the context is selected, which removes a large amount of repetitive typing.
Notes
Because a context also carries a default namespace, an unset or wrong context is behind many apparent NotFound errors. Confirming the context before investigating a missing object is a cheap first step.
Related
- The connection to the server localhost:8080 was refused — kubectl has no kubeconfig
- Error from server (NotFound) — The named object does not exist
Sources
- Kubernetes documentation — Organizing Cluster Access Using kubeconfig Files
- Kubernetes documentation — kubectl config
- Kubernetes documentation — kubectl Quick Reference