Day 3: Many Clusters, Many Namespaces
Goal: Never work in the wrong cluster again: understand the kubeconfig, switch quickly with kubectx and kubens, see the context in the prompt, open separate shells per cluster with kubie, and build your own kubeconfig for a ServiceAccount · Duration: approx. 60 min · Prerequisite: Day 2
Briefing
- The most expensive typo in everyday Kubernetes work is not a typo
but the wrong context:
kubectl deletein the production cluster instead of the test cluster. The tools of this day make the current context visible and the switch deliberate. - Two “clusters” are ready: the kind cluster
kind-werkzeugeand the simulator KubeDrill with the contextkubedrill. KubeDrill is a Kubernetes simulator without containers but with a real API; for switching contexts, it is a full second cluster.kubedrill startadds the contextkubedrillto your kubeconfig and leaves your current context as it was. Install it with brew, apt or dnf: https://itdrills.com/kubedrill.
| Question | Tool |
|---|---|
| Which clusters do I know, where am I right now? | kubectl config get-contexts, kubectx |
| Switch cluster or namespace quickly | kubectx, kubens |
| Always see where I am | kube-ps1 in the prompt |
| Two clusters at the same time without interfering with each other | kubie |
| Access for a service or colleague | your own kubeconfig with a token |
Drill 1: The kubeconfig
A kubeconfig has three lists, clusters (where),
users (who) and contexts (pairs of
both, plus a default namespace), as well as a
current-context:
kubectl config get-contexts
kubectl config view --minify -o jsonpath='{.contexts[0].name} {.clusters[0].cluster.server}{"\n"}'
ls -l ~/.kube/configExpected (port will differ for you):
CURRENT NAME CLUSTER AUTHINFO NAMESPACE
* kind-werkzeuge kind-werkzeuge kind-werkzeuge
kubedrill kubedrill kubedrill default
kind-werkzeuge https://127.0.0.1:45811
-rw------- 1 ubuntu ubuntu 6882 Nov 24 11:31 /home/ubuntu/.kube/config
--minify shows only the current context. The file
contains keys and certificates, which is why it is readable only by its
owner.
Several files can be merged via KUBECONFIG. This is
practical when each cluster has its own file:
mkdir -p ~/werkzeuge/tag03 && cd ~/werkzeuge/tag03
kubectl config view --minify --flatten --context kubedrill > kubedrill.yaml
kubectl config view --minify --flatten --context kind-werkzeuge > kind.yaml
KUBECONFIG=kind.yaml:kubedrill.yaml kubectl config get-contextsExpected:
CURRENT NAME CLUSTER AUTHINFO NAMESPACE
* kind-werkzeuge kind-werkzeuge kind-werkzeuge default
kubedrill kubedrill kubedrill default
--flatten embeds certificates so that the file works on
its own. kubectl takes the current-context from the first
file in the list that has one.
Save your progress? Sign up for free
Drill 2: kubectx and kubens
Two small programs from one project. Here UARCH is
x86_64 or arm64:
UARCH=$(uname -m | sed s/aarch64/arm64/)
for t in kubectx kubens; do curl -sL https://github.com/ahmetb/kubectx/releases/download/v0.11.0/${t}_v0.11.0_linux_$UARCH.tar.gz | tar xz -C /tmp $t && sudo install /tmp/$t /usr/local/bin/$t; done
kubectx --version
kubectx
kubectx kubedrill
kubectl get nodes | head -2
kubectx -
kubectx -cExpected:
0.11.0
kind-werkzeuge
kubedrill
✔ Switched to context "kubedrill".
NAME STATUS ROLES AGE VERSION
kubedrill-control-plane Ready control-plane 12s v1.37.2
✔ Switched to context "kind-werkzeuge".
kind-werkzeuge
kubectx - jumps back to the previous context, like
cd -. kubens does the same for namespaces. The
namespace is stored in the context, so it applies to every command
without -n:
kubens laden
kubectl get deploy
kubectl config get-contexts kind-werkzeuge
kubens -Expected:
✔ Active namespace is "laden"
NAME READY UP-TO-DATE AVAILABLE AGE
api 2/2 2 2 9m1s
cache 1/1 1 1 9m1s
web 3/3 3 3 9m1s
CURRENT NAME CLUSTER AUTHINFO NAMESPACE
* kind-werkzeuge kind-werkzeuge kind-werkzeuge laden
✔ Active namespace is "default"
Long context names from cloud consoles can be renamed:
kubectx kurz=sehr-langer-name. If fzf is
installed, both programs show a selection list when called without an
argument.
Save your progress? Sign up for free
Drill 3: The Context in the Prompt
kubectx makes switching easy, and with it forgetting where you currently are. kube-ps1 writes the context and namespace into the prompt:
curl -sLo ~/.kube-ps1.sh https://raw.githubusercontent.com/jonmosco/kube-ps1/v1.0.0/kube-ps1.sh
cat >> ~/.bashrc <<'EOF'
source ~/.kube-ps1.sh
KUBE_PS1_SYMBOL_ENABLE=false
PS1='$(kube_ps1) \$ '
EOF
source ~/.bashrc
kubens ladenExpected (prompt after the switch, colors omitted here):
✔ Active namespace is "laden"
(kind-werkzeuge:laden) $
Context and namespace appear in color before every command.
kubeoff hides the display, kubeon shows it
again. Go back with kubens default. If you use zsh with a
prompt framework (Starship, Powerlevel10k), you will find dedicated
Kubernetes modules there.
Save your progress? Sign up for free
Drill 4: Separate Shells with kubie
kubectx and kubens change the shared ~/.kube/config. If
you switch the context in one terminal, it applies immediately in all
others. kubie takes a different approach: each shell gets its own
temporary kubeconfig.
DARCH=$(dpkg --print-architecture)
curl -sLo /tmp/kubie https://github.com/sbstp/kubie/releases/download/v0.28.0/kubie-linux-$DARCH && sudo install /tmp/kubie /usr/local/bin/kubie
kubie --version
kubie ctx kind-werkzeuge -n ladenThis opens a new shell. Inside it:
kubie info ctx
kubie info ns
echo $KUBECONFIG
kubectl get deploy
exitExpected:
kubie 0.28.0
kind-werkzeuge
laden
/tmp/kubie-configH8ooXn.yaml
NAME READY UP-TO-DATE AVAILABLE AGE
api 2/2 2 2 10m
cache 1/1 1 1 10m
web 3/3 3 3 10m
After exit, the shell is gone and the context with it;
the shared kubeconfig was never changed. For single commands without a
shell, there is kubie exec:
kubie exec kubedrill default kubectl get nodes | head -2
kubectx -cExpected:
NAME STATUS ROLES AGE VERSION
kubedrill-control-plane Ready control-plane 12s v1.37.2
kind-werkzeuge
| kubectx/kubens | kubie | |
|---|---|---|
| Effect | changes ~/.kube/config for all shells |
own kubeconfig per shell |
| Two clusters in two terminals | trap | made for it |
| Scripts | specify --context/-n |
kubie exec |
A third tool with a similar goal, kubeswitch, has not been actively released since 2025; it is listed as a profile in Appendix C.
Save your progress? Sign up for free
Exercise: Speed Drills
- Which namespace are you working in right now? Target time: 10 s
- Which contexts exist? Only the names. Target time: 10 s
- At which address do you reach the API server of the current context? Target time: 30 s
Solution
kubens -c
kubectx
kubectl config view --minify -o jsonpath='{.clusters[0].cluster.server}{"\n"}'Expected:
default
kind-werkzeuge
kubedrill
https://127.0.0.1:45811
Save your progress? Sign up for free
Challenge
In an emergency, the shop API should also be able to talk to the
cluster from outside, with exactly the permissions of its ServiceAccount
and only for one hour. Build a standalone kubeconfig
api.yaml for the ServiceAccount api in the
namespace laden without touching your own
~/.kube/config, and prove that it has the ServiceAccount’s
permissions.
Hint: kubectl create token issues a
time-limited token. kubectl config set-cluster,
set-credentials and set-context write to the
file that KUBECONFIG points to.
Solution
cd ~/werkzeuge/tag03
TOKEN=$(kubectl -n laden create token api --duration=1h)
SERVER=$(kubectl config view --minify -o jsonpath='{.clusters[0].cluster.server}')
kubectl config view --minify --raw -o jsonpath='{.clusters[0].cluster.certificate-authority-data}' | base64 -d > ca.crt
export KUBECONFIG=api.yaml
kubectl config set-cluster werkzeuge --server=$SERVER --certificate-authority=ca.crt --embed-certs
kubectl config set-credentials api --token=$TOKEN
kubectl config set-context api --cluster=werkzeuge --user=api --namespace=laden
kubectl config use-context api
kubectl auth whoami -o jsonpath='{.status.userInfo.username}{"\n"}'
kubectl get deploy
kubectl get secrets
unset KUBECONFIG
rm ca.crt
kubectx -cExpected:
Cluster "werkzeuge" set.
User "api" set.
Context "api" created.
Switched to context "api".
system:serviceaccount:laden:api
NAME READY UP-TO-DATE AVAILABLE AGE
api 2/2 2 2 10m
cache 1/1 1 1 10m
web 3/3 3 3 10m
Error from server (Forbidden): secrets is forbidden: User "system:serviceaccount:laden:api" cannot list resource "secrets" in API group "" in the namespace "laden"
kind-werkzeuge
Reading yes, Secrets no: exactly the permissions from Day 2. After
one hour, the token is invalid and the file worthless. That is the
reason not to create long-lived token Secrets anymore:
create token issues short-lived tokens that the API server
checks itself. Delete api.yaml afterwards; for one hour, it
is a key to the cluster.
Save your progress? Sign up for free
Checkpoint
kubectx -c
kubens -cExpected:
kind-werkzeuge
default
Clean-up
cd ~/werkzeuge/tag03
rm -f api.yamlkubectx, kubens, kubie and the prompt stay; from now on they are part of everyday work.
Interview Note
- What is in a kubeconfig? Clusters (address, CA), users (certificate, token or a plugin such as a cloud CLI), contexts as pairs of both with a default namespace, and the current context.
- How do I prevent commands in the wrong cluster?
Make the context visible (prompt), use short-lived shells per cluster
(kubie), always specify
--contextin scripts, and access production only with a separate user with fewer permissions. - How do I give a service outside the cluster access?
A ServiceAccount with matching permissions, a short-lived token via
kubectl create token, or better the platform’s workload identity.
Enterprise Note
- One file per cluster under
~/.kube/and settingKUBECONFIGvia a script or direnv is more robust than one huge shared file that every cloud CLI keeps modifying. - Separate production by color: many teams color the prompt red for
production. With kube-ps1, this works through a custom function that
processes the context name
(
KUBE_PS1_CLUSTER_FUNCTION). - In companies, personal access goes through OIDC (kubelogin, Appendix
C), not through shared admin kubeconfigs. An admin file from
kind/kubeadmbelongs in the vault, not on laptops.