DEV Community

Cover image for Merge multiple kubeconfig files
Kyriakos Akriotis
Kyriakos Akriotis

Posted on

Merge multiple kubeconfig files

We use kubeconfig files to organize information about clusters, users, namespaces, and authentication mechanisms. kubectl command-line tool itself, uses kubeconfig files to source the information it needs in order to connect and communicate with the API server of a cluster.

By default, kubectl requires a file named config that lives under $HOME/.kube directory. You can multiple cluster entries in that file or specify additional kubeconfig files by setting the KUBECONFIG environment variable or by setting the --kubeconfig flag.

When you have multiple kubeconfig files and you want to merge them in one without using multiple files and switching among them with the kubeconfig flag, I personally get a headache because there is no way I remember how to merge those files from the command line. But there is a simpler way so please don’t waste brain cells to remember complicated bash commands.

As we mentioned before the easy way is by setting the KUBECONFIG environment variable. You can specify there multiple config files divided by the colon symbol (:) and kubectl will merge those automatically for you.

export KUBECONFIG=~/.kube/config:~/.rancher/local:~/.kube/kubeconfig.json
Enter fullscreen mode Exit fullscreen mode

Special treat: You can mix and match YAML and JSON files!

If you want to see now the current merged configuration that your kubectl is working with, just issue the command:

kubectl config view
Enter fullscreen mode Exit fullscreen mode

and if you want to export this configuration for future use as a single file you can do it by running:

kubectl config view --flatten > my-config.yaml
Enter fullscreen mode Exit fullscreen mode

and then you can replace your ~/.kube/config file with the file above for permanent effect.

Don’t over-complicate staff, don’t try to memorize staff.

Photo by Growtika Developer Marketing Agency on Unsplash

Top comments (0)