Kubernetes foundations: the cluster and kubectl · 6 MIN READ

kubeconfig, contexts and namespaces: know where every kubectl command goes

Read a kubeconfig, list, create and switch contexts, set a default namespace, and use -n, -A and explicit contexts so that commands reach the cluster and namespace you intend.

Every kubectl command goes to one API server, as one user, in one namespace, and all three come from a file you rarely look at. The most expensive kubectl mistakes are seldom wrong commands; they are right commands sent to the wrong cluster or namespace. This lesson explains kubeconfig, contexts and namespaces, and the habits that keep production out of reach of a command meant for your laptop.

kubernetes · A MAP OF THE IDEA

  1. Check current-context
  2. Choose the namespace with -n
  3. Search widely with -A
  4. Pass --context in scripts

Useful when

  • One file describes every cluster, identity and default namespace you use.
  • Per-namespace contexts make switching cheap and explicit.

Watch out

  • use-context changes every terminal that shares the kubeconfig.
  • A remembered default namespace makes objects seem to vanish.

Implementation & operation cost: A visible context in the prompt and explicit --context and -n flags in every script.

01

What kubeconfig contains

kubectl reads ~/.kube/config unless the KUBECONFIG environment variable lists other files, which it merges in order (colon-separated on Linux and macOS). The file holds three lists and a pointer. clusters hold an API server URL and the CA certificate to trust. users hold credentials: a client certificate, a token, or an exec plugin that fetches short-lived tokens, which is how EKS, GKE and AKS usually sign you in. contexts each join one cluster and one user under a name, optionally with a default namespace. current-context names the context kubectl uses when you do not pass --context. kind create cluster adds a cluster, a user and a context, all called kind-NAME, and makes that context current, which is why creating a cluster quietly changes where your next command goes.

yaml
apiVersion: v1
kind: Config
clusters:
- name: kind-kind
  cluster:
    server: https://127.0.0.1:6443
    certificate-authority-data: LS0tLS1CRUdJTi...
users:
- name: kind-kind
  user:
    client-certificate-data: LS0tLS1CRUdJTi...
    client-key-data: LS0tLS1CRUdJTi...
contexts:
- name: kind-kind
  context:
    cluster: kind-kind
    user: kind-kind
- name: dev
  context:
    cluster: kind-kind
    user: kind-kind
    namespace: dev
current-context: kind-kind
Read the code

Two contexts share one cluster and one user and differ only in their default namespace, a common way to give each team or environment its own entry. The user's credentials are an embedded certificate and private key with full admin rights on the kind cluster, so treat the file like a private key: never commit it, and keep production credentials in a separate file that you add to KUBECONFIG only when you need it.

02

Listing, switching and creating contexts

kubectl config get-contexts lists every context and marks the current one with an asterisk; kubectl config current-context prints only its name, which is what scripts should check. use-context NAME rewrites current-context in the file, so it affects every terminal and every tool that reads the same kubeconfig, not only the shell where you typed it. set-context NAME --cluster=C --user=U --namespace=N creates or edits a context entry. A context is only a named triple, so one per namespace you work in costs nothing, and none of these commands touch the server: the namespace does not even have to exist yet. delete-context removes the entry but not the cluster or user it refers to. kubectl config view prints the merged configuration with credentials redacted, and --minify limits it to what the current context uses.

03

Namespaces are the default scope

Most objects, such as pods, Deployments, Services and Secrets, live in a namespace; nodes, namespaces, PersistentVolumes, StorageClasses and ClusterRoles do not, and kubectl api-resources shows which is which in its NAMESPACED column. Every command acts in one namespace: the one given with -n or --namespace, otherwise the context's namespace, otherwise default. -A (--all-namespaces) widens a read to every namespace and adds a NAMESPACE column. Names are unique only within a namespace, so web in shop and web in default are unrelated objects. A fresh cluster has default, kube-system for cluster components, kube-public, kube-node-lease for node heartbeats and, in kind, local-path-storage. Namespaces scope names, RBAC permissions and quotas; they are not a network boundary unless NetworkPolicies make them one.

04

Set a default namespace, and remember that you did

Typing -n shop on every command is tedious, so you store it once in the context with kubectl config set-context --current --namespace=shop. That convenience is also the most common source of a puzzling No resources found: the setting lives in kubeconfig, survives new terminals and reboots, and kubectl never reminds you of it. Look at the NAMESPACE column of get-contexts when results seem to be missing. An explicit -n always overrides the context's namespace, and so does a namespace written in a manifest's metadata. When a manifest names one namespace and -n names another, kubectl refuses with the namespace from the provided object does not match, rather than guessing which one you meant.

bash
kubectl config set-context --current --namespace=shop
kubectl config get-contexts
kubectl get deployments
kubectl get deployments -n default
Read the code

After set-context, the current line of get-contexts shows shop in its NAMESPACE column, and a plain get deployments lists the objects in shop. The last command looks in default for this one invocation only; the stored setting stays shop until you change it with another set-context.

05

Guarding against the wrong context

Wrong-context accidents follow a pattern: a context switched in one terminal changes the file every other terminal reads; kind create cluster or a cloud CLI's get-credentials command makes a new cluster current without asking; a script runs against whatever happens to be current. Guards that work: show the context and namespace in your shell prompt (kube-ps1 or your prompt theme can do it); keep production credentials in a separate kubeconfig that you load deliberately, with read-only permissions by default; in scripts and CI, pass --context and -n on every command or point KUBECONFIG at a file that contains only the intended cluster; and make destructive scripts check kubectl config current-context first. The lab terminal holds one cluster, but --context works there for any context in its kubeconfig, so you can practise the habit.

bash
kubectl --context kind-staging -n shop get deployments
KUBECONFIG=$HOME/.kube/prod.yaml kubectl get nodes
Read the code

The first command names its cluster and namespace explicitly, so it does the same thing whatever is current. The second points a single command at a dedicated production kubeconfig without merging it into your everyday file. Both make the target visible in shell history and in code review, which is where wrong-context mistakes are caught.

06

Try it in the lab terminal

Create a namespace and a Deployment inside it, then see how the namespace decides what a plain command shows and how -n and -A override it. Next, store a default namespace in the current context, add a second context for the same cluster and switch to it. The last command is meant to fail, because kubectl can only switch to contexts that already exist in kubeconfig.

bash
kind create cluster
kubectl config get-contexts
kubectl create namespace shop
kubectl create deployment web --image=nginx:1.27 -n shop
kubectl get deployments
kubectl get deployments -A
kubectl config set-context --current --namespace=shop
kubectl get deployments
kubectl config set-context dev --cluster=kind-kind --user=kind-kind --namespace=default
kubectl config use-context dev
kubectl config get-contexts
kubectl get deployments
kubectl get deployments -n shop
kubectl config use-context prod
Read the code

get-contexts starts with one context, kind-kind, current and without a namespace. The Deployment created with -n shop is invisible to a plain get deployments, which answers No resources found in default namespace., and appears with -A beside the system Deployments. After set-context --current --namespace=shop the same plain command lists web. The dev context reuses the kind-kind cluster and user; after use-context dev the asterisk moves to dev, kind-kind keeps shop in its NAMESPACE column, and plain commands look in default again until you add -n shop. use-context prod fails with error: no context exists with the name: "prod".

TAKE THIS WITH YOU

A context is a named cluster, user and namespace in kubeconfig, and current-context silently decides where every command goes. Keep it visible in your prompt, pass --context and -n in scripts, and keep production credentials out of your everyday file.

Check your understanding

Yesterday you ran kubectl config set-context --current --namespace=shop. Today, in a new terminal, kubectl get deployment web reports NotFound, although web runs in default. What is still in effect?

Go to the source

Progress stays in this browser. No account needed.