16
votes

I was able to cluster 2 nodes together in Kubernetes. The master node seems to be running fine but running any command on the worker node results in the error: "The connection to the server localhost:8080 was refused - did you specify the right host or port?"

From master (node1),

$ kubectl get nodes
NAME       STATUS    AGE       VERSION
node1       Ready     23h       v1.7.3
node2       Ready     23h       v1.7.3

From worker (node 2),

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

$ telnet localhost 8080
Trying ::1...
telnet: connect to address ::1: Connection refused
Trying 127.0.0.1...
telnet: connect to address 127.0.0.1: Connection refused

$ ping localhost
PING localhost (127.0.0.1) 56(84) bytes of data.
64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.032 ms

I am not sure how to fix this issue. Any help is appreciated.

On executing,"journalctl -xeu kubelet" I see: "CNI failed to retrieve network namespace path: Cannot find network namespace for the terminated container", but this seems to be related to installing a pod network ... which I am not able to because of the above error.

Thanks!

5

5 Answers

16
votes

kubectl interfaces with kube-apiserver for cluster management. The command works on the master node because that's where kube-apiserver runs. On the worker nodes, only kubelet and kube-proxy is running.

In fact, kubectl is supposed to be run on a client (eg. laptop, desktop) and not on the kubernetes nodes.

12
votes

from master you need ~/.kube/config pass this file as argument for kubectl command. Copy the config file to other server or laptop then pass the argument as for kubectl command

eg:

kubectl --kubeconfig=~/.kube/config

2
votes

This worked for me after executing following commands:

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

As a hint, the message being prompted indicates its related to network. So one potential answer could also be, which worked for my resolution, is to have a look at the key cluster value for context within contexts.

My error was that I had placed an incorrect cluster name there.

Having the appropriate cluster name is crucial to finding it for respective context and the error will disappear.

0
votes

To solve the issue The connection to the server localhost:8080 was refused - did you specify the right host or port?, you may be missing a step.

My Fix:

On MacOS if you install K8s with brew, you still need to brew install minikube, afterwards you should run minikube start. This will start your cluster.

Run the command kubectl cluster-info and you should get a happy path response similar to:

Kubernetes control plane is running at https://127.0.0.1:63000
KubeDNS is running at https://127.0.0.1:63308/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy

Kubernetes install steps: https://kubernetes.io/docs/tasks/tools/install-kubectl-macos/

Minikube docs: https://minikube.sigs.k8s.io/docs/start/