How to Switch Kubernetes Cluster Context

Itacen Sabacok | May 14, 2022
1# when you have the context name then run below command to switch
2kubectl config use-context my-context
3
4# or modify kubeconfig files using subcommands like
5kubectl config set current-context my-context
1# find all contexts in kubeconfig file
2kubectl config get-contexts
3
4# find only clusters in kubeconfig file
5kubectl config get-clusters
6
7# you can check current context
8kubectl config current-context

kubectl config …

The loading order follows these rules:

  1. If the –kubeconfig flag is set, then only that file is loaded. The flag may only be set once and no merging takes place.
  1. If $KUBECONFIG environment variable is set, then it is used as a list of paths (normal path delimiting rules for your system). These paths are merged. When a value is modified, it is modified in the file that defines the stanza. When a value is created, it is created in the first file that exists. If no files in the chain exist, then it creates the last file in the list.
  1. Otherwise, ${HOME}/.kube/config is used and no merging takes place.


NOTE

you can use kubectx to switch the context also. click here to see more…