Managing Multiple Clusters and Namespaces with kubectx and kubens

Hemanth M Gowda
4 min readMar 12, 2024

Are you managing multiple Kubernetes clusters and finding it challenging to switch between them efficiently? In today’s fast-paced world of container orchestration, streamlining your workflow is crucial. Thankfully, tools like kubectx and kubens offer powerful features to simplify cluster management and boost productivity.

Kubectx: Simplifying Cluster Navigation Kubectx simplifies the process of switching between Kubernetes clusters. Instead of recalling and typing complex commands, you can utilize the kubectx command followed by the cluster name. For instance, ‘kubectx my-cluster’. This approach saves time and mitigates the risk of inadvertently executing commands on the incorrect cluster.

$ kubectx <cluster_name>
Switched to context <cluster_name>

Kubens: Streamlined Namespace Navigation Kubens facilitates seamless switching between namespaces. Like Kubectx, you can employ the kubens command followed by the namespace name. For example: ‘kubens my-namespace’. This functionality proves especially useful when managing multiple applications or teams within the same cluster.

$ kubens <namespace_name>
Switched to namespace <namespace_name>

We’ll begin by installing kubectl, then proceed to configure settings aimed at enhancing efficiency when managing Kubernetes clusters from the command line interface using kubectl.

kubectl: The Kubernetes command-line tool, kubectl, allows you to run commands against Kubernetes clusters. You can use kubectl to deploy applications, inspect and manage cluster resources, and view logs.

kubectl is installable on a variety of Linux platforms, macOS and Windows. Find your preferred operating system below.

The kubectl tool searches for a configuration file named ‘config’ in the $HOME/.kube directory, though an alternative file can be designated using the — kubeconfig option. Kubeconfig files enable efficient organization of details regarding clusters, users, namespaces, and authentication methods.

$ cat $HOME/.kube/config

In the config file, the following components must be configured:

Clusters: Configuring access to a cluster requires knowledge of its location and the necessary credentials. Within the cluster section, you’ll define the certificate-authority-data, access URL, and the cluster’s name.
Context: A context element serves to organize access parameters under a user-friendly name. Each context within the configuration file should include three parameters: cluster, namespace, and user.
Users: Specify the user and its associated credentials for accessing the cluster.

Configuration for multiple clusters

Here’s a template configuration file for managing access to four Kubernetes clusters:

  1. Cluster: dev-k8s
    Context: dev-k8s
    User: dev-k8s-admin
  2. Cluster: staging-k8s
    Context: staging-k8s
    User: staging-k8s-admin
  3. Cluster: qa-k8s
    Context: qa-k8s
    User: qa-k8s-admin
  4. Cluster: prod-k8s
    Context: prod-k8s
    User: prod-k8s-admin

Customize the template located at $HOME/.kube/config to have access to all four clusters, please find the template below:

apiVersion: v1
kind: Config
preferences: {}

clusters:
- cluster:
certificate-authority-data:
server:
name: dev-k8s

- cluster:
certificate-authority-data:
server:
name: staging-k8s

- cluster:
certificate-authority-data:
server:
name: qa-k8s

- cluster:
certificate-authority-data:
server:
name: prod-k8s

contexts:
- context:
cluster: dev-k8s
user: dev-k8s-admin
name: dev-k8s

- context:
cluster: staging-k8s
user: staging-k8s-admin
name: staging-k8s

- context:
cluster: qa-k8s
user: qa-k8s-admin
name: qa-k8s

- context:
cluster: prod-k8s
user: prod-k8s-admin
name: prod-k8s

users:
- name: dev-k8s-admin
user:
password:
username:

- name: staging-k8s-admin
user:
client-certificate-data:
client-key-data:

- name: qa-k8s-admin
user:
client-certificate-data:
client-key-data:

- name: prod-k8s-admin
user:
client-certificate-data:
client-key-data:

Install kubectx and kubens

Viewing Clusters and Namespaces Both Kubectx and Kubens offer commands to display the available clusters and namespaces, respectively. For example, using ‘kubectx’ will list all clusters, while ‘kubens’ will enumerate the namespaces within the current cluster.

# List all the contexts
$ kubectx
dev-k8s
staging-k8s
qa-k8s
prod-k8s

# Switching to dev-k8s context
$ kubectx dev-k8s

# Get all namespaces in dev-k8s context
$ kubens
default
namespace-1
namespace-2

# Switch to a namespace
$ kubens namespace-1

# After setting the context and namespace
# below command lists all the pods in namespace-1 of dev-k8s
# no need to explicitly mention the context and namespace
$ kubectl get pods
$ kubectl logs my-pod
$ kubectl apply -f deployment.yaml

# To fetch the current context and namespace
$ kubectx --current
$ kubens --current

# For other options
$ kubectx --help
$ kubens --help

Conclusion: Kubectx and Kubens streamline the management of multiple clusters and namespaces, leading to enhanced productivity. These tools facilitate quicker context switching, reducing the reliance on manual command adjustments.

--

--