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 – did you specify the right host or port?
$ 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
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.
The post [Solved] The connection to the server localhost:8080 was refused – did you specify the right host or port? appeared first on Cloud Training Program.