kubectl get nodes报错:The connection to the server localhost:8080

报错描述kubectl get nodes命令无法执行

在K8S-master初始化后,worker-node节点加入K8S集群后

bash 复制代码
kubeadm join 192.168.31.150:6443 --token 2n0t62.gvuu8x3zui9o8xnc \
        --discovery-token-ca-cert-hash sha256:d294c082cc7e0d5f620fb10e527a8a7cb4cb6ccd8dc45ffaf2cddd9bd3016695

通过kubectl get nodes查看集群的情况,出现了报错,内容如下:

bash 复制代码
[root@k8s-master01 ~]# kubectl get nodes
E1114 16:28:52.032089    2254 memcache.go:265] couldn't get current server API group list: Get "http://localhost:8080/api?timeout=32s": dial tcp [::1]:8080: connect: connection refused
E1114 16:28:52.032313    2254 memcache.go:265] couldn't get current server API group list: Get "http://localhost:8080/api?timeout=32s": dial tcp [::1]:8080: connect: connection refused
E1114 16:28:52.033599    2254 memcache.go:265] couldn't get current server API group list: Get "http://localhost:8080/api?timeout=32s": dial tcp [::1]:8080: connect: connection refused
E1114 16:28:52.034881    2254 memcache.go:265] couldn't get current server API group list: Get "http://localhost:8080/api?timeout=32s": dial tcp [::1]:8080: connect: connection refused
E1114 16:28:52.036130    2254 memcache.go:265] couldn't get current server API group list: Get "http://localhost:8080/api?timeout=32s": dial tcp [::1]:8080: connect: connection refused
The connection to the server localhost:8080 was refused - did you specify the right host or port?

最初,查找在/etc/kubernetes目录下,存放了许多K8S配置文件,通过kubectl --kubeconfig命令执行正常,这样执行感觉很怪。

解决办法

最终发现,答案其实就在k8s-worker-node节点生成的内容当中,只需要master执行以下命令就可以:

bash 复制代码
mkdir -p $HOME/.kube

cp -i /etc/kubernetes/admin.conf $HOME/.kube/config

chown $(id -u):$(id -g) $HOME/.kube/config

mkdir -p $HOME/.kube ##创建一个名为 .kube 的目录,该目录用于存储 Kubernetes 相关的配置文件

cp -i /etc/kubernetes/admin.conf $HOME/.kube/config##表示复制 Kubernetes 集群的管理员配置文件(通常是 /etc/kubernetes/admin.conf)到用户的 .kube 目录下的 config 文件。

chown $(id -u):$(id -g) $HOME/.kube/config ##更改用户的 .kube/config 文件的所有者为当前用户(通过 ( i d − u ) 获取用户 I D , (id -u) 获取用户 ID, (id−u)获取用户ID,(id -g) 获取用户所属的主要组 ID)。

再次执行该命令:kubectl get nodes

相关推荐
IT利刃出鞘15 分钟前
K8S--解决dashboard一直Pending的问题
k8s
爱学习 爱分享6 小时前
k8s 开启防火墙,容器内部无法访问外部 ip
tcp/ip·容器·kubernetes
叶帆7 小时前
【YFIOs】Docker方式部署
运维·docker·容器
岳来8 小时前
Docker 的 --privileged 特权模式学习
docker·容器·--privileged
彩色的黑'''8 小时前
[root@localhost ~]#,Linux系统的命令提示符为啥现在变成-bash-4.2#了,哪里设置的
linux·运维·bash
源远流长jerry9 小时前
Linux 网络发送机制深度解析:从应用到网线
linux·服务器·网络·网络协议·tcp/ip
南境十里·墨染春水9 小时前
linux学习进展 shell编程
linux·运维·学习
goyeer10 小时前
【ITIL4】32服务实践 - 问题管理(Problem Management)
linux·运维·服务器·企业数字化·it管理·itil·it治理
怀旧,10 小时前
【Linux网络编程】8. 网络层协议 IP
linux·网络·tcp/ip
RH23121110 小时前
2026.5.12 Linux
java·linux·数据结构