MP32
MP32

Reputation: 721

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

I'm on an ec2 instance trying to get my cluster created. I have kubectl already installed and here are my services and workloads yaml files

services.yaml

apiVersion: v1
kind: Service
metadata:
  name: stockapi-webapp

spec:
  selector:
    app: stockapi

  ports:
    - name: http
      port: 80

  type: LoadBalancer

workloads.yaml

apiVersion: v1
kind: Deployment
metadata:
  name: stockapi
spec:
  selector:
    matchLabels:
      app: stockapi
  replicas: 1
  template: # template for the pods
    metadata:
      labels:
        app: stockapi
    spec:
      containers:
      - name: stock-api
        image: public.ecr.aws/u1c1h9j4/stock-api:latest

When I try to run

kubectl apply -f workloads.yaml

I get this as an error

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

I also tried changing the port in my services.yaml to 8080 and that didn't fix it either

Upvotes: 24

Views: 124962

Answers (8)

texasdave
texasdave

Reputation: 716

I was using ansible trying to install calico and discovered I had placed some extra arguments in the playbook command. I took the extra args out and ran it like this and it finally worked:

ansible-playbook -i inventory/inventory.yml --limit "control-1" --ask-become-pass -u XXXX kubernetes_install_part2_network.yml

Upvotes: 0

JagadeeshK
JagadeeshK

Reputation: 47

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

In my case, issue was fixed by exporting the KUBECONFIG

export KUBECONFIG=/home/.kube/kubeconfig

Upvotes: 0

Lherben G
Lherben G

Reputation: 383

Encountered the exact error in my cluster when I executed the "kubectl get nodes" command.

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

I ran the following command in master node and it fixed the error.

apt-get update && apt-get install -y apt-transport-https ca-certificates curl software-properties-common

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -

add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable"

apt-get update && apt-get install -y containerd.io

Configure containerd

mkdir -p /etc/containerd

containerd config default > /etc/containerd/config.toml

systemctl restart containerd

Upvotes: 4

user18547346
user18547346

Reputation:

I got the same error and after switching from root user to regular user (ubuntu, etc...) my problem was fixed.

Upvotes: -1

Igor Tiulkanov
Igor Tiulkanov

Reputation: 596

In my case I had a problem with a certificate authority. Found out that by checking the kubectl config

kubectl config view

The clusters part was null, instead of having something similar to

- cluster:
certificate-authority-data: DATA+OMITTED
server: https://kubernetes.docker.internal:6443
  name: docker-desktop

It was not parsed because of time differences between my machine and a server (several seconds was enough).

Running

sudo apt-get install ntp
sudo apt-get install ntpdate
sudo ntpdate ntp.ubuntu.com

Had solved the issue.

Upvotes: 6

TechHari
TechHari

Reputation: 1

I found how to solve this question. Run the below commands

1.sudo -i 2.swapoff -a 3.exit 4.strace -eopenat kubectl version

and you can type kubectl get nodes again.

Cheers !

Upvotes: -1

Jake
Jake

Reputation: 4660

I was following the instructions on aws

With me, I was on a Mac. I had docker desktop installed. This seemed to include kubectl in homebrew

I traced it down to a link in usr/local/bin and renamed it to kubectl-old

Then I reinstalled kubectl, put it on my path and everything worked.

I know this is very specific to my case, but may help others.

Upvotes: 0

Preet Sindhal
Preet Sindhal

Reputation: 599

This error comes when you don't have ~/.kube/config file present or configured correctly on the client / where you run the kubectl command.

kubectl reads the clusterinfo and which port to connect to from the ~/.kube/config file.

if you are using eks here's how you can create config file aws eks create kubeconfig file

Upvotes: 31

Related Questions