Kubernetes error: Unable to connect to the server: dial tcp 127.0.0.1:8080

Viewed 26054

I'm seeing the following error when running kubectl get pods:

Unable to connect to the server: dial tcp 127.0.0.1:8080: connectex: No connection could be made because the target machine actively refused it.

This was working when I ran it two weeks ago. My config file in the ~/.kube/config directory looks as follows:

apiVersion: v1
clusters:
- cluster:
    insecure-skip-tls-verify: true
    server: https://zld05687.vci.co.com
  name: dev-cluster
- cluster:
    insecure-skip-tls-verify: true
    server: https://zlt20087.vci.co.com
  name: perf-cluster
- cluster:
    insecure-skip-tls-verify: true
    server: https://zlt17291.vci.co.com
  name: test-cluster
contexts:
- context:
    cluster: dev-cluster
    namespace: com-co-acp-dev
    user: default-user
  name: default-context
- context:
    cluster: perf-cluster
    namespace: com-co-acp-perf
    user: default-user
  name: perf-context
- context:
    cluster: test-cluster
    namespace: com-co-acp-test
    user: default-user
  name: test-context
current-context: perf-context
kind: Config
preferences: {}
users:
- name: default-user
  user:
    token: myusername:enc:23hfeu39393yd223944uekd

What caused this error, and how can I fix it?

5 Answers

If you have kubectl already installed and pointing to some other environment, such as minikube or a GKE cluster, be sure to change context so that kubectl is pointing to docker-desktop:

  1. kubectl config get-contexts

  2. kubectl config use-context docker-desktop

Not sure if it applies to your environment, but I was having similar issue - any kubectl commands were returning: Unable to connect to the server: dial tcp [::1]:8080: connectex: No connection could be made because the target machine actively refused it.

127.0.0.1 is local host (IPV4), [::1] is the IPV6 equivalent.

kubectl appears to set up a local connection proxy on localhost to the cluster controller. In my case on a Windows10 pc, running the session ( in my case it was a powershell window) as administrator resolved the issue. Anyway, that was the fix for my environment.

It looks like the Kubernetes is not running on your machine. As your question doesn't mention the environment. I had similar issues with the following setup.

Windows 7 with Docker Toolbox 18.09.6

Kubernetes v1.14.3

minikube v1.1.1.

You would need to start Kubernetes and check if it displays the cluster information correctly before getting the pods. Run the following commands to setup & display cluster info.

minikube start

kubectl cluster-info

kubectl get pods

enter image description here

enter image description here

The Kubernetes server API seems invalid:

curl -LIk https://zlt20087.vci.co.com
HTTP/1.1 301 Moved Permanently
Date: Wed, 02 Jan 2019 21:43:24 GMT
Server: Apache
Location: https://registry.co.com
Content-Type: text/html; charset=iso-8859-1

HTTP/1.1 403 Forbidden
Date: Wed, 02 Jan 2019 21:43:24 GMT
Server: Apache
Vary: User-Agent
Accept-Ranges: bytes
Content-Type: text/html

Are you sure the URL is correct?

This can happen due to a spelling error.

In the .deployment file, errors (for me) were called out in the terminal. In the .secret file, they were not.

apiVersion: v1
kind: Secret
metatdata:
  name: mongodb-secret
  type: Opaque
data:
  mongo-root-username: bGlsbHk=
  mongo-root-password: cGFzc3dvcmQ=

'type:' is incorrectly indented & metatdata is spelt wrong. Once I fixes these, I was good to go.

apiVersion: v1
kind: Secret
metadata:
  name: mongodb-secret
type: Opaque
data:
  mongo-root-username: bGlsbHk=
  mongo-root-password: cGFzc3dvcmQ=
Related