Master Fast Kubernetes Cluster Switching with kubectx and kubens
Learn how to merge multiple kubeconfig files, resolve TLS user conflicts, and use the third‑party tools kubectx and kubens to quickly list, switch, and manage Kubernetes clusters and namespaces with simple command‑line shortcuts.
When operating multiple Kubernetes clusters with kubectl, manually renaming config files becomes cumbersome, especially if you frequently switch between environments such as a local Minikube cluster and production clusters.
Kubernetes solves this with contexts in the kubeconfig file, allowing you to define multiple clusters, users, and contexts in a single file.
A common pitfall occurs when two clusters share the same user name and TLS credentials; kubectl may merge the client-certificate-data and client-key-data fields, causing one cluster to become unreachable. The fix is to rename the user field in each context and update the corresponding entry under users so the names match.
apiVersion: v1
kind: Config
preferences: {}
clusters:
- cluster:
server: https://xxxxx:6443
certificate-authority-data: xxxxx
name: cluster1 # first cluster name
- cluster:
server: https://xxxxx:6443
certificate-authority-data: xxxxx
name: cluster2 # second cluster name
contexts:
- context:
cluster: cluster1
user: user1
name: cluster1
- context:
cluster: cluster2
user: user2
name: cluster2
users:
- name: user1
user:
client-certificate-data: xxxxxxxxx
client-key-data: xxxxxxxxxxxxx
- name: user2
user:
client-certificate-data: xxxxxxxxx
client-key-data: xxxxxxxxxxxxxAs long as the clusters , contexts , and users entries have unique names, you can add any number of them.
To switch contexts more conveniently, the open‑source tool kubectx (and its companion kubens for namespaces) can be used.
1. Installation
Clone the repository and copy the binaries to a directory in your PATH. The binaries are renamed to avoid interfering with kubectl auto‑completion.
git clone https://github.com/ahmetb/kubectx
cd kubectx
cp kubectx/kube* /usr/local/bin/
chmod +x /usr/local/bin/kube*
mv /usr/local/bin/kubectx /usr/local/bin/kbx
mv /usr/local/bin/kubens /usr/local/bin/kbsRenaming prevents the tool from overriding kubectl 's completion scripts.
2. Using kubectx to switch contexts quickly
2.1 Basic usage
kbx -h
USAGE:
kubectx : list the contexts
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> [...] : delete context <NAME>
kubectx -u, --unset : unset the current context
kubectx -h,--help : show this message2.2 List all contexts
kbx
cluster1
cluster22.3 Switch to a specific context
kbx cluster1
Switched to context "cluster1".2.4 Switch back to the previous context
kbx -
Switched to context "cluster1".3. Using kubens to switch namespaces
3.1 Basic usage
kbs -h
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 : show this message3.2 List all namespaces
kbs
default
kube-public
kube-system3.3 Switch to a specific namespace
kbs kube-system
Context "cluster1" modified.
Active namespace is "kube-system".⚠️ Managing multiple clusters on the same machine is convenient, but ensure each cluster has distinct permissions and contexts to avoid security issues.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Ops Development Stories
Maintained by a like‑minded team, covering both operations and development. Topics span Linux ops, DevOps toolchain, Kubernetes containerization, monitoring, log collection, network security, and Python or Go development. Team members: Qiao Ke, wanger, Dong Ge, Su Xin, Hua Zai, Zheng Ge, Teacher Xia.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
