When I add this command:
sudo kubectl get node --kubeconfig /etc/kubernetes/admin.conf
I got
NAME STATUS ROLES AGE VERSION
master NotReady control-plane 42m v1.26.1
but when I send:
sudo kubectl get node
I got
E0126 08:58:10.293496 32223 memcache.go:238] couldn't get current server API group list: Get "http://localhost:8080/api?timeout=32s": dial tcp 127.0.0.1:8080: connect: connection refused
E0126 08:58:10.294046 32223 memcache.go:238] couldn't get current server API group list: Get "http://localhost:8080/api?timeout=32s": dial tcp 127.0.0.1:8080: connect: connection refused
E0126 08:58:10.295501 32223 memcache.go:238] couldn't get current server API group list: Get "http://localhost:8080/api?timeout=32s": dial tcp 127.0.0.1:8080: connect: connection refused
E0126 08:58:10.297011 32223 memcache.go:238] couldn't get current server API group list: Get "http://localhost:8080/api?timeout=32s": dial tcp 127.0.0.1:8080: connect: connection refused
E0126 08:58:10.298647 32223 memcache.go:238] couldn't get current server API group list: Get "http://localhost:8080/api?timeout=32s": dial tcp 127.0.0.1:8080: connect: connection refused
The connection to the server localhost:8080 was refused - did you specify the right host or port?
However, I exported the KUBECONFIG and sending
kubectl config view
will not return empty.
Shouldn't both work the same?
CodePudding user response:
As per this doc
kubectl get nodes The connection to the server localhost:8080 was refused - did you specify the right host or port? When you run the kubectl or a similar command, this is a common problem. Kubernetes lacks the proper credentials to access the cluster most of the time.
Resolution steps
1.Check if the kubeconfig environment variable is exported if not exported
export KUBECONFIG=/etc/kubernetes/admin.conf or $HOME/.kube/config
2.Check the home directory file or your.kube configuration. You need to move that to the home directory if you didn't find it. using the command that follows
cp /etc/kubernetes/admin.conf $HOME/
chown $(id -u):$(id -g) $HOME/admin.conf
export KUBECONFIG=$HOME/admin.conf
Whenever you are starting Master Node you may require to set the environment variable. Hence it’s a repetitive task for you. It can be set permanently using the following command.
echo 'export KUBECONFIG=$HOME/admin.conf' >> $HOME/.bashrc
A Kubernetes form discussion thread can also be found here.