简体   繁体   English

如何使用 kubectl 部署到运行 microk8s 集群的 VM?

[英]How to deploy to a VM running a microk8s cluster with kubectl?

I have a Windows 10 machine running a Hyper-V virtual machine with Ubuntu guest.我有一台 Windows 10 机器,它运行带有 Ubuntu 来宾的 Hyper-V 虚拟机。 On Ubuntu, there is a Microk8s installation for my single node Kubernetes cluster.在 Ubuntu 上,我的单节点 Kubernetes 集群安装了 Microk8s。

I can't figure out how to set up my kubectl on the Win10 to allow deploying on the microk8s cluster in the vm.我不知道如何在 Win10 上设置我的 kubectl 以允许在 vm 中的 microk8s 集群上进行部署。

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 ). Atm,从外部,我可以 ssh 进入虚拟机,我可以到达 microk8s 的仪表板代理(在本例中为本地 https://ubuntuk8s:10443 )。

How to configure kubectl on my windows to deploy to microk8s inside the vm ?如何在我的 windows 上配置 kubectl以部署到vm 内的 microk8s

In short, you can copy the file from microk8s instance that kubectl is using ( kubeconfig ) to the Windows machine in order to connect to your microk8s instance.简而言之,您可以将kubectl正在使用的microk8s实例 ( kubeconfig ) 中的文件复制到 Windows 机器上,以便连接到您的microk8s实例。


As you already have full connectivity between the Windows machine and the Ubuntu that microk8s is configured on, you will need to:由于您已经在 Windows 机器和配置了 microk8s 的microk8s之间建立了完全连接,因此您需要:

  • Install kubectl on Windows machine在 Windows 机器上安装kubectl
  • Copy and modify the kubeconfig file on your Windows machine复制并修改 Windows 机器上的kubeconfig文件
  • Test the connection测试连接

Install kubectl安装kubectl

You can refer to the official documentation on that matter by following this link:您可以通过以下链接参考有关此问题的官方文档:


Copy and modify the kubeconfig file on your Windows machine复制并修改 Windows 机器上的kubeconfig文件

IMPORTANT!重要的!

After a bit of research, I've found easier way for this step.经过一番研究,我找到了更简单的方法来完成这一步。

You will need to run microk8s.config command on your microk8s host.您需要在microk8s主机上运行microk8s.config命令。 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 kubectl on Windows machine.您只需在 Windows 机器上将它与kubectl一起使用。

I've left below part to give one of the options on how to check where the config file is located我在下面部分给出了有关如何检查配置文件所在位置的选项之一

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 kubeconfig is located, copy it to the Windows machine and edit it to point to the IP address of your microk8s instance (not 127.0.0.1 ).这将取决于您使用的工具,但其背后的主要思想是登录到您的 Ubuntu 机器,检查kubeconfig的位置,将其复制到 Windows 机器并编辑它以指向您的microk8s实例地址不是127.0.0.1 )。

If you can SSH to the VM you can run following command to check where the config file is located (I would consider this more of a workaround solution):如果您可以将SSH连接到VM ,则可以运行以下命令来检查配置文件的位置(我认为这更像是一种解决方法):

  • microk8s kubectl get pods -v=6
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.

As you can see in this example, the file is in (it could be different in your setup):正如您在此示例中看到的,该文件位于(在您的设置中可能有所不同):

  • /var/snap/microk8s/2143/credentials/client.config

You'll need to copy this file to your Windows machine either via scp or other means.您需要通过scp或其他方式将此文件复制到 Windows 机器上。

After it's copied, you'll need to edit this file to change the field responsible for specifying where the kubectl should connect to:复制后,您需要编辑此文件以更改负责指定kubectl应连接到何处的字段:

  • before:前:
    • server: https://127.0.0.1:16443
  • after:后:
    • server: https://ubuntuk8s:16443

Test the connection测试连接

One of the ways to check if you can connect from your Windows machine to microk8s instance is following:检查是否可以从 Windows 机器连接到microk8s实例的方法之一如下:

PS C:\Users\XYZ\Desktop> 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    <none>   101m   v1.20.6-34+e4abae43f6acde   192.168.0.116   <none>        Ubuntu 20.04.2 LTS   5.4.0-65-generic   containerd://1.3.7

Where:在哪里:

  • --kubeconfig=client.config is specifying the file that you've downloaded --kubeconfig=client.config指定您下载的文件

Additional resources:其他资源:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM