Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

kubectl: Connection to server was refused

When I run kubectl run ... or any command I get an error message saying

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

What exactly is this error and how to resolve it?

like image 513
Rumu Avatar asked Jun 20 '16 11:06

Rumu


4 Answers

In my case, working with minikube I had not started minikube. Starting minikube with

minikube start

fixed it.

like image 62
Marijn Deé Avatar answered Oct 07 '22 06:10

Marijn Deé


In most cases, this means a missing kubeconfig file. kubectl is trying to use the default values when there is no $HOME/.kube/config. You must create or copy a valid config file to solve this problem. For example if you are using kubeadm you can solve this with:

mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

Alternatively you can also export KUBECONFIG variable like this:

export KUBECONFIG=/etc/kubernetes/admin.conf
like image 37
isca Avatar answered Oct 07 '22 06:10

isca


I really don't know much about kubectl... But the various reasons you have a connection refused to localhost I know of are as follows

  • 1) Make sure you can resolve and ping your local host with the IP(127.XX.XX.XX) and also "localhost" if using a DNS or host file.

  • 2) Make sure the service trying to access the localhost has enough permissions to run as root if trying to use localhost.

  • 3) Check the ports with netstat and check for the appropriate flags you need amongst the "Plantu" flags, Look up the meaning of each of the flags as it would apply to your situation. If the service you are trying to access on localhost is listening on that port, netstat would let you know.

  • 4) Check if you have admin or management settings in your application that needs permissions to access your localhost in the configuration parameters of your application.

  • 5) According to the statement that says did you specify the right host or port, this means that either your "kubectl" run is not configured to run as localhost instead your primary DNS server hostname or IP, Check what host is configured to run your application and like I said check for the appropriate ports to use, You can use telnet to check this port and further troubleshoot form there.

My two cents!

like image 30
OlaB Avatar answered Oct 07 '22 06:10

OlaB


creating cluster before running kubectl worked for me

gcloud container clusters create k0

like image 3
Shashwat Avatar answered Oct 07 '22 05:10

Shashwat