23

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?

Philipp Kyeck
  • 18,402
  • 15
  • 86
  • 123
Rumu
  • 403
  • 1
  • 3
  • 10

8 Answers8

26

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

minikube start

fixed it.

Marijn Deé
  • 886
  • 2
  • 13
  • 21
7

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
  • 986
  • 8
  • 9
  • I am facing the same problem. I have just installed k3s on my laptop along with kubectl and docker. I do not have the kubeconfig file in /etc/* too. What could be the solution to this? Thanks! – Andy Dufresne Sep 01 '21 at 06:43
  • k3s stores the cluster kubeconfig by default in: `/etc/rancher/k3s/k3s.yaml`. I do recommend reading this doc as well: https://rancher.com/docs/k3s/latest/en/cluster-access/ – isca Dec 22 '21 at 14:53
4

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!

akmozo
  • 9,829
  • 3
  • 28
  • 44
OlaB
  • 176
  • 11
4

creating cluster before running kubectl worked for me

gcloud container clusters create k0

Shashwat
  • 525
  • 4
  • 8
3

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..

Yamini Balakrishnan
  • 2,361
  • 16
  • 24
  • In my case this was exactly the issue, so thank you for the hint! Apparently something or someone turned on the swap again (because the cluster was working just fine before). – turbolocust Dec 04 '20 at 07:59
1

I'm a newbie in k8s, came here while working with microk8s & want to use kubectl on microk8s cluster.

run below command
microk8s config > ~/.kube/config

got the solution from this link
https://microk8s.io/docs/working-with-kubectl

overall, kubectl needs a config file to work with cluster (here microk8s cluster)

Thanks

  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – Niklas Mohrin Sep 06 '21 at 11:59
0

I also experienced the same issue when I executed kubectl get pods. The reason was docker desktop was not running, then I ran the docker desktop, checked for the docker and k8s running . Then again I ran kubectl get pods same output. Then I started minikube by minikube start. Everything went normal.

Pranu Pranav
  • 353
  • 3
  • 8
-1

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

Italo José
  • 1,558
  • 1
  • 17
  • 50