Use kubectl with microk8s



I’ve been switching my dev environment from minikube to microk8s and have just set up my kubectl to work with microk8s without any fuss or conflicts. I’ve noticed some advice to overwrite existing kubectl config with microk8s, but this is a non-starter if you need to retain access to existing kubernetes clusters.

Fortunately integrating microk8s with an existing kubectl is super easy

# add the minik8s cluster to kubectl
kubectl config set-cluster microk8s-cluster --server= --insecure-skip-tls-verify

# create the microk8s context
kubectl config set-context microk8s --user=admin --cluster=microk8s-cluster

# switch to the microk8s context
kubectl config use-context microk8s


Hi @gerbal,

Thank you for this post. It looks great.

Note that the insecure port 8080 is marked for deprecation from the upstream Kubernetes project, I do not know how long it will be around. Starting from the v1.14 release MicroK8s will start using the secure port 16443 while keeping the insecure port open for local use to maintain backwards compatibility.

Have you looked at the option of merging the kubeconfig files as described in Organizing Cluster Access Using kubeconfig Files - Kubernetes ?



I did not know about the kubeconfig envvar.

I’ve simplified my config by adding this to my .bashrc

microk8s.kubectl config view --raw > $HOME/.kube/microk8s.config
export  KUBECONFIG=$HOME/.kube/config
export  KUBECONFIG=$KUBECONFIG:$HOME/.kube/microk8s.config

which, IMO, is safer than the advice in the project readme