Kubectl Use — Context !!install!!
kubectl config set-context my-prod-context \ --cluster=production-cluster \ --user=admin \ --namespace=default # Rename by creating a new one and deleting the old kubectl config set-context new-name --cluster=old-cluster --user=old-user kubectl config delete-context old-name Delete a context kubectl config delete-context <context-name> Advanced Tips Temporary context switch (without changing default) # Use a different context for a single command kubectl --context=staging-asia get pods Multiple commands with temporary context kubectl --context=dev-europe get nodes kubectl --context=dev-europe get services Set default namespace for a context # Modify existing context kubectl config set-context --current --namespace=<namespace> Or specify context explicitly kubectl config set-context dev-europe --namespace=frontend Use aliases for efficiency Add to ~/.bashrc or ~/.zshrc :
alias k="kubectl" alias kctx="kubectl config get-contexts" alias kuse="kubectl config use-context" alias kcur="kubectl config current-context" Then use: kubectl use context
# Switch to development context kubectl config use-context dev-europe kubectl config use-context staging-asia kubectl use context
This guide should help you effectively manage multiple Kubernetes clusters and environments using kubectl contexts . kubectl use context
kuse prod-us-east k get pods # Now uses production context Add to ~/.bashrc :
After switching, all subsequent kubectl commands will use the new context. Create a new context # Basic syntax kubectl config set-context <context-name> \ --cluster=<cluster-name> \ --user=<user-name> \ --namespace=<namespace> Example:
|