kubectl: Connection to server was refused

6/20/2016

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?

-- Rumu
containers
docker
kubectl
kubernetes
yaml

6 Answers

3/16/2018

try run with sudo permission mode sudo kubectl run....

-- Italo José
Source: StackOverflow

1/6/2019

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

minikube start

fixed it.

-- Marijn Deé
Source: StackOverflow

6/20/2016

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!

-- OlaB
Source: StackOverflow

9/20/2016

creating cluster before running kubectl worked for me

gcloud container clusters create k0

-- Shashwat
Source: StackOverflow

4/22/2019

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
-- isca
Source: StackOverflow

5/21/2018

If swap is not disabled, kubelet service will not start on the masters and nodes, for Platform9 Managed Kubernetes version 3.3 and above..

By running the below command to turn off swap memory

sudo swapoff -a

To make it permanent go to /etc/fstab and comment the swap line works well..

-- Veeresh Reddy
Source: StackOverflow