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

相关推荐
mCell1 小时前
从删库到跑路?这50个Linux命令能保你职业生涯
linux·windows·macos
杰克逊的日记1 小时前
GPU运维常见问题处理
linux·运维·gpu
誰能久伴不乏2 小时前
Linux系统调用概述与实现:深入浅出的解析
linux·运维·服务器
程序员学习随笔2 小时前
Linux进程深度解析(2):fork/exec写时拷贝性能优化与exit资源回收机制(进程创建和销毁)
linux·运维·服务器
mmoyula2 小时前
【RK3568 PWM 子系统(SG90)驱动开发详解】
android·linux·驱动开发
-SGlow-3 小时前
MySQL相关概念和易错知识点(2)(表结构的操作、数据类型、约束)
linux·运维·服务器·数据库·mysql
代码改变世界ctw3 小时前
Linux内核设计与实现 - 第14章 块I/O层
linux·运维·服务器
勇哥的编程江湖4 小时前
starrocks官网docker部署mysql无法连接
运维·docker·容器
van叶~5 小时前
Linux网络-------1.socket编程基础---(TCP-socket)
linux·网络·tcp/ip
风吹落叶花飘荡6 小时前
Ubuntu系统 系统盘和数据盘扩容具体操作
linux·运维·ubuntu