The connection to the server localhost 8080 was refused — did you specify the right host or port due to wrong kubectl config

Kun-Hung Tsai
1 min readJul 24, 2021

I was trying to copy a kubectl config from other machine by hand yesterday to set up my new environment. However, I kept getting “The connection to the server localhost 8080 was refused — did you specify the right host or port” error for all my kubectl operations.

Most of the results from Google mentioned that the correct config path should be set to resolve this issue, but none of them helped. I started to check if there is any problem for the routing to my cluster and even looked into /etc/hosts.

It turned out that I accidentally add intents in name part of my cluster config. This makes kubectl unable to recognize the cluster name and will try to connect to localhost:8080 .

The config with error indents is as below. It has four spaces for the name part in cluster config.

kubectl config with wrong indent

The correct one should be only two spaces.

kubectl config with correct indent

For the same reason, if you have wrong indent for the server part, you will get the same error.

I hope this will help someone facing the same issue with me.

--

--