处理 "Unable to connect to the server dial tcp 127.0.0.1:52186 connectex No connection could be made because the target machine actively refused it." 的错误

在笔记本电脑的 Windows 10 中”启用”了Docker Desktop 自带的 Kubernetes 成功后,却发现执行任何 kubectl 命令都会得到如下的错误: “Unable to connect to the server: dial tcp 127.0.0.1:52186: connectex: No connection could be made because the target machine actively refused it.”。

比如:执行

1
kubectl ge node

系统如下的错误信息

1
Unable to connect to the server: dial tcp 127.0.0.1:52186: connectex: No connection could be made because the target machine actively refused it.

一开始有点懵,觉得整个安装步骤都没有报错,说明安装是成功的。仔细想了一下才想起来我原来在这台机器上安装过 minikube , 虽然后来删除了,但可能只是将程序删除了,应该是还残留量一些配置信息,导致了上面的错误。

根据这个思路,查看用户目录中 .kube 目录里面的 config 文件。发现内容如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
apiVersion: v1
clusters:
- cluster:
certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUMvakNDQWVhZ0F3SUJBZ0lCQURBTkJna3Foa2lHOXcwQkFRc0ZBREFWTVJNd0VRWURWUVFERXdwcmRXSmwKY201bGRHVnpNQjRY......
server: https://kubernetes.docker.internal:6443
name: docker-desktop
- cluster:
certificate-authority: C:\Users\xxx\.minikube\ca.crt
extensions:
- extension:
last-update: Sat, 09 Apr 2022 12:52:16 CST
provider: minikube.sigs.k8s.io
version: v1.24.0
name: cluster_info
server: https://127.0.0.1:52186
name: minikube
contexts:
- context:
cluster: docker-desktop
user: docker-desktop
name: docker-desktop
- context:
cluster: minikube
extensions:
- extension:
last-update: Sat, 09 Apr 2022 12:52:16 CST
provider: minikube.sigs.k8s.io
version: v1.24.0
name: context_info
namespace: default
user: minikube
name: minikube
current-context: minikube
kind: Config
preferences: {}
users:
- name: docker-desktop
user:
client-certificate-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSURRakNDQWlxZ0F3SUJBZ0lJYmh1QzZIV......
client-key-data: LS0tLS1CRUdJTiBSU0EgUFJJVkFURSBLRVktLS0tLQpNSUlFcEFJQkFBS0NBUUVBMmYrYWxq......
- name: minikube
user:
client-certificate: C:\Users\xxx\.minikube\profiles\minikube\client.crt
client-key: C:\Users\xxx\.minikube\profiles\minikube\client.key

可以果然是多了一个 Context, 是前一次安装、使用 minikube 时,由 minikube 安装程序建立的 Cluster 和 context 信息, 该 context 的名字被设置为 minikube。

而且配置文件中的 current-context 也被设置为 minikube.

由此推断,简单的删除 minikube 相关的配置信息并修改 current-context 的值就应该可以解决问题。

动手修改 config 文件,最后的内容如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
apiVersion: v1
clusters:
- cluster:
certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUMvakNDQWVhZ0F3SUJBZ0lCQURBTkJna3Foa2lHOXc......
server: https://kubernetes.docker.internal:6443
name: docker-desktop
contexts:
- context:
cluster: docker-desktop
user: docker-desktop
name: docker-desktop
current-context: docker-desktop
kind: Config
preferences: {}
users:
- name: docker-desktop
user:
client-certificate-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUMvakNDQWVhZ0F3SUJBZ0lCQURBTkJna3Foa2lHOXc.....
client-key-data: LS0tLS1CRUdJTiBSU0EgUFJJVkFURSBLRVktLS0tLQpNSUlFcEFJQkFBS0NBUUVBMmYrYWxqOFQz...

保存后 再执行

1
kubectl get node

得到

1
2
NAME             STATUS   ROLES           AGE    VERSION
docker-desktop Ready control-plane 4d1h v1.24.1

故障解除

本文标题:处理 "Unable to connect to the server dial tcp 127.0.0.1:52186 connectex No connection could be made because the target machine actively refused it." 的错误

文章作者:Morning Star

发布时间:2022年09月14日 - 16:09

最后更新:2022年09月14日 - 16:09

原始链接:https://www.mls-tech.info/microservice/k8s/k8s-handle-Unable-to-connect-to-the-server-dial-tcp-127-0-0-1-52186/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。