How to switch between Kubernetes clusters
You can find yourself as the Kubernetes administrator required to install more clusters from different teams. Once clusters are enabled, you always use kubectl to apply YAML configuration files, but how do you navigate between clusters?
Kube Context
Normally, you use the kubectl kubeconfig file to access each cluster you need to specify kubectl — kubeconfig [cluster name] is annoying if you need to administer multiple clusters. There is a better way to handle several clusters with kubectl with context. Contextual help by defining all clusters and users in a kubeconfig file and in that file, we can define a context for each cluster so that we can switch between clusters. This means only one kubeconfig file in a single location.
What is a cluster context?
In the kubeconfig file, we have a list of clusters, for each you have the cluster end-point and the cluster CA, a list of users and for each user the access credentials for the cluster, a client certificate, or secret account token. Users and clusters may receive arbitrary names that may be used to reference them in the kubeconfig file.
Current Context
Let’s say we have multiple clusters and multiple users in our kubeconfig file, when we execute kubectl apply [deployment-name.YAML) which context doe sit use? This is defined by the current context attribute in the kubeconfig file that references one of the contexts in the list.
So, how can we switch to another context to connect to a different cluster? Using the kubectl config use-context [context-name] command changes the current context to that you specify in the command. You can change the kubeconfig file and apply it again, but I think it’s a lot more effective to use the kubectl command. You don’t need to remember all of your context lists, simply type: kubectl config get-context and display which one is active one: kubectl config current-context.
Add a context
Let’s say you have one administrator user for each cluster and one user running scripts using the kubectl commands. And if you want to switch Kubernetes users to the same cluster? For example, you have a dev cluster, and you want the admin user of the test cluster to connect to the dev cluster as well. To do that you will have to add the user under user in the kubeconfig file, give him a name a token, or a cert, and create a new context in the dev context and reference it to the username in the same kubeconfig file. You may now change users within the same group.
Namespaces in contexts
Namespaces are also part of kubeconfig, the default namespace is configured, so it is not necessary to specify this when querying the default namespace. However, we need to explicitly specify the namespace in order to work with no default namespace. Let’s say we don’t use default namespaces at all in our project, all our applications will be grouped into namespaces with meaningful names, which means we will have to specify every namespace in our kubectl command, which could be inefficient and annoying. It would be more efficient for kubectl to switch from the default namespace to the one we want to work with. You can do this by using the kubectl config subcommand. kubectl config set-context — current — namespace=[name] and context has changed. This is very powerful and useful if you are working with multiple namespaces in your cluster.