Appearance

kubectl and your cluster context

Find out where kubectl is about to send a request and choose the command that answers your question.

What you will understand

  • Identify a cluster, user, and context in a kubeconfig.
  • Target a specific context and namespace for inspection.
  • Choose between listing objects, reading events, viewing logs, and previewing changes.

Builds on: Labels, selectors, and namespaces

Where kubectl sends your command

You have two Kubernetes clusters open on your laptop, one for practice and one at work. Both have a namespace called default. A command like kubectl get pods only makes sense once you know which cluster and namespace it targets, and which identity will make the request.

A kubeconfig is a client configuration file. Its cluster entries describe API endpoints and trust settings. Its user entries specify how the client obtains credentials. A context pairs a cluster with a user and can set a default namespace. The current context is the one kubectl uses unless you supply a different one. The KUBECONFIG environment variable can direct the client to other files; a command can also provide a specific file with --kubeconfig.

The Workbench activates a dedicated kubeconfig by sourcing its lab-env.sh. A context name such as kind-kv-lab is a useful cue. Verify where it points if there is any doubt. A context called test is only a local name, so it is worth checking the endpoint before using it.

Which cluster will this command reach?

A context chooses a cluster and user, and can set a default namespace. Explicit flags can change the target for one command.

  1. Read the context

    The current context references a cluster definition, a user configuration, and a default namespace. Its display name alone proves none of these.

  2. Check the default namespace

    kubectl uses the selected endpoint and credentials, then addresses a namespaced resource in training. Server authorization still applies.

  3. Target the practice cluster

    --context and -n make the destination visible for this command. They do not create namespaces or grant permissions.

One kubeconfig context is selected; credential values are intentionally never shown.

Inspect the command target

Two namespaces hold the same ConfigMap name. Follow a kubectl lookup and override the namespace for just one command.

1 / 4

From kubectlTo Lab context

Choose the lab context

kubectl reads the specified context from its kubeconfig. The context selects the cluster and identity and supplies a default namespace when the command has no namespace flag.

Selected context
Before
No explicit API target yet
After
kind-kv-lab with default namespace kv-context-a
Choose the lab context

kubectl reads the specified context from its kubeconfig. The context selects the cluster and identity and supplies a default namespace when the command has no namespace flag.

Both commands use the same local kind cluster and API identity. The exercise changes a context preference only in a temporary kubeconfig copy.

Does --namespace change the context for later commands?
Reveal prediction answer

The flag scopes one request. A later command uses the context’s default unless it supplies another namespace.

Compare the values returned before, during, and after the namespace override.

Read the same name in two namespaces
Explore another case

Open Inspect the command target to see how two successful requests for the same name can return different objects.

Check your context and namespace

Read the current context and server address before practice commands. The JSONPath expression below prints only the address and namespace. A blank namespace means the context has no namespace set; it normally falls back to default for namespaced commands without -n. Avoid printing a kubeconfig with credentials included, especially in a shared terminal or support ticket.

kubectl config get-contexts
kubectl config current-context
# Show only the chosen endpoint and default namespace, not credential data.
kubectl config view --minify \
  -o jsonpath='{.clusters[0].cluster.server}{"\n"}{.contexts[0].context.namespace}{"\n"}'
kubectl cluster-info
kubectl get namespaces

In the next block, replace YOUR_LAB_CONTEXT with the name you verified. --context selects the context for one command and -n chooses the namespace for that operation. The last example changes the namespace preference recorded for the context in your local config. It neither creates a namespace nor sends an application change.

kubectl --context YOUR_LAB_CONTEXT -n selection-lab get pods
kubectl --context YOUR_LAB_CONTEXT -n selection-lab describe pod catalog-demo
kubectl --context YOUR_LAB_CONTEXT auth can-i get pods -n selection-lab
# A local preference change, not a server-side namespace creation:
kubectl config set-context YOUR_LAB_CONTEXT --namespace=selection-lab

Choose an inspection command

Choose the command that answers your question. get lists matching objects and summarizes their state. describe gives a human-readable description and often shows relevant recent events. logs reads a container’s output. For a resource field such as spec.containers, explain reads schema information. diff previews the difference between a manifest and the current API state, and returns code 1 when it sees changes.

Choose evidence for the question you have

Use a listing to find the object, descriptions and events to investigate startup, and logs to read what the process emitted.

  1. Find the object

    get returns the live API representation or summary. It answers which object and state the API currently reports.

  2. Check events and conditions

    describe combines fields and events. explain reads schema documentation rather than the running application.

  3. Read application output

    logs reads container output. Select the container and use previous when inspecting the last terminated instance.

One Pod is the investigation target; commands use the already verified context.

Make the target explicit

If you created catalog-demo in the previous lesson, inspect it again with an explicit destination. If it is missing, check the namespace and permissions before creating a second copy. auth can-i answers whether the caller is authorized for the requested action. A permitted write can still be rejected later by validation or admission.

kubectl --context YOUR_LAB_CONTEXT -n selection-lab get pod catalog-demo -o yaml
kubectl --context YOUR_LAB_CONTEXT -n selection-lab logs catalog-demo -c web --tail=20
kubectl explain pod.spec.containers
# Preview an existing local manifest before its next write.
kubectl --context YOUR_LAB_CONTEXT -n selection-lab diff -f catalog-demo.yaml

Read the same name in two namespaces

Source lab-env.sh from the disposable kind lab. These namespaces each get a ConfigMap named catalog. The example changes the default namespace in a temporary kubeconfig copy to keep the lab’s original context settings intact.

(
  set -euo pipefail
  config=$(mktemp)
  trap 'rm -f "$config"' EXIT
  cp "$KUBECONFIG" "$config"
  export KUBECONFIG="$config"
  kubectl --context kind-kv-lab create namespace kv-context-a
  kubectl --context kind-kv-lab create namespace kv-context-b
  kubectl --context kind-kv-lab -n kv-context-a create configmap catalog --from-literal=color=blue
  kubectl --context kind-kv-lab -n kv-context-b create configmap catalog --from-literal=color=green
  kubectl config set-context kind-kv-lab --namespace=kv-context-a
  kubectl --context kind-kv-lab get configmap catalog -o jsonpath='{.data.color}{"\n"}'
  kubectl --context kind-kv-lab -n kv-context-b get configmap catalog -o jsonpath='{.data.color}{"\n"}'
  kubectl --context kind-kv-lab get configmap catalog -o jsonpath='{.data.color}{"\n"}'
  kubectl --context kind-kv-lab delete namespace kv-context-a kv-context-b
)

The lookups return blue, green, then blue. After the command with -n kv-context-b, the following command uses the default in the temporary context again.

Look here when your query returns nothing

A successful command proves that the operation worked in the context you sent it to. It tells you nothing about a different cluster. Before running a write, read the context, namespace, resource type, and name aloud or from the command line. The module lab uses explicit scope so you can practice on a dedicated cluster.

Check your understanding

You list Pods with no namespace flag and get an empty result. What should you check first?

Check the active context and the namespace the command selected. Repeat the read with the verified lab context and an explicit namespace. Check authorization if you get a permission error. An empty result in one scope does not tell you whether a Pod exists elsewhere.

Primary documentation

Go to the source

Reviewed 2026-10-27 · Kubernetes 1.37 · Related local exercise

Search the book

Search loads when you open it.

Keyboard shortcuts

Ctrl + K / ⌘ + K
Search the book
?
Open this guide
Tab / Shift + Tab
Move between controls
Enter / Space
Activate a focused button or disclosure
← / →
Focused visual timeline · previous or next step
Home / End
Focused visual timeline · first or last point
Esc
Close the open dialog or menu