Applying a configuration file using Kubectl -f filename.yml but getting this error

12/17/2019

I have a deployment yaml file created. I am trying to apply this configuration using kubectl apply -f filename.yml, but I am getting this error: The connection to the server localhost:8080 was refused - did you specify the right host or port?

How can I make sure that I connect to localhost:8080? or remove any restrictions in doing so?

-- Ryguy444222
kubectl
kubernetes

2 Answers

1/16/2020

kubectl is a command-line tool tool that allows you to run commands against Kubernetes clusters

By default kubectl looks for a file named config in the $HOME/.kube directory to connect with cluster. ensure you have a file named config with correct values in this path.

$ pwd
/home/ubuntu/.kube

$ ll
total 88
drwxrwxrwx 4 ubuntu ubuntu  4096 Jan 16 10:31 ./
drwxr-xr-x 7 ubuntu ubuntu  4096 Jan 16 10:30 ../
drwxr-x--- 3 ubuntu ubuntu  4096 Jan 13 13:00 cache/
-rwxr-xr-x 1 ubuntu ubuntu  5455 Jan 13 15:17 config*

You can specify other kubeconfig files by setting the KUBECONFIG environment variable or by setting the --kubeconfig flag.

Note: If you use --kubeconfig to pass config file it takes precedence over the KUBECONFIG environment variable.

Example : Setting KUBECONFIG env varaible.

$ KUBECONFIG=$HOME/.kube/cluster-1

$ kubectl cluster-info
Kubernetes master is running at https://xx.xx.xx.101:6443
KubeDNS is running at https://xx.xx.xx.101:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy

To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.

Example : Using --kubeconfig to pass config file to override default KUBECONFIG

$ kubectl cluster-info --kubeconfig=/home/ubuntu/.kube/cluster-2
Kubernetes master is running at https://xx.xx.xx.102:6443
KubeDNS is running at https://xx.xx.xx.102:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy

To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.

To reproduce your error I simply set KUBECONFIG to empty to show that you are missing this setting when running kubectl commands

$ KUBECONFIG=""

$ kubectl get pods
The connection to the server localhost:8080 was refused - did you specify the right host or port?
-- DT.
Source: StackOverflow

12/17/2019

It seems like your kubectl CLI is not configured properly.

By default, kubectl looks for a file named config in the $HOME/.kube directory. You can specify other kubeconfig files by setting the KUBECONFIG environment variable or by setting the --kubeconfig flag.

-- Neutralizer
Source: StackOverflow