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

相关推荐
wsad053224 分钟前
CentOS 7 更换腾讯云 yum 源及 EPEL 源
linux·centos·腾讯云
wdfk_prog24 分钟前
[Linux]学习笔记系列 -- [kernel]kallsyms
linux·笔记·学习
emiya_saber1 小时前
Linux 进程调度管理
linux·运维·服务器
不脱发的程序猿1 小时前
嵌入式Linux:线程同步(读写锁)
linux·嵌入式
yangzhi_emo1 小时前
配置dns主从服务
linux·运维·服务器
SongYuLong的博客3 小时前
Linux开源代码汇总
linux·运维·服务器
JiMoKuangXiangQu3 小时前
Linux 内存管理 (5):buddy 内存分配简要流程
linux·内存管理·buddy 分配器
铭哥的编程日记3 小时前
【Linux网络】应用层协议HTTP
linux·运维·http
yolo_guo3 小时前
opencv 学习: 07 使用迭代器 (iterator) 遍历像素
linux·c++·opencv
子牙老师3 小时前
硬盘第一关:MBR VS GPT
linux·操作系统·文件系统