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

asked May 22, 2022 at 0:27

MP32's user avatar

MP32MP32

7213 gold badges15 silver badges33 bronze badges

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

answered May 22, 2022 at 1:16

Preet Sindhal's user avatar

3

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.

answered Sep 7, 2022 at 10:56

Igor Tiulkanov's user avatar

1

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

answered Jan 27, 2023 at 7:56

Lherben G's user avatar

Lherben GLherben G

3932 silver badges6 bronze badges

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.

answered Aug 21, 2022 at 23:42

Jake's user avatar

JakeJake

4,68010 gold badges43 silver badges89 bronze badges

1

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

answered Dec 14, 2023 at 21:44

JagadeeshK's user avatar

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

texasdave's user avatar

texasdavetexasdave

7561 gold badge5 silver badges16 bronze badges

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 !

answered Aug 24, 2022 at 11:55

TechHari's user avatar

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

answered Dec 8, 2022 at 21:12

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.