Blog Post

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:usersandrew.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 ~.kubeconfig ~.kubeconfig_backup

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

Copy-Item C:Tempconfig ~.kubeconfig2

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

$env:KUBECONFIG="C:usersandrew.pruski.kubeconfig;C:usersandrew.pruski.kubeconfig2"

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 > ~.kubeconfig_tmp

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

kubectl config get-clusters --kubeconfig=C:usersandrew.pruski.kubeconfig_tmp

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

Remove-Item ~.kubeconfig
Move-Item ~.kubeconfig_tmp ~.kubeconfig

Finally, confirm it’s working: –

kubectl config get-clusters

Thanks for reading!

Original post (opens in new tab)
View comments in original post (opens in new tab)

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating