kubernetes: The connection to the server localhost:8080 was refused

The error

When you try to execute kubectl you get the error:

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

Never try to execute kubectl with sudo! NEVER!, this is not a good solution. Check the following solutions below.

Solution 1: Google cloud

When using google cloud follow these steps:

  • Navigate to Container Engine Google Cloud Platform -> Container Engine
  • Click CONNECT on Cluster
  • In your Terminal, use the login credentials to access the cluster.

Solution 2

When you start using the cluster you will need to run:

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