Merge Kube Config in PowerShell

Merging Kubernetes Config files is a rather cumbersome process. You can manually copy in the values but sometimes that doesn’t always go as planned. I was looking for a way to merge Kube Config files when I ran across a great post that wasn’t geared for PowerShell users.

Key Takeaways

  • Must use full path. Relative paths did not work.
  • $ENV:KUBECONFIG was an empty value before I set it.
  • –flatten – is used to flatten the kubeconfig file into a self-contained output.

For this demo, my newly created Kubernetes Config file will be called config-devbox.

I have to thank Jacob Tomlinson for his post on Medium “How to merge Kubernetes kubectl config files”, however, his post was not based on Windows PowerShell users. If you’re looking for the Bash equivalent I suggest you visit his post!


# make a backup
cd ~/.kube/
cp config config.bak

# merge both kube config files
$ENV:KUBECONFIG = "C:\Users\mrjamiebowman\.kube\config;C:\Users\mrjamiebowman\.kube\config-devbox"

# verify that the variable is set

# output to temp file
kubectl config view --flatten > config-merged

# verify that config-merged is correct
kubectl --kubeconfig=config-merged config get-clusters

# delete backup
rm config

# move merged file to config
mv config-merged config

# remove (optional)
rm config.bak

Clean Up

To remove old clusters by name see the commands below.

# list all clusters
kubectl config get-clusters

# delete cluster by name
kubectl config delete-cluster minikube

Further Reading