• +43 660 1453541
  • contact@germaniumhq.com

Managing Microk8s and Regular Clusters by Merging Configs


Managing Microk8s and Regular Clusters by Merging Configs

After we install the snap of microk8s, we notice that the default .kube/config is not there, furthermore we need to use the microk8s.kubectl binary to access it. One option is aliasing the kubectl to microk8s.kubectl, but that has its own drawbacks, such as not being able to use it in scripts. Furthermore if we already have a .kube/config we’d want the configs to be merged. So how do we achieve that?

Let’s introduce kube-merge. Kube-merge is a small python program that merges multiple configurations into the current one.

To install it we’ll simply run the regular pip install:

pip install -U kube-merge

First we’d need to export the configuration from microk8s into a new file:

microk8s.kubectl config view --raw > microkube.yml

This basically dumps the config into a new file. The --raw flag is set, so the credentials are also dumped into the yml file. Then to merge it we just call:

kube-merge microkube.yml

From that moment we’re just going to use the regular Kubernetes config command to switch contexts:

kubectl config get-contexts
CURRENT   NAME                          CLUSTER            AUTHINFO           NAMESPACE
*         kubernetes-admin@kubernetes   kubernetes         kubernetes-admin
          microk8s                      microk8s-cluster   admin

Then to change the context, we’re simply switching the active context:

kubectl config use-context microk8s

That’s it! :)

PS: Kubernetes also allows changing the environment variables to point to multiple config files. I personally prefer to have a single config, instead of using that approach. Why? Because if for any reason I’m screwing up the environment variable, trying to figure out where my yaml files are, and to rebuild the environment variable becomes a nightmare.