24

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

8 Answers 8

31

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

3
6

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.

1
4

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

0

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.

1
  • That's exactly what i did on my Mac too, or you can just open Docker GUI on Mac or Window before you use kubectl. You could check API server with docker ps | grep kube-apiserver
    – Aung Baw
    Commented May 11, 2023 at 3:19
0

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

0

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

-1

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.