Cloud Native 6 min read

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.

Linux Ops Smart Journey
Linux Ops Smart Journey
Linux Ops Smart Journey
Master Kubernetes Contexts and Switch Clusters Efficiently with kubectx
Kubernetes context illustration
Kubernetes context illustration

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

.

<code>$ 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 -d
</code>

Set the cluster, user, and context:

<code># 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.
</code>

Configure Context for Test Cluster

Similarly, extract certificates and configure the test cluster:

<code># 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.
</code>

Verify Contexts

Switch to each context and list nodes:

<code># Production
kubectl config use-context prod-ctx
kubectl get nodes

# Test
kubectl config use-context test-ctx
kubectl get nodes
</code>

Boost Switching Efficiency with kubectx

When many clusters exist, typing full context names is inefficient. Install kubectx:

<code>$ 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/
</code>

List all contexts and switch quickly:

<code>$ kubectx
prod-ctx
test-ctx   # highlighted as current

$ kubectx prod-ctx
✔ Switched to context "prod-ctx"
</code>

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.

cloud-nativeKubernetesmulti-clusterContextkubectlkubectx
Linux Ops Smart Journey
Written by

Linux Ops Smart Journey

The operations journey never stops—pursuing excellence endlessly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.