Dit deel is nog niet vertaald. Je ziet de Engelse tekst.
$ als gewone gebruiker# als root (of met sudo)
The quick answer
Step by step
- See where you are — before every risky command:
kubectl config current-context
kubectl config view --minify -o jsonpath='{..namespace}{"\n"}'
kubectl cluster-info- Run one command in another cluster or namespace without switching:
- 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- Several kubeconfig files — merge them with the
KUBECONFIGvariable:
export KUBECONFIG=~/.kube/config:~/.kube/lab-cluster.yaml
kubectl config get-contexts
kubectl config view --flatten > ~/.kube/merged && chmod 600 ~/.kube/merged- 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- Make the cluster visible all the time — show it in the prompt, and make production look different:
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.
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.
Common problems
| Problem | Cause | Fix |
|---|---|---|
error: context "prod" does not exist | Wrong name, or the file is not in KUBECONFIG. | kubectl config get-contexts; check echo $KUBECONFIG. |
Unable to connect to the server | VPN 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 found | The auth plugin (aws, kubelogin, gke-gcloud-auth-plugin) is missing. | Install it and make sure it is in PATH. |
| You changed the wrong cluster | The context was not what you thought. | Show it in the prompt; use --context in scripts; read-only default for prod. |
~/.kube/config permission warning | The file is readable by others. | chmod 600 ~/.kube/config — it contains credentials. |
Go further
- Command pages: kubectl, aws, az, gcloud.
- Next how-tos: Port-forward to a service, Find why a Pod does not start, Get the kubeconfig of EKS, AKS or GKE.
- Learn it: CKA — kubeconfig, CKA — RBAC users, Cloud logins in daily work.
- Full guides: kubectl, k9s, eksctl, Azure CLI.
1How do you run one command in another cluster without switching the current context?
Add --context NAME (and -n NAMESPACE) to the command.