kubectl 命令报错couldn't get current server API group list: ... connect: connection refused

kubenets在运行时,依赖环境变量。如果所有的服务都是正常的,只是kubectl命令报错,那就是环境变量没有设置导致。一般报错样例如下: 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?

报错样例

解决方案

临时方案

可以直接export环境变量

export KUBECONFIG=/etc/kubernetes/admin.conf

长期方案

将环境变量复制一份到默认的kube配置文件里

mkdir ~/.kube
cp /etc/kubernetes/admin.conf ~/.kube/config

需要重新登录用户