Geavanceerd
Zoeken inHow-to-gidsen
Zo zoek je
  • Typ de taak in je eigen woorden (poort vinden, firewall openen, schijf vol) of plak een foutmelding tussen aanhalingstekens.
  • Kies "Snel antwoord" om alleen de commando's te zien, of een gebied om in één vakgebied te blijven.
  • "twee woorden" vindt precies die zin.
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken
Probeer:

How-to 38Kubernetes · kubectl

How to switch between clusters and namespaces

At work you have several clusters (dev, staging, production, maybe in AWS and Azure) and many namespaces. One command in the wrong cluster can delete production. kubectl keeps all of this in the kubeconfig: clusters, users and contexts (cluster + user + namespace). This how-to shows how to list, switch and set a default namespace, merge kubeconfig files, get credentials from EKS/AKS/GKE, and how to make the current cluster always visible.

  • 5 Lessen
  • 8 Commando's

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

$ als gewone gebruiker# als root (of met sudo)

38.1

The quick answer

kubectl config get-contexts
CURRENT   NAME          CLUSTER       AUTHINFO      NAMESPACE
*         dev           dev           dev-admin     shop
          prod-eu       prod-eu       prod-admin    default
kubectl config use-context prod-eu
kubectl config set-context --current --namespace=shop
kubectl config current-context
prod-eu

38.2

Step by step

  1. See where you are — before every risky command:
kubectl config current-context
kubectl config view --minify -o jsonpath='{..namespace}{"\n"}'
kubectl cluster-info
  1. Run one command in another cluster or namespace without switching:
kubectl get pods --context prod-eu -n shop
kubectl get pods -A
  1. Add a cluster's credentials (they are merged into ~/.kube/config):
aws eks update-kubeconfig --region eu-west-1 --name shop-prod --alias prod-eu
az aks get-credentials --resource-group rg-shop --name aks-shop-dev --context dev
gcloud container clusters get-credentials shop-stg --region europe-west4
  1. Several kubeconfig files — merge them with the KUBECONFIG variable:
export KUBECONFIG=~/.kube/config:~/.kube/lab-cluster.yaml
kubectl config get-contexts
kubectl config view --flatten > ~/.kube/merged && chmod 600 ~/.kube/merged
  1. Rename and clean up:
kubectl config rename-context arn:aws:eks:eu-west-1:111122223333:cluster/shop-prod prod-eu
kubectl config delete-context old-lab
  1. Make the cluster visible all the time — show it in the prompt, and make production look different:
~/.bashrc
kctx() { kubectl config current-context 2>/dev/null; }
PS1='[\u@\h \W $(kctx)]\$ '

Tools that help: kubectx / kubens (fast switch), k9s (shows the context at the top), and a separate, read-only user for production that you use by default.

38.3

Check that it worked

kubectl config get-contexts shows the * on the context you want, with the right namespace, and kubectl get pods lists the Pods you expect — not the ones of another cluster.

38.4

Common problems

ProblemCauseFix
error: context "prod" does not existWrong name, or the file is not in KUBECONFIG.kubectl config get-contexts; check echo $KUBECONFIG.
Unable to connect to the serverVPN off, IP not allowed, cluster stopped.Check VPN and the API server allow list.
You must be logged in to the server (Unauthorized)Expired token or cloud login.aws sso login, az login, or get credentials again.
exec plugin: … executable not foundThe auth plugin (aws, kubelogin, gke-gcloud-auth-plugin) is missing.Install it and make sure it is in PATH.
You changed the wrong clusterThe context was not what you thought.Show it in the prompt; use --context in scripts; read-only default for prod.
~/.kube/config permission warningThe file is readable by others.chmod 600 ~/.kube/config — it contains credentials.

38.5

Go further

1How do you run one command in another cluster without switching the current context?

Add --context NAME (and -n NAMESPACE) to the command.

2Which command sets the default namespace of the current context?

Alle commando's in dit hoofdstuk