<p>I have a Windows 10 machine running a Hyper-V virtual machine with Ubuntu guest.
On Ubuntu, there is a Microk8s installation for my single node Kubernetes cluster.</p>
<p>I can't figure out how to set up my kubectl on the Win10 to allow deploying on the microk8s cluster in the vm.</p>
<p>Atm, from outside, I can ssh into the vm and I can reach the dashboard-proxy for microk8s (in this case locally https://ubuntuk8s:10443 ).</p>
<p>How to configure <strong>kubectl on my windows</strong> to deploy to <strong>microk8s inside the vm</strong>?</p>

babbyldockr
<p>In short, you can copy the file from <code>microk8s</code> instance that <code>kubectl</code> is using (<code>kubeconfig</code>) to the Windows machine in order to connect to your <code>microk8s</code> instance.</p>
<hr />
<p>As you already have full connectivity between the Windows machine and the Ubuntu that <code>microk8s</code> is configured on, you will need to:</p>
<ul>
<li>Install <code>kubectl</code> on Windows machine</li>
<li>Copy and modify the <code>kubeconfig</code> file on your Windows machine</li>
<li>Test the connection</li>
</ul>
<hr />
<h3>Install <code>kubectl</code></h3>
<p>You can refer to the official documentation on that matter by following this link:</p>
<ul>
<li><em><a href="https://kubernetes.io/docs/tasks/tools/install-kubectl-windows/" rel="nofollow noreferrer">Kubernetes.io: Docs: Tasks: Tools: Install kubectl Windows</a></em></li>
</ul>
<hr />
<h3>Copy and modify the <code>kubeconfig</code> file on your Windows machine</h3>
<blockquote>
<p><strong>IMPORTANT!</strong></p>
<p>After a bit of research, I've found easier way for this step.</p>
<p>You will need to run <strong><code>microk8s.config</code></strong> command on your <code>microk8s</code> host. It will  give you the necessary config to connect to your instance and you won't need to edit any fields. You will just need to use it with <code>kubectl</code> on Windows machine.</p>
<p>I've left below part to give one of the options on how to check where the config file is located</p>
</blockquote>
<p>This will depend on the tools you are using but the main idea behind it is to login into your Ubuntu machine, check where the <code>kubeconfig</code> is located, copy it to the Windows machine and edit it to point to the IP address of your <code>microk8s</code> instance (not <code>127.0.0.1</code>).</p>
<p>If you can <code>SSH</code> to the <code>VM</code> you can run following command to check where the config file is located (I would consider this more of a workaround solution):</p>
<ul>
<li><code>microk8s kubectl get pods -v=6</code></li>
</ul>
<pre><code>I0506 09:10:14.285917  104183 loader.go:379] Config loaded from file:  /var/snap/microk8s/2143/credentials/client.config
I0506 09:10:14.294968  104183 round_trippers.go:445] GET https://127.0.0.1:16443/api/v1/namespaces/default/pods?limit=500 200 OK in 5 milliseconds
No resources found in default namespace.
</code></pre>
<p>As you can see in this example, the file is in (it could be different in your setup):</p>
<ul>
<li><code>/var/snap/microk8s/2143/credentials/client.config</code></li>
</ul>
<p>You'll need to copy this file to your Windows machine either via <code>scp</code> or other means.</p>
<p>After it's copied, you'll need to edit this file to change the field responsible for specifying where the <code>kubectl</code> should connect to:</p>
<ul>
<li>before:
<ul>
<li><code>    server: https://127.0.0.1:16443</code></li>
</ul>
</li>
<li>after:
<ul>
<li><code>    server: https://ubuntuk8s:16443</code></li>
</ul>
</li>
</ul>
<hr />
<h3>Test the connection</h3>
<p>One of the ways to check if you can connect from your Windows machine to <code>microk8s</code> instance is following:</p>
<pre><code>PS C:\Users\XYZ\Desktop&gt; kubectl --kubeconfig=client.config get nodes -o wide
NAME     STATUS   ROLES    AGE    VERSION                     INTERNAL-IP     EXTERNAL-IP   OS-IMAGE             KERNEL-VERSION     CONTAINER-RUNTIME
ubuntu   Ready    &lt;none&gt;   101m   v1.20.6-34+e4abae43f6acde   192.168.0.116   &lt;none&gt;        Ubuntu 20.04.2 LTS   5.4.0-65-generic   containerd://1.3.7
</code></pre>
<p>Where:</p>
<ul>
<li><code>--kubeconfig=client.config</code> is specifying the file that you've downloaded</li>
</ul>
<hr />
<p>Additional resources:</p>
<ul>
<li><em><a href="https://kubernetes.io/docs/concepts/configuration/organize-cluster-access-kubeconfig/" rel="nofollow noreferrer">Kubernetes.io: Docs: Concepts: Configuration: Organize cluster access kubeconfig</a></em></li>
<li><em><a href="https://github.com/ubuntu/microk8s/issues/421#issuecomment-481248192" rel="nofollow noreferrer">Github.com: Ubuntu: Microk8s: Issues: Cannot connect to microk8s from another machine</a></em> - this link pointed me to <code>microk8s.config</code></li>
</ul>
