Master Kubernetes Contexts and Switch Clusters Efficiently with kubectx
Learn how to define and manage multiple Kubernetes clusters using contexts, extract certificates, configure contexts for production and test environments, and boost switching efficiency with the kubectx tool, complete with step‑by‑step commands and practical tips.
What is a Kubernetes Context?
Kubernetes context determines which cluster, user identity, and default namespace you are operating on.
Configure Context for Production Cluster
Extract certificates from the admin.conf file and save them as ca.crt, tls.crt, and tls.key.
$ sudo cat /etc/kubernetes/admin.conf | awk '/certificate-authority-data/ {print $NF}' | base64 -d
$ sudo cat /etc/kubernetes/admin.conf | awk '/client-certificate-data/ {print $NF}' | base64 -d
$ sudo cat /etc/kubernetes/admin.conf | awk '/client-key-data/ {print $NF}' | base64 -dSet the cluster, user, and context:
# 1. Set cluster and CA certificate
kubectl config set-cluster produce --server https://172.139.20.100:6443 --certificate-authority ca.crt
# 2. Set user credentials (certificate authentication)
kubectl config set-credentials prod-admin --client-certificate tls.crt --client-key tls.key
# 3. Add context
kubectl config set-context prod-ctx --cluster produce --user prod-admin
Context "prod-ctx" created.Configure Context for Test Cluster
Similarly, extract certificates and configure the test cluster:
# 1. Set cluster and CA certificate
kubectl config set-cluster test --server https://172.139.20.96:6443 --certificate-authority ca.crt
# 2. Set user credentials
kubectl config set-credentials test-admin --client-certificate tls.crt --client-key tls.key
# 3. Add context
kubectl config set-context test-ctx --cluster test --user test-admin
Context "test-ctx" created.Verify Contexts
Switch to each context and list nodes:
# Production
kubectl config use-context prod-ctx
kubectl get nodes
# Test
kubectl config use-context test-ctx
kubectl get nodesBoost Switching Efficiency with kubectx
When many clusters exist, typing full context names is inefficient. Install kubectx:
$ wget https://github.com/ahmetb/kubectx/releases/download/v0.9.5/kubectx_v0.9.5_linux_x86_64.tar.gz
$ tar xvf kubectx_v0.9.5_linux_x86_64.tar.gz
$ sudo mv kubectx /usr/local/bin/List all contexts and switch quickly:
$ kubectx
prod-ctx
test-ctx # highlighted as current
$ kubectx prod-ctx
✔ Switched to context "prod-ctx"Conclusion
As the number of Kubernetes clusters grows, using the context mechanism together with tools like kubectx turns you from an operator into an engineer, making cluster management more elegant, efficient, and less error‑prone.
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.
Linux Ops Smart Journey
The operations journey never stops—pursuing excellence endlessly.
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.
