Working with kubectl

MicroK8s comes with its own packaged version of the kubectl command for operating Kubernetes. By default, this is accessed through MicroK8s, to avoid interfering with any version which may already be on the host machine (including its configuration). It is run in a terminal like this:

microk8s kubectl

If you are using or want to use a different kubectl command, see the relevant sections below for your OS

Linux

If you’d prefer to use your host’s kubectl command, running the following command will output the kubeconfig file from MicroK8s.

microk8s config

If you have not already configured kubectl on the host, you can just open a terminal and generate the required config :

cd $HOME
mkdir .kube
cd .kube
microk8s config > config

If you have already configured other Kubernetes clusters, you should merge the output from the microk8s config with the existing config (copy the output, omitting the first two lines, and paste it onto the end of the existing config using a text editor).

Windows

MicroK8s comes with its own kubectl command, which can be accessed like this:

microk8s kubectl

There are some advantages to running the Windows native version of kubectl, notably when working with files(which otherwise need to be copied to/from the VM). To install the Windows version of kubectl, see the official documentation. Running the following command will output the kubeconfig file from MicroK8s:

microk8s config

If you have not already configured kubectl on the host for other clusters, you can open a ‘cmd’ Command Prompt and run the following:

cd %USERPROFILE%
mkdir .kube
cd .kube
microk8s config > config

If you have already configured other Kubernetes clusters, you should merge the output from the microk8s config with the existing config (copy the output, omitting the first two lines, and paste it onto the end of the existing config using a text editor).

macOS

MicroK8s comes with its own kubectl command, which can be accessed like this:

microk8s kubectl

There are some advantages to running the native version of kubectl for macOS, notably when working with files (which otherwise need to be copied to/from the VM). To install the macOS version of kubectl, see the official documentation. Running the following command will output the kubeconfig file from MicroK8s:

microk8s config

If you have not already configured kubectl on the host, you can just open a terminal and generate the required config :

cd $HOME
mkdir .kube
cd .kube
microk8s config > config

If you have already configured other Kubernetes clusters, you should merge the output from the microk8s config with the existing config (copy the output, omitting the first two lines, and paste it onto the end of the existing config using a text editor).

If you have already configured other Kubernetes clusters, you should merge the output from the microk8s config with the existing config (copy the output, omitting the first two lines, and paste it onto the end of the existing config using a text editor).

just skipping the first 2 lines is not working when merging 2 kube config files,

Need kubectl to flatten it, eg.)
$ KUBECONFIG=~/.kube/config1:~/configs/config2.yaml kubectl config view --flatten > ~/.kube/config

thanks for the tip @sbellan! We’ll check this out and add something appropriate

I have created this script microk8s.refreshconfig:

#!/bin/sh
set -ex
microk8s.config > /tmp/microk8s.yaml
KUBECONFIG=/tmp/microk8s.yaml:~/.kube/config kubectl config view --flatten > /tmp/kubeconfig.yaml
mv /tmp/kubeconfig.yaml ~/.kube/config
chmod go-rwx ~/.kube/config

Seems like the sort of thing that should be available as, say, microk8s.config --in-place / -i or --update / -u?

Can we please update this for a ha setup?

I am struggling to setup my local machines kubectl with multiple nodes (local machine kubectl stops working when the one node whos IP is in the kubectl config is down)…