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

相关推荐
CoolTiger、7 分钟前
【Vmware16安装教程】
linux·虚拟机·vmware16
m0_7417688538 分钟前
使用docker的小例子
运维·docker·容器
学习3人组1 小时前
CentOS 中配置 OpenJDK以及多版本管理
linux·运维·centos
厨 神2 小时前
vmware中的ubuntu系统扩容分区
linux·运维·ubuntu
Karoku0662 小时前
【网站架构部署与优化】web服务与http协议
linux·运维·服务器·数据库·http·架构
geek_Chen012 小时前
虚拟机共享文件夹开启后mnt/hgfs/下无sharefiles? --已解决
linux·运维·服务器
(⊙o⊙)~哦2 小时前
linux 解压缩
linux·运维·服务器
牧小七3 小时前
Linux命令---查看端口是否被占用
linux
最新小梦3 小时前
Docker日志管理
运维·docker·容器
鸡鸭扣4 小时前
虚拟机:3、(待更)WSL2安装Ubuntu系统+实现GPU直通
linux·运维·ubuntu