Are you getting this error message when you are running the Kubernetes Kubectl command.

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

Xybernetics Kubernetes Rancher K3S localhost:8080 was refused right host or port

Well…. you came to the right place. Allow me to show you how to resolve it.

K3S or the Rancher server.
1) Get the content of the config file (k3s.yaml) from the K3S or the Rancher server. The configuration can be found the in the directory below (in the K3S or the Rancher server).
You can either copy the content in a clipboard or a text file.
cat /etc/rancher/k3s/k3s.yaml

Xybernetics Kubernetes Rancher K3S Server Config File Directory Location k3s.yaml

Computer To Run Kubectl Command
1) Change directory to ~
cd ~

2) Create directory called .kube in the ~ directory
mkdir .kube

3) Change directory to .kube
cd .kube

Xybernetics Kubernetes Rancher K3S Server cd mkdir .kube

4) Create new config file. You can use any text editor; nano, vim, vi, etc.
nano config

5) Paste the config content that you copied from K3S or Rancher into this file.

Xybernetics Kubernetes Rancher K3S Server nano config

6) Done.

Xybernetics Kubernetes Rancher K3S Server .kube config file

Command Featured In This Video

1) Get Kubernetes nodes
kubectl get nodes

2) Display Kubectl configuration file
kubectl config view

3) Location of the Kubectl config file in K3S Server
cat /etc/rancher/k3s/k3s.yaml

What is a Kubectl?
Kubernetes provides a command line tool for communicating with a Kubernetes cluster’s control plane, using the Kubernetes API. That command line tools is Kubectl.

https://youtu.be/IDG0xpTjwuM

Reference