Wechat official account: Operation and maintenance development story, author: Double Dong

In the process of using Kubectl operation kubernetes cluster, may encounter the need to operate different cluster problems, such as local minikube cluster and online deployment cluster, or online test cluster and online deployment cluster, if the switch between the cluster is not frequent, can also use stupid method, For example, prepare multiple config files in the /.kube folder, change the name of config when you want to switch, or use an alias, but for those who need to switch operation clusters frequently, this method is obviously too cumbersome, and certainly not cool enough. Of course, K8S is a very complete cluster solution, must take this problem into account, here uses the K8S config file context(context). Kubernetes. IO/docs/tasks /… We will not go into the details of this method, but we will talk about how to easily generate the context when there are already two clusters of config files. Of course, in the second part of the above document, I also need to explain this, or you can read the English original. I have encountered a pit here, so I will explain in detail. Methods first, we all know that Kubectl will default to one place to look for config if it does not set the environment variable to the config location. For Mac, that is

$HOME/.kube/config

Copy the code

Since the configuration files of the two clusters may be assigned by the administrator only for your own use, the user name may be the same. In this case, if the connection of the cluster is enabled with TLS authentication, Kubectl’s config file will use the same client-certificate-data and client-key-data. If two clusters are on the same network of the same service provider, it is ok. If two clusters are on different networks, it will only be able to connect to one of them. In the case that another cluster cannot be connected, my solution is to change the user name, but note that in the context of config, you can see two fields: user and name. We need to change the user field, which is only a name and does not affect the user authentication on the cluster side. After modifying the user field, Remember to go to the Users field, there is a name, the name value should be the same as the value of the user field, change this to the same new value, save, reconfigure the environment variable to make it effective, this is ready to use, I wrote a multi-cluster merge example, Kube /config: kubectx: kubectx: kubectx: kubectx: kubectx: kubectx: kubectx: kubectx: kubectx: kubectx: kubectx: kubectx

apiVersion: v1 kind: Config preferences: {} clusters: - cluster: server: https://xxxxx:6443 certificate-authority-data: -cluster: server: https://xxxxx:6443 certificate-authority-data: XXXXX name: cluster1 # Cluster2 # contexts: -Context: Cluster: Cluster1 # Context: Cluster1 # User: user2 # User: cluster2 # user: user2 # user: cluster2 # users: - name: User1 # user in the first cluster context user user: client-certificate-data: XXXXXXXXXXX client-key-data: xxxxXXXXXXXXxxx-name: User2 # user in the second cluster context user: client-certificate-data: XXXXXXXXXXX client-key-data: XXXXXXXXXXXXXXXCopy the code

For the cluster name and user name of Clusters,contexts, Users, you can add any number of customized names as long as the user name and cluster name are not the same

As for the official way to switch the cluster, you can also find in the above documentation, and I will not go into details here. Here I mainly introduce a third-party tool to help us do this more quickly: Kubectx

1 installation

Kubectx and Kubens are two executable files that are used to switch the selected cluster and switch the namespace in the selected cluster respectively

➜ / Users/MAC git clone https://github.com/ahmetb/kubectx ➜ / Users/MAC CD kubectx ➜ / Users/MAC/kubectx git: (master)  ll total 144 -rw-r--r-- 1 mac staff 968B 6 20 00:48 CONTRIBUTING.md -rw-r--r-- 1 mac staff 11K 6 20 00:48 LICENSE README. Md drwxr-xr-x 4 MAC staff 128B 6 20 00:48 CMD drwxr-xr-x 8 MAC staff 256B 6 20 00:48 completion -rw-r--r-- 1 mac staff 765B 6 20 00:48 go.mod -rw-r--r-- 1 mac staff 22K 6 20 00:48 go.sum Drwxr-xr-x 5 MAC staff 160B 6 20 00:48 IMG DRwxr-xr-x 7 MAC staff 224B 6 20 00:48 internal -rwxr-xr-x 1 MAC staff 5.9k 6 20 00:48 kubectx-rwxr-xr-x 1 MAC staff 5.4K 6 20 00:48 kubens drwxr-xr-x 7 MAC staff 224B 6 20 00:48 test ➜ [/Users/ MAC /kubectx] git:(master) cp kubectx/kube* /usr/local/bin/➜ [/Users/ MAC /kubectx] git:(master) chmod +x /usr/local/bin/kube* ➜ [/Users/ MAC /kubectx] git:(master) mv /usr/local/bin/kubectx /usr/local/bin/kbx ➜ [/Users/mac/kubectx] git:(master) mv /usr/local/bin/kubens /usr/local/bin/kbsCopy the code

Note that the main reason for renaming the downloaded binary is to prevent the kubectx tool from affecting the automatic completion of Kubectl.

2 Use Kubectx to quickly switch context

Kube/to the ~ /. Kube/directory on the management machine. You can use this tool to quickly switch between different operation clusters. It can be said that from now on switching clusters can be as smooth as silk

2.1 Basic Usage

➜ [/Users/ MAC] KBX -h USAGE: kubectx: list the context Kubectx <NAME> : switch to context <NAME> kubectx - : switch to the previous context kubectx -c, --current : show the current context name kubectx <NEW_NAME>=<NAME> : rename context <NAME> to <NEW_NAME> kubectx <NEW_NAME>=. : rename current-context to <NEW_NAME> kubectx -d <NAME> [<NAME...>] : delete context <NAME> ('.' for current-context) (this command won't delete the user/cluster entry that is used by the context) kubectx -u, --unset : unset the current context kubectx -h,--help : show this messageCopy the code

Kubectx – : switch to the previous context kubectx <NEW NAME>= Kubectx =. : Give the current context a new name

### 2.2 List all contextCopy the code

➜ [/Users/ MAC] KBX Cluster1 Cluster2

### 2.3 Switch to the specified contextCopy the code

➜ [/Users/ MAC] KBX Cluster1 Switched to context “Cluster1 “.

View cluster-info KBX cluster-info Result Example Kubernetes master is running at https://10.0.2.15:6443 KubeDNS is running at https://10.0.2.15:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy To further debug and diagnose cluster problems, Use 'kubectl cluster-info dump'. ' '### 2.4' ➜ [/Users/ MAC] KBX cluster1 Switched to context ➜ [/Users/ MAC] KBX Cluster2 Switched to context cluster2". ➜ [/Users/ MAC] kbx-switched to context "Cluster1". ` ` ` 3 USES kubens quickly switch the namespace -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - # # # 3 \. 1 parameter specifies ` ` ` ➜ / Users/MAC KBS - h the USAGE: kubens : list the namespaces in the current context kubens <NAME> : change the active namespace of current context kubens - : switch to the previous namespace in this context kubens -c, --current : show the current namespace kubens -h,--help : Namespace ' '➜ [/Users/ MAC] KBS default kube-public kube-system' '### 3.3 Context "cluster1" modified. Active namespace is "kube-system". Context "cluster1" modified. Next, you can run the following command to quickly switch between the two namespaces that have been used recently. Kubens - '> pay special attention to ⚠️ It is convenient to manage multiple clusters on the same manager, but you need to pay attention to the security of administrators of multiple clusters. You can also refer to the official documents. Create a context with different permissions yourself https://blog.csdn.net/qq\_21816375/article/details/80305851?utm\_medium=distribute.pc\_relevant.none-task-blog-BlogComme ndFromMachineLearnPai2-1.nonecase&depth\_1-utm\_source=distribute.pc\_relevant.none-task-blog-BlogCommendFromMachineLear nPai2-1.nonecase https://kubernetes.io/docs/tasks/access-application-cluster/configure-access-multiple-clusters/#define-clusters-users-an D-context ** public account: o&M Development story ** **github: https://github.com/orgs/sunsharing-note/dashboard * * * * love life, love operations * * if you think the article is good, please click on the top right corner to send to a friend or forwarded to the circle of friends. Your support and encouragement is my biggest motivation. Like please pay attention to me ~! [images] (https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/5539fcef174d4d79a27255c925e96966~tplv-k3u1fbpfcp-zoom-1.image) If you like this article, please share it with your moments. For more information, please follow me. . > in this paper, using synchronous assistant] [article (https://juejin.cn/post/6940875049587097631)Copy the code