Windows下DockerCE启用K8s报错kubernetes.docker.internal no such host

踩坑

在安装 Windows CE 之后,我尝试从设置菜单中启用Kubernetes,却一直处于 starting 状态不见好,查找位于 C:\ProgramData\DockerDesktop\service.txt 目录下的日志文件后,发现大片大片类似的错误日志:

根据错误信息

dial tcp: lookup kubernetes.docker.internal: no such host

可知,错误的原因是无法解析到域名 kubernetes.docker.internal,随即开始寻找解决思路。

寻求解决

一(失败)

首先我在 DockerCE for Windows 的 issue 看到JoseThen提出了如下解决方案

I was able to get this working even after restarting docker::

  1. Change DNS to fixed and use 8.8.8.8, this is within docker for window’s settings
  2. Remove the .kube
  3. Add the KUBECONFIG environment variable to System Variables and have the path be C:\Users[MYUSER].kube\config. Note that before I had it set as a User Variable.
  4. Restart Docker from the Docker for Window’s reset tab in settings.
  5. Restart Kubernetes Cluster from the Docker for Window’s reset tab in settings (you can do this a number of times).
  6. Afterwards just wait for some time and Kubernetes is running should display.
    I hope this can help someone, this issue is a pain.

查看之后发现 kube 的配置文件确实有配置这个路径
clusters[0]>server:https://kubernetes.docker.internal:6443 ,按照 JoseThen 的方法删除配置文件后重启,却并未成功解决,重新生成的文件中对应节点的值依然还是之前的地址,继续寻找解决思路。

之后我在 DockerCE for Mac 的 issue 看到了解决方案,原来 kubernetes.docker.internal 应该指向的就是本机,将

1
2
# Kubernetes
127.0.0.1 kubernetes.docker.internal

加入hosts之后即成功解决。

效果