In kubectl we often need to switch between different contexts to connect to different clusters, or access different namespaces, or use different user identities. This can be done in a number of different ways, including using the –kubeconfig parameter to specify a different Kubeconfig file, and using the kubectl config subcommand to switch the context. Kubectl -cf and Kubectx + kubens can be used to switch the context of kubectl gracefully. ☕ ️

Kubectl context

Before we introduce kubectl-cf, Kubectx, and Kubens, we will introduce the concept of Kubectl context, in case the reader is not aware of the content of kubectl context.

Anyone who knows a little about the Kubectl command will know that the Kubectl configuration comes from the Kubeconfig file. The default kubeconfig file is located at ${HOME}/.kube/config. This contains all of kubectl’s context information, such as:

apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: {truncated}
    server: https://api-server.wbsnail.com:6443
  name: kubernetes
contexts:
- context:
    cluster: my-cluster
    namespace: default
    user: admin
  name: admin@my-cluster
current-context: admin@my-cluster
kind: Config
users:
- name: admin
  user:
    client-certificate-data: {truncated}
    client-key-data: {truncated}
Copy the code

A context contains cluster, namespace, and user information. The current context is also determined by current-context (“admin@my-cluster”).

As you can see from the above configuration, a Kubeconfig file may contain multiple clusters, namespaces, and Users in the following format:

apiVersion: v1
kind: Config
preferences: {}

clusters:
- cluster:
  name: development
- cluster:
  name: scratch

users:
- name: developer
- name: experimenter

contexts:
- context:
  name: dev-frontend
- context:
  name: dev-storage
- context:
  name: exp-scratch

current-context: dev-frontend
Copy the code

When using Kubectl, you need some way to switch between multiple contexts.

If you don’t have a Kubeconfig file, Kubectl can also connect to the cluster using a set of parameters, The value can be –server, –certificate-authority, –client-certificate, –client-key, or –namespace. In most cases, this operation is not performed.

Context switching

Specify parameters directly

Specify specific context parameters for the current command, such as –namespace, –cluster and –user, or specify –context parameters directly.

use--kubeconfigparameter

You can use kubectl’s –kubeconfig parameter to specify the kubeconfig file for context switching, in effect replacing the entire configuration:

kubectl --kubeconfig=${HOME}/.kube/local.kubeconfig get pods
Copy the code

Switching kubectl contexts in this way is less difficult, but the commands are cumbersome.

usekubectl configSons command

The kubectl config subcommand provides a series of commands to view and modify the current configuration, including context switching:

Modify kubeconfig files using subcommands like "kubectl config set current-context my-context" 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. 2. 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. 3. Otherwise, ${HOME}/.kube/config is used and no merging takes place. Available Commands: current-context Displays the current-context delete-cluster Delete the specified cluster from the kubeconfig delete-context Delete the specified context from the kubeconfig get-clusters Display clusters defined in the kubeconfig get-contexts Describe one or many contexts rename-context Renames a context from the kubeconfig file. set Sets an individual value in a kubeconfig file set-cluster Sets a cluster entry in kubeconfig set-context Sets a context entry in  kubeconfig set-credentials Sets a user entry in kubeconfig unset Unsets an individual value in a kubeconfig file use-context Sets the current-context in a kubeconfig file view Display merged kubeconfig settings or a specified kubeconfig file Usage: kubectl config SUBCOMMAND [options] Use "kubectl <command> --help" for more information about a given command. Use "kubectl options" for a list of global command-line options (applies to all commands).Copy the code

How to switch context:

kubectl config use-context admin@my-cluster
Copy the code

Switching kubectl contexts in this way is less difficult, but the commands are equally tedious.

Using an alias

The above two methods are easy to operate, the only problem is that the command is too cumbersome, the first solution is to set alias, such as I used to set:

alias k='kubectl'
alias kp='kubectl --kubeconfig=${HOME}/.kube/production.kubeconfig'
alias kl='kubectl --kubeconfig=${HOME}/.kube/local.kubeconfig'
Copy the code

This allows KP to operate the production cluster and KL to operate the local cluster.

Or:

alias k='kubectl'
alias kp='kubectl config use-context admin@production-cluster'
alias kd='kubectl config use-context admin@local-clusetr'
Copy the code

It is also possible to switch clusters using KP and KL.

Is there a better way to do this? Kubectl-cf, Kubectx and Kubens are plugins for kubectl.

About kubectl plug-in, see the official document: the Extend kubectl with plugins | Kubernetes, in simple terms, all kubectl – at the beginning of the command is kubectl plug-in, very simple?

For example, kubectl-cf can be called either using kubectl-cf or using kubectl cf.

Use kubectl – cf

Spongeprojects/Kubectl-cf is a tool for switching between multiple Kubeconfig files and can be downloaded and installed on the Release page: Releases Releases · SpongeProjects/Kubectl-cf · GitHub.

Kubectl-cf Fork: wbsnail/kubectl-cf

As mentioned above, as kubectl plug-in, after installation kubectl – cf can pass kubectl – cf and kubectl cf calls in two ways, has the following three basic patterns of use:

Kubectl-cf kubeconfig cf [config] kubeconfig cf [config] kubeconfig cf [config] kubeconfig CFCopy the code

Kubectl – cf kubeconfig file to “*. Kubeconfig” suffix format in the ${HOME} /. Kube directory, such as “${HOME} /. Kube/production. Kubeconfig”, “${HOME} /. Kube/local. Kubeconfig”.

It works like this:

Kubectl-cf production: kubectl-cf production: kubectl-cf production: kubectl-cf production Can be shortened to kubectl-cf p, similar to Docker.

Note that kubectl-cf is used to switch between KubeconFig files. If you need to switch between different contexts in a single KubeconFig (or multiple KubeconFigs), you need to use another kubectl plugin: Kubectx.

Using kubectx

Kubectx is a tool for switching between multiple contexts and can be downloaded and installed on the Release page with Releases · Ahmetb/Kubectx · GitHub, or installed using Krew:

kubectl krew install ctx
Copy the code

It is similar to kubectl-cf but does not provide an interactive command line:

kubectx admin@production
kubectl ctx admin@production
Copy the code

Using kubens

There are also many times when you just want to switch between namespaces. For example, for the next few commands I want to operate under dev namespace, I don’t want to add “-n dev” to each command.

Kubens and Kubectx are in the same repository and can also be installed on the release page using Releases · Ahmetb/Kubectx · GitHub, which can also be installed using Krew:

kubectl krew install ns
Copy the code

It works exactly the same as Kubectx:

kubens dev
kubectl ns dev
Copy the code

Used together

This is how I use the kubectl command in practice:

#Set an aliasAlias k='kubectl' # in rc files such as.bashrc,.zshrc k get Pods
#Switch kubeconfig file, l islocalThe prefix
k cf l

#Switch the kubeconfig file, p is the production prefix
k cf p

#Switching contexts (I generally don't use Kubectx with kubectl-cf, because it's clearer to put different cluster configurations in different files)
k ctx admin@my-cluster

#Switch the namespace
k ns dev

#Switch the namespace
k ns default
Copy the code

Forget kubectl –kubeconfig and kubectl config use-context because these two commands are too long…

This makes the kubectl context switch operation the most elegant! Do you have any suggestions for better use? Please let me know by any means 💌


☕️ — Kubectl-cf, Kubectx, and Kubens-wbsnail

The resources

Configure Access to Multiple Clusters | Kubernetes

Extend kubectl with plugins | Kubernetes

Krew – kubectl plugin manager

Who Lives in a Pineapple Under the Sea? , a lot

GitHub – spongeprojects/kubectl-cf: Faster way to switch between kubeconfig files.

GitHub -wbsnail /kubectl-cf: Easier to switch between KubeconFig configuration files.

GitHub – ahmetb/kubectx: Faster way to switch between clusters and namespaces in kubectl