BACK TO BLOG

kubectl get context: inspect the target before you act

Kubernetes contexts store the defaults that point commands toward a cluster. When staging and production span multiple facilities, a familiar context name can hide the wrong cluster reference or namespace. We keep those local defaults separate from the permissions a server actually grants. Here is how to inspect each binding, understand which configuration wins, and change the selected context without touching a running workload.

Get the current context and list configured contexts

To get the current kubectl context, run:

kubectl config current-context

To list all configured contexts, run:

kubectl config get-contexts

The list marks the saved current context with * in the CURRENT column. For names alone, use kubectl config get-contexts -o name. To display one context's row, use kubectl config get-contexts CONTEXT_NAME. These are local configuration commands, so they need access to the kubeconfig files, with no Kubernetes API permission required.

kubectl get context is not the built-in command for this task. Contexts are local configuration entries, rather than built-in Kubernetes resources retrieved by kubectl get.

Read cluster, user, and namespace as separate fields

A kubeconfig file holds connection settings for Kubernetes. Each named context groups a cluster reference, a user reference, and an optional namespace default. The cluster and user settings live in separate named entries, as shown in Kubernetes' multicluster configuration guide.

Output column What it means
CURRENT* identifies the saved current-context.
NAMEThe local name of the context. It can differ from the cluster name.
CLUSTERThe name of a kubeconfig cluster entry, which holds the address and connection settings for the API server, the endpoint that receives Kubernetes requests.
AUTHINFOThe name of a kubeconfig user entry, which holds credentials or a way to obtain them. It is not proof of the identity accepted by the server.
NAMESPACEThe stored default for namespaced requests. A blank field normally means default when no namespace override is supplied.

A cluster entry's name is a local alias, not a universal cluster identifier. Two contexts can reference the same cluster while using different users or namespaces. Neither a name such as prod-reader nor selecting that context grants read access.

A namespace scopes resources such as Pods within a cluster. The --namespace flag overrides the context's namespace for a request. It does not narrow cluster-scoped resources such as Nodes, and a stored namespace does not prove that namespace exists. These distinctions follow Kubernetes' namespace behavior.

Use a credential-free local example

A local kubectl client and Bash or Zsh are enough for this example. It creates a temporary kubeconfig with two contexts and an explicit starting context. The user entries are empty, the endpoints are placeholders, and no command contacts them.

The inspection commands need local file-read access. The later switching example also needs write access to this temporary directory and configuration file. No administrator role or cluster access is needed.

context_demo_dir=$(mktemp -d)
cat > "$context_demo_dir/config" <<'EOF'
apiVersion: v1
kind: Config
clusters:
- name: facility-a
  cluster:
    server: https://facility-a.example.invalid:6443
- name: facility-b
  cluster:
    server: https://facility-b.example.invalid:6443
users:
- name: reader-a
  user: {}
- name: reader-b
  user: {}
contexts:
- name: facility-a-staging
  context:
    cluster: facility-a
    user: reader-a
    namespace: inference
- name: facility-b-prod
  context:
    cluster: facility-b
    user: reader-b
    namespace: serving
current-context: facility-a-staging
EOF

Inspect this file explicitly, keeping your normal kubeconfig out of the example:

kubectl --kubeconfig="$context_demo_dir/config" config get-contexts
CURRENT   NAME                 CLUSTER      AUTHINFO   NAMESPACE
*         facility-a-staging   facility-a   reader-a   inference
          facility-b-prod      facility-b   reader-b   serving

The staging context references facility-a, reader-a, and inference. The production context references a different cluster, user, and namespace. Neither row contains credentials.

Context references point to separate cluster and user entries; namespace is a stored default

To list the available cluster and user entry names separately:

kubectl --kubeconfig="$context_demo_dir/config" config get-clusters
kubectl --kubeconfig="$context_demo_dir/config" config get-users

These list configured names, rather than discover live clusters or enumerate Kubernetes identities. An entry's presence says nothing about whether its connection or credentials work.

Inspect a specific context without switching

For a one-off inspection, name both the file and context. config view --minify keeps only the selected context and its referenced entries. JSONPath, a field-selection expression, limits the output to the four fields we need:

kubectl --kubeconfig="$context_demo_dir/config" \
  --context=facility-b-prod config view --minify \
  -o jsonpath='{.contexts[0].name}{"\t"}{.contexts[0].context.cluster}{"\t"}{.contexts[0].context.user}{"\t"}{.contexts[0].context.namespace}{"\n"}'
facility-b-prod	facility-b	reader-b	serving

The output contains the context name, cluster reference, user reference, and stored namespace, in that order. The saved current context remains unchanged:

kubectl --kubeconfig="$context_demo_dir/config" config current-context
facility-a-staging

There is an important distinction here: config current-context and the CURRENT marker report the saved selection, even when passed a different --context. The minified-view implementation applies --context to its selected view. A saved staging marker therefore does not establish the target of a separate command that explicitly selects production.

This output describes the chosen context's stored fields. It is not a full rendering of every request override: flags such as --cluster, --user, --server, and --namespace can change how a later API command runs.

Keep inspection output narrow. The config view reference documents --raw as exposing sensitive data. Leave that flag out, and avoid dumping an entire real kubeconfig into a terminal recording or support ticket. The field selection above excludes tokens, private keys, certificates, and credential-plugin settings.

Use kubeconfig files from trusted sources. Some credential plugins execute local programs to obtain credentials for API access; a clean context listing does not establish that a supplied configuration is safe to use.

Understand which kubeconfig wins

A correct context name can still come from an unexpected file. Kubectl follows this configuration loading order:

Configuration source Result
--kubeconfig=/path/to/configLoads that one file. No merging with other kubeconfig files.
KUBECONFIG environment variable, without --kubeconfigMerges the listed files in order. The separator is : on Linux and macOS, and ; on Windows.
Neither is suppliedLoads ~/.kube/config, without merging.

For merged configuration, the first file to set a value or named map entry wins. A later file does not replace an earlier context, cluster, or user with the same name. A duplicate user entry is taken as a whole from its first definition; later fields do not fill it in. The first nonempty saved current-context also wins, under the documented merge precedence rules.

For example, suppose staging.yaml selects facility-a-staging and production.yaml selects facility-b-prod:

KUBECONFIG order Saved current context
staging.yaml:production.yamlfacility-a-staging
production.yaml:staging.yamlfacility-b-prod

The reference names can conceal another collision. If both files define a cluster named gpu, their order determines which cluster entry that name resolves to. A context row can look identical while the underlying connection settings differ.

For a facility-specific runbook, an explicit trusted kubeconfig file removes this merge ambiguity. Combining it with an explicit context also removes dependence on a shared saved selection. A context name alone cannot establish the intended physical facility.

Switch only when you want a persistent local change

config use-context writes the saved selection. That change affects later commands reading the same configuration, including commands in another terminal. It does not modify cluster resources.

The following sequence stays inside the temporary file. It displays the starting bindings, selects production, displays that row, and restores staging:

kubectl --kubeconfig="$context_demo_dir/config" config get-contexts
kubectl --kubeconfig="$context_demo_dir/config" config use-context facility-b-prod
kubectl --kubeconfig="$context_demo_dir/config" config get-contexts
kubectl --kubeconfig="$context_demo_dir/config" config use-context facility-a-staging
kubectl --kubeconfig="$context_demo_dir/config" config get-contexts

The * moves from facility-a-staging to facility-b-prod, then back. The cluster, user, and namespace references remain unchanged. For a one-command choice, --context avoids this persistent write.

Changing a stored namespace is a different local edit, performed with config set-context NAME --namespace=VALUE. It changes a default and does not create or move any cluster resources. An explicit namespace on an individual namespaced request avoids changing that stored default.

Once finished, remove only the temporary example files and directory:

rm "$context_demo_dir/config"
rmdir "$context_demo_dir"
unset context_demo_dir

Separate configuration errors from access failures

Local inspection can succeed even when a context is unusable for an API request. get-contexts lists references without proving that the corresponding cluster or user entry exists. The minified view catches missing referenced entries before any network request.

Result Meaning
Only the header appears, with no context rowsNo contexts were loaded. An absent explicitly named file can produce this without a file-not-found error.
current-context is not setThe loaded configuration has no saved current selection. Context entries may still exist.
context ... not foundThe requested context name is absent from the loaded configuration.
cannot locate cluster ... or cannot locate user ... during minificationA context references a missing local entry. A row in get-contexts was insufficient to establish a complete binding.
Local permission deniedThe operating system denied file access. Changing Kubernetes permissions does not resolve that local denial.

None of these local commands establishes server reachability, certificate validity, authenticated identity, namespace existence, or authorization. For a separate API operation, permissions must cover the actual verb, resource, and scope. A Forbidden response is an API authorization denial, rather than proof that the context name is wrong.

Local inspection also does not exercise client/server compatibility. For API operations, upstream supports kubectl within one minor version of each API server it contacts, as defined by the version skew policy.

Keep local targeting separate from fleet operations

Context hygiene belongs alongside the other boundaries in bare-metal cluster operations: named targets, scoped credentials, and clear ownership. We prefer explicit file, context, and namespace choices in operational runbooks over reliance on whichever defaults a terminal happens to inherit.

clusterdOS is the open-source engine inside each cluster. It is built on Kubernetes, and can ship enterprise-grade Slurm and VMs. Slurm schedules batch computing jobs; virtual machines (VMs) run guest operating systems. Local kubectl selection remains a separate client concern. We act only on access customers explicitly grant, with role-based permissions for each person.

If operating clusters across facilities is taking time away from serving workloads, talk to an engineer about our fully managed clusters, covering architecture, deployment, and end-to-end operations.