云其它

关注公众号 jb51net

关闭
首页 > 网站技巧 > 服务器 > 云和虚拟化 > 云其它 > k8s The connection to the server localhost:8080 was refused - did you specify

k8s kubectl启动成功,但执行命令时报错解决:The connection to the server localhost:8080 was refused - did you specify

作者:珂玥c

用户使用kubectl时出现连接拒绝错误,排查发现配置文件名非admin.conf,修改后问题解决,提示需使用kubernetes-admin配置文件以确保正确性

问题描述

node2 systemctl start kubectl 后可以正常启动服务,systemctl ststus kubectl 也显示running。

但是执行 kubectl cluster-info 或  kubectl get pod 均报错:

The connection to the server localhost:8080 was refused - did you specify the right host or port

原因分析

怀疑是重启服务之前 ~/.kube/config 被测试进行修改后导致报错

解决方案

首先查看具体的信息是如何导致的错误,按照提示信息执行命令,看起来并没有什么卵用:

kubectl cluster-info

kubectl cluster-info dump

接着查看了端口是否被占用,发现也没有:

 lsof -i:8080

 netstat -anp |grep 8080

最后添加了一下环境变量进项尝试,意外有收获,多了一条报错信息:

echo "export KUBECONFIG=/etc/kubernetes/admin.conf" >> ~/.bash_profile

source ~/.bash_profile

kubectl get pod  

###下次可以尝试一下这条命令,看看是否直接会有更详细的信息
kubectl get pod  -n kube-system

此时报错显示没有这个文件,查看后发现我的配置文件命名不是admin.conf,因此,从新设置成我的文件名,问题解决:

ls /etc/kubernetes

echo "export KUBECONFIG=/etc/kubernetes/kubelet.kubeconfig" >> ~/.bash_profile

source ~/.bash_profile

后续:

查询过其他文档,大佬们表示出现这个问题的原因是kubectl命令需要使用kubernetes-admin来运行,就是配置文件出现的问题。

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文