I just installed kubectl and I am trying to run my first command like 'kubectl get nodes', but I keep getting an error saying "The connection to the server localhost:8080 was refused - did you specify the right host or port?". I haven't manually configured a server on port 8080, so I'm confused why kubectl is looking there by default. Is there a missing configuration file or an environment variable I need to set so that it points to my actual cluster API server instead of localhost?
3 answers
This error occurs because kubectl cannot find a valid configuration file (the kubeconfig) and is defaulting to localhost:8080 as a last resort. To fix this, you need to ensure that your cluster configuration is located at ~/.kube/config. If you are using a tool like Minikube or an managed service like EKS, you usually need to run a command to generate this file. For example, in Minikube, running minikube start should set this up for you automatically. If you already have the file elsewhere, you can set the environment variable by running export KUBECONFIG=/path/to/your/config. Once the tool knows where your actual API server is located (usually on port 6443, not 8080), the commands will start working immediately.
I checked my ~/.kube/ directory and the config file is definitely there, but I am still getting the localhost:8080 error. Could it be a permissions issue with the folder, or is it possible that the context inside the config file is set incorrectly? I'm on a Linux machine and I used sudo to install most of the tools.
Sometimes this happens because the Docker Desktop Kubernetes engine isn't fully started yet. Ensure your cluster status is 'Running' before executing kubectl commands.
I agree with Linda. In my experience with Cloud Technology on local machines, we often forget that the API server is a container itself. If the container runtime hasn't finished booting up the control plane, kubectl has nowhere to go and defaults back to that 8080 error.
Steven, if you used sudo to create the config, your current user might not have permission to read it. Try running sudo chown -R $USER:$USER ~/.kube. Also, check the content of the file with kubectl config view. If the "server" field under the current context still says localhost:8080, you’ll need to update it to point to your master node's IP. This is a common Software Development hurdle when shifting between local and remote environments—the context must always match the active cluster's endpoint.