Merge kubectl config files on Windows

When working with multiple Kubernetes clusters, at some point you’ll want to merge your kubectl config files.

I’ve seen a few blogs on how to merge kubectl config files but haven’t seen any on how to do it on Windows. It’s pretty much the same process, just adapted for powershell on Windows.

In this example, I’ll merge a new config file in C:\Temp to my existing config file in C:\users\andrew.pruski\.kube

N.B.- If you’re working with AKS, az aks get-credentials will do this for you

Firstly, backup the existing config file:-

cp ~\.kube\config ~\.kube\config_backup

Copy the new config file into the .kube directory: –

Copy-Item C:\Temp\config ~\.kube\config2

Set the KUBECONFIG environment variable to point at both config files:-

$env:KUBECONFIG="C:\users\andrew.pruski\.kube\config;C:\users\andrew.pruski\.kube\config2"

N.B. – Note that I’m using the absolute paths here, ~\.kube will not work

Export the output of the config view command (which references both config files) to a config_tmp file: –

kubectl config view  --raw > ~\.kube\config_tmp

Check all is working as expected (all clusters can be seen):-

kubectl config get-clusters --kubeconfig=C:\users\andrew.pruski\.kube\config_tmp

If all is working as expected, replace the old config file with the config_tmp file: –

Remove-Item ~\.kube\config
Move-Item ~\.kube\config_tmp ~\.kube\config

Finally, confirm it’s working: –

kubectl config get-clusters

Thanks for reading!