# Working with kubectl

**URL:** <https://discuss.kubernetes.io/t/working-with-kubectl/11258>\
**Category:** microk8s\
**Tags:** docs\
**Created:** [June 4, 2020, 3:06pm UTC](https://discuss.kubernetes.io/t/working-with-kubectl/11258 "2020-06-04T15:06:31Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![evilnick](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.kubernetes.io/evilnick/32/3481_2.png) [@evilnick](https://discuss.kubernetes.io/u/evilnick)\
**Post date:** [June 4, 2020, 3:06pm UTC](https://discuss.kubernetes.io/t/working-with-kubectl/11258/1 "2020-06-04T15:06:31Z")

</div>

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:

```auto
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](https://kubernetes.io/docs/reference/kubectl/overview/) command, running the following command will output the kubeconfig file from MicroK8s.

```nohighlight
microk8s config

```

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

```auto
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](https://kubernetes.io/docs/reference/kubectl/overview/) command, which can be accessed like this:

```auto
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](https://kubernetes.io/docs/tasks/tools/install-kubectl/#install-kubectl-on-windows). Running the following command will output the kubeconfig file from MicroK8s:

```auto
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:

```auto
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](https://kubernetes.io/docs/reference/kubectl/overview/) command, which can be accessed like this:

```auto
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](https://kubernetes.io/docs/tasks/tools/install-kubectl/#install-kubectl-on-macos). Running the following command will output the kubeconfig file from MicroK8s:

```nohighlight
microk8s config

```

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

```auto
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).

---

<div class="post-metadata">

**Author:** ![sbellan](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@sbellan](https://discuss.kubernetes.io/u/sbellan)\
**Post date:** [September 12, 2020, 8:53am UTC](https://discuss.kubernetes.io/t/working-with-kubectl/11258/2 "2020-09-12T08:53:36Z")

</div>

> 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

---

<div class="post-metadata">

**Author:** ![evilnick](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.kubernetes.io/evilnick/32/3481_2.png) [@evilnick](https://discuss.kubernetes.io/u/evilnick)\
**Post date:** [September 16, 2020, 10:25am UTC](https://discuss.kubernetes.io/t/working-with-kubectl/11258/3 "2020-09-16T10:25:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jglick](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.kubernetes.io/jglick/32/8284_2.png) [@jglick](https://discuss.kubernetes.io/u/jglick)\
**Post date:** [August 2, 2021, 7:50pm UTC](https://discuss.kubernetes.io/t/working-with-kubectl/11258/4 "2021-08-02T19:50:34Z")

</div>

I have created this script `microk8s.refreshconfig`:

```bash
#!/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`?

---

<div class="post-metadata">

**Author:** ![NebojsaKrtolica](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.kubernetes.io/nebojsakrtolica/32/14479_2.png) [@NebojsaKrtolica](https://discuss.kubernetes.io/u/NebojsaKrtolica)\
**Post date:** [March 8, 2024, 4:34pm UTC](https://discuss.kubernetes.io/t/working-with-kubectl/11258/5 "2024-03-08T16:34:56Z")

</div>

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)…

---

<div class="post-metadata">

**Author:** ![Rene\_Radoi](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.kubernetes.io/rene_radoi/32/13067_2.png) [@Rene\_Radoi](https://discuss.kubernetes.io/u/Rene_Radoi)\
**Post date:** [April 23, 2024, 9:05am UTC](https://discuss.kubernetes.io/t/working-with-kubectl/11258/6 "2024-04-23T09:05:44Z")

</div>

> [@evilnick](#):
>
> If you have not already configured kubectl on the host, you can just open a terminal and generate the required config :
> 
> ```auto
> cd $HOME
> mkdir .kube
> cd .kube
> microk8s config > config
> 
> ```

Maybe it’s better to use `microk8s.kubectl config view --raw` here instead of `microk8s config` as it omits the newline at the end.
