This blog post covers the issue & fix which most of us encountered while performing the kubectl get command. We often see a kubectl get server error while saying the connection to the server localhost:8080 was refused
$ kubectl get nodes The connection to the server localhost:8080 was refused - did you specify the right host or port?
This is a common issue when you run the kubectl command or a similar command. In most cases, Kubernetes does not have the correct credentials to access the cluster. It can be easily resolved easily by setting an environment variable in most similar cases.
Check out: How To Setup A Three Node Kubernetes Cluster For CKA: Step By Step
Background of Error
The Kubernetes command-line tool, kubectl, allows you to run commands against Kubernetes clusters. You can use kubectl to deploy applications, inspect and manage cluster resources, and view logs. Kubectl commands are used to interact and manage Kubernetes objects and the cluster. If kubectl does not have the correct credentials to access the cluster this issue may encounter.
Issue Encountered – Kubectl the connection to the server localhost:8080 was refused – did you specify the right host or port?
when you run any kubectl or a similar command in Kubernetes you may get an error message like this The connection to the server localhost:8080 was refused – did you specify the right host or port?.
Cause of Error
1.This problem can arise when you haven’t set the kubeconfig environment variable.
export KUBECONFIG=/etc/kubernetes/admin.conf or $HOME/.kube/config
2. .kube/config file is not exported to User $HOME directory
cp /etc/kubernetes/admin.conf $HOME/ chown $(id -u):$(id -g) $HOME/admin.conf export KUBECONFIG=$HOME/admin.conf
Fix the Error – The connection to the server localhost:8080 was refused
1. Check if the kubeconfig environment variable is exported if not exported
export KUBECONFIG=/etc/kubernetes/admin.conf or $HOME/.kube/config
2. Check your .kube or config in the home directory file. If you did not found it, then you need to move that to the home directory. using the following command
cp /etc/kubernetes/admin.conf $HOME/ chown $(id -u):$(id -g) $HOME/admin.conf export KUBECONFIG=$HOME/admin.conf
Whenever you are starting Master Node you may require to set the environment variable. Hence it’s a repetitive task for you. It can be set permanently using the following command.
echo 'export KUBECONFIG=$HOME/admin.conf' >> $HOME/.bashrc
You can also check a discussion thread on Kubernetes form here
To Download Kubernetes CKA Sample Exam Questions, Click here.
Related Post
- How To Setup A Three Node Kubernetes Cluster For CKA: Step By Step
- Certified Kubernetes Administrator (CKA): Step-by-Step Activity Guide (Hands-on Lab)
- CKA Certification Exam (Certified Kubernetes Administrator)
- Kubernetes for Beginners – A Complete Beginners Guide
- Kubernetes Dashboard: An Overview, Installation, and Accessing
- CKA/CKAD Exam Questions & Answers 2022
- Docker Container Lifecycle Management: Create, Run, Pause, Stop And Delete
- CKA vs CKAD vs CKS – Differences & Which Exam is Best For You?
- Etcd Backup And Restore In Kubernetes: Step By Step
Join FREE Class
Begin your journey towards becoming a Certified Kubernetes Administrator [CKA] from our Certified Kubernetes Administrator (CKA) training program. To know about the Roles and Responsibilities of a Kubernetes administrator, why learn Docker and Kubernetes, Job opportunities for Kubernetes administrator in the market. Also, know about Hands-On labs you must perform to clear the Certified Kubernetes Administrator (CKA) Certification exam by registering for our FREE class.




What are environment variables? Please explain
Environment variables are a common way for developers to move application and infrastructure configuration into an external source outside of application code.
To learn more please to the official documentation of kubernetes: https://kubernetes.io/docs/tasks/inject-data-application/define-environment-variable-container/
Thanks & Regards
Sahid
Team K21
This error is also caused when you use the wrong user i.e
$ sudo kubectl get nodes
So drop the sudo
thanks, it work for me
Hi Prateek,
Glad you liked the blog and this helped you.
Please stay tuned for more informative blogs.
Thanks and Regards
Rahul Dangayach
Team K21 Academy
This helped resolved my issue, thanks.
Hi Ramsy,
Glad you liked the blog and that this helped you.
Please stay tuned for more informative blogs.
Thanks and Regards
Rahul Dangayach
Team K21 Academy
I still have the same error despite the fact that I implemented same steps you did. I am trying to run k8s via EC2 instances. Please help
Hi Tevfik,
Are you still getting the error for port 8080 or this time it’s showing another port?
Please let us know. So that we could suggest the resolution accordingly.
Thanks & Regards
Sahid
Team K21