AKS (Kubernetes) and no connection could be made because the target machine actively refused it

1 minute read

A client of my had an error while connecting to different resources within their Kubernetes cluster in Azure (AKS).

Kubectl error

On the kubectl commands kubectl proxy and kubectl get nodes they got the error:

connectex: No connection could be made because the target machine actively refused it.

The complete message after visiting the dashboard (that wasn't loaded) on the command kubectl proxy was:

http: proxy error: dial tcp 127.0.0.1:8080: connectex: No connection could be made because the target machine actively refused it.

Kubectl Proxy

The problem occurs because the command line isn't able to load the correct config file from the file system. The config file is saved within an environment variable that you can change or alter.

Solution

The solution to the problem was changing or setting the environment variable to the kubernetes config file name "KUBECONFIG". You can do this by performing the following PowerShell command.

[Environment]::SetEnvironmentVariable("KUBECONFIG", $HOME + "\.kube\config", [EnvironmentVariableTarget]::Machine)