17

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

MP32
  • 573
  • 1
  • 9
  • 25

7 Answers7

19

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

Preet Sindhal
  • 459
  • 4
  • 7
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.

Igor Tiulkanov
  • 552
  • 5
  • 18
3

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

Lherben G
  • 289
  • 1
  • 4
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.

Jake
  • 4,322
  • 6
  • 39
  • 83
  • 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 Zan Baw May 11 '23 at 03:19
0

The reply stating that you do not have ~/.kube/config properly configured is correct. However, if you are using "sudo kubectl," then you are running your commands as essentially a root user. And as such root must have the /root/.kube/config in place.

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

This will allow sudo users the proper configuration to run kubectl commands

Surfingjoe
  • 11
  • 2
-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.