DEV Community

Cover image for How I manage multiple kubernetes clusters w/ kubectx & kubens
Furkan Kalkan
Furkan Kalkan

Posted on

How I manage multiple kubernetes clusters w/ kubectx & kubens

Kubernetes is a powerful platform for automating the deployment and scaling of applications. It has become the industry standard for managing server clusters. Managing a single Kubernetes cluster is pretty straightforward. You put your cluster configuration in a directory (usually ~/.kube) and start using kubectl. But if you have to manage multiple kubernetes clusters for reason it can be quite mess to track them.

I used to switch between clusters by renaming config directories or modifying the KUBECONFIG environment variable, but I realized that it's not sustainable.

I recently saw kubectx project on Github and I decide to use it.

Step 1: Merging kubernetes configs

I merged my kubernetes configs like below and put it on ~/.kube directory.

apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: <certificate authority data>
    server: https://<cluster1-ip>:6443
  name: my-cluster-1
- cluster:
    certificate-authority-data: <certificate authority data>
    server: https://<cluster2-ip>:6443
  name: my-cluster-2
contexts:
- context:
    cluster: my-cluster-1
    namespace: <default-namespace-name>
    user: my-user-1
  name: cluster-one
- context:
    cluster: my-cluster-2
    namespace: <default-namespace-name>
    user: my-user-2
  name: cluster-two
current-context: cluster-one
kind: Config
preferences: {}
users:
- name: my-user-1
  user:
    <add auth config here>
- name: my-user-2
  user:
    <add auth config here>
Enter fullscreen mode Exit fullscreen mode

Step 2: Installing kubectx & kubens

Installing kubectx & kubens tools are easy.

You can install with sudo apt install kubectx in Debian/Ubuntu or brew install kubectx in MacOS. More install methods described on README file. I also installed fzf package for easy navigation using arrow keys.

It is possible to switch between clusters using kubectx and between namespaces using kubens commands. Once namespace selected, you don't have to use -n option to define namespace kubectl, all bare kubectl commands applied on selected namespace.

Step 3: Setting the powerline to tracking clusters and namespaces

I love powerline over traditional PS1 (aka command prompt). It's beautiful and highly customizable.

You can refer the powerline documentation for install and configure powerline itself.

I use powerline-k8s plugin to see currently active kubernetes cluster and namespace in prompt.

You can install it using pip:
pip install powerline-k8s

After install I add this lines to bottom of groups section ~/.config/powerline/colorschemes/default.json

...
        "k8s":           { "fg": "solarized:blue", "bg": "solarized:base2", "attrs": [] },
        "k8s_namespace": { "fg": "solarized:red",  "bg": "solarized:base2", "attrs": [] },
        "k8s_context":   { "fg": "solarized:blue", "bg": "solarized:base2", "attrs": [] },
        "k8s:divider":   { "fg": "gray4",          "bg": "solarized:base2", "attrs": [] }
...
Enter fullscreen mode Exit fullscreen mode

Final result:

Powerline with k8s plugin

You may want to use https://github.com/jonmosco/kube-ps1 for more lightweight alternative.

Top comments (0)