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

This fixed my issue. Thank you.

Use the following command to create the default config view
sudo kubeadm init

I’ve skipped the step of creating the directory and file [ $HOME/.kube/config ] for the user in use. after these steps, no more issues.

Thank you for this great sharing! :+1:

try command for minikube:

minikube start --vm-driver=none

should help to config.

3q , it’s work

I follow these steps to fix it.
sudo kubeadm init
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
export KUBECONFIG=$HOME/.kube/config
kubectl get nodes

**** Result***
NAME STATUS ROLES AGE VERSION
kubernetes-master Ready control-plane 2m3s v1.24.1

2 Likes

I was facing the same issue while working on Microk8s :slight_smile:

Book-Pro ~ % kubectl get services
The connection to the server localhost:8080 was refused - did you specify the right host or port?

Book-Pro ~ % kubectl get secrets
The connection to the server localhost:8080 was refused - did you specify the right host or port?

But the below resolved the issue: alias kubectl=‘microk8s kubectl’

Book-Pro Downloads % alias kubectl=‘microk8s kubectl’
Book-Pro Downloads % kubectl get pods --all-namespaces
NAMESPACE NAME READY STATUS RESTARTS AGE
production nginx 1/1 Running 0 18h
default test-deployment-548 1/1 Running 0 18h
default test-deployment-5486 1/1 Running 0 18h
default test-deployment-5486f6 1/1 Running 0 18h
kube-system calico-kube-controllers-5b9 1/1 Running 0 19h
ingress nginx-ingress-microk8s- 1/1 Running 0 18h
kube-system calico-n 1/1 Running 0 19h
kube-system coredns-7 1/1 Running 0 17h
kube-system hostpath-prov 1/1 Running 0 17h
default busy 1/1 Running 15 (14m ago) 18h
default wordp 0/1 CrashLoopBackOff 40 (3m32s ago) 17h
default wordpre 0/1 ImagePullBackOff 0 17h
default web-0 0/1 CrashLoopBackOff 9 (35s ago) 21m
Book-Pro Downloads %

this worked for me, thanks for the tip.

mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
export KUBECONFIG=$HOME/.kube/config
kubectl get nodes

I just found this info in the official documentation that recommends adding the above config:
Creating a cluster with kubeadm - More Information

but admin.conf file is at master node,
here we are getting issue with worker node. in worker node there is no admin.config file at the specified location, (we can get this file by running command “kubeadm init” ,but then it will act as a master node). we are getting this issue for slave/worker node .

Thanks Niranjan_Alur ,
By this Technique i get rid of this issue in worker-Nodes


as you said i copied the admin.conf file content and pasted in the worker node
in this path ~/.kube/config . then i can able to run the command “kubectl get nodes” (as a sudo user) and it worked .

Worked for me. Thank you.

thanks, this suggestion helped me, I configured contexts and it worked.

Hi, I have faced problems by installing the K8sa and helm via the command
“./install_k8s_and_helm.sh”
May anyone help me with other commands do the same thing ?
becuase I am following a demo and I couldn’t proceed forward

For me it was a root user. After I switched a user, I could see pods.

Hi Hemanth,

Am facing same issues

Please help me

Hi Gil

Please help me here

it works for me, thanks a lot.

I am in the process of setting up a Ubuntu Kubernetes/Docker cluster (this is at home using six bare-metal machines). I have started with the master node and one slave node following (for the most part) the instructions here How To Install Kubernetes on Ubuntu. When I boot my master node quite often I receive intermittent “the connection to the server was refused - did you specify the right host or port?” type messages. I want to stress the point that these errors are intermittent meaning: “kubectl get nodes” will result in this type of error, but then will clear themselves up later, and then fail again in the same manner. Given this, I have to assume configuration files are correct, directory structure is correct, required processes are being started under the correct context, and the necessary permissions have been applied where they should have been. I have re-installed both the master and slave node numerous times using different “how to” guides, but having the same issue.

Even though it is not my strong suite, I would like to stay the Ubuntu course. I could go the Windows route
however, Linux seems the dominant operating system as far as Kubernetes is concerned.

Given the details provided here, what could possibly going wrong?

Hi guys.My operation system ubuntu and kubernetes version v1.25.2 I try all that here writing. Nothing don’t help me. I try all this command in root user and also user, but nothing.

Can anybody help me how to fix this error?

Try this workaround
sudo kubectl --kubeconfig ~/.kube/config get node