Kubernetes error: “The connection to the server localhost:8080 was refused – did you specify the right host or port ?”


This error is caused Kubeadm missing the environment variables it needs to run, and often happens after a restart of the master node.
To fix this issue, simply export the admin.conf on the master node.

sudo cp /etc/kubernetes/admin.conf $HOME
sudo chown $(id -u):$(id -g) $HOME/admin.conf
export KUBECONFIG=$HOME/admin.conf

Now run a “kubectl get” command and it should run without error:

kubectl get nodes

if that does not work, use below commands to disable swap.
The Kubelet service with not run unless swap is disabled on the all the master and worker nodes. Note that ‘swapoff -a’ immediately disables swap until server is reboot and swap is reinitialized with the ‘/etc/fstab’ configuration.

sudo -i
swapoff -a # this disables swap immediately
exit
strace -eopenat kubectl get nodes

To avoid having to do this whenever the master node is restarted, add the command to your $HOME/.bashrc


echo "export KUBECONFIG=$HOME/admin.conf" > $HOME/.bashrc

Do this to permanently disable swap:

sudo sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab


About Matthias 33 Articles
I am a Software Engineer from Houston, TX who love to write codes that brings great ideas to live. In my professional life, I have created software for different industries including Oil & Gas, Finance, Service Provider, Cloud Computing and Embedded Systems. When not writting codes, i enjoy travelling, good music and photography. You can reach me at me@matthiasomisore.com.

Be the first to comment

Leave a Reply

Your email address will not be published.


*