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

Viewed 8507

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

4 Answers

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

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.

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 !

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.

Related