KubeErrors

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

How to diagnose it

  1. List the contexts available: kubectl config get-contexts. The current one is marked, and no mark confirms the problem.
  2. Read the field directly: kubectl config current-context.
  3. Check whether several files are merged: echo $KUBECONFIG.
  4. Inspect the merged result: kubectl config view.

How to fix it

  1. Select a context: kubectl config use-context NAME.
  2. Pass one per command with --context NAME if switching is undesirable.
  3. Set the field explicitly in any generated kubeconfig.
  4. Use kubectl config set-context --current --namespace=NS to 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

Sources

Pages on this site are written with AI assistance from the primary sources listed on each page, then checked against those sources before publishing.