Digital Ocean K8S: Merging Config files

I just got access to Digital Ocean Kubernetes service (still on a limited offer) so my minions there need to get busy!

a 5$ for a k8s cluster is the cheapest price I have seen. The first problem I run into was how to smoothly run kubectl commands and also easily move from one k8s context to another.

DigitalOcean documentation suggest to pass argument to the config file on each command

So instead of

kubectl get nodes

you will have to do

kubectl --kubeconfig="cluster1-kubeconfig-dupe.yaml" get nodes

So why is that?

If you used GKE or AKS, they will let you use a cli command to set the cluster and context into your local Kubectl client config (stored at $HOME/.kube/config). Other providers might not have a nice tool that does that so they might give you a config in form of a download-able yaml instead such as DigitalOcean.

Solution

As per kubectl Cheat Sheet, you can run the following command to sort of merge the configurations of many files in different locations in your system. All it does is to save an environment variable has a list of paths to these config files:

  • For Linux:

export KUBECONFIG=~/.kube/config:~/path/to/your/downloaded-config-file.yaml

  • On windows:

$env:KUBECONFIG ="$HOME\.kube\config; path\to\your\downloaded-config-file.yaml "

Notice that for windows we separate paths using semicolons

Now when you run kubectl config view you will see a merged view for all your config files. So, you can run kubectl config use-context on any of the listed contexts.

Now if you open another shell, you will lose your changes. On windows PowerShell, in order to persist the change you made on windows, you need to store the file permanently to your system environment variables:

[Environment]::SetEnvironmentVariable("KUBECONFIG", "$HOME\.kube\config;path\to\your\downloaded-config-file.yaml", "Machine")

Please note that frequently these tokens get expired so you will need to download the config file from DO again and save it in the same name and location.