问题描述:
K8s中,主节点init结束后,执行kubectl get nodes时,出现The connection to the server localhost:8080 was refused- did you specify the right host or port?报错

#以主节点为例:
queena@queena-Lenovo:~$ sudo kubeadm init --apiserver-advertise-address=192.168.31.245 --pod-network-cidr=10.244.0.0/16  --kubernetes-version=v1.22.3
...
Your Kubernetes control-plane has initialized successfully!
...

queena@queena-lenovo:~$ kubectl get nodes
The connection to the server localhost:8080 was refused - did you specify the right host or port?
#出现此报错.

解决方法:
K8s没有与本机绑定,需要设置在本机的环境变量,即可解决问题。

#设置环境变量
queena@queena-lenovo:~$ sudo su 
root@queena-lenovo: echo "export KUBECONFIG=/etc/kubernetes/admin.conf" >> /etc/profile

#使其生效
root@queena-lenovo: source /etc/profile

#再次执行kubectl get nodes,不会报错了.
root@king-ubuntu:/etc/kubernetes# kubectl get nodes
NAME                      STATUS     ROLES                               AGE   VERSION
queena-lenovo   Ready      control-plane,master   24h    v1.22.3

至此,问题得以解决!

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐