Kubectl Set-context -

kubectl set-context production --cluster=aws-prod --user=admin-user This creates the context but does not switch to it. To switch, you would run kubectl use-context production . This is the most common real-world use case. You have a context called staging , but you are tired of typing -n backend for every command.

export KUBECONFIG=~/.kube/config:~/secrets/gke-config kubectl set-context gke-prod --cluster=gke-prod --user=gke-user The command will update the first writable file in the list (usually the first one). kubectl set-context is deceptively simple. It does one small thing—modifying a field in a text file—but that small thing is the foundation of safe, efficient multi-cluster Kubernetes administration. kubectl set-context

kubectl set-context --current --namespace=backend Output: You have a context called staging , but

# Good: Relies on context namespace kubectl get pods kubectl get pods -n default 3. Verify Before You Execute Create a shell alias to show your current context in your prompt: It does one small thing—modifying a field in

kubectl set-context development --cluster=staging-cluster This updates the development context to use staging-cluster instead of its original cluster. kubectl set-context modifies your kubeconfig file (usually ~/.kube/config ). You can see the changes directly:

kubectl set-context development --namespace=testing Need to repoint a context to a different cluster? You can do that without deleting and recreating it.

error: