kubectl apply command does not work, gives connection refused error

7/26/2018

when I am trying to setup pod network using the following

sudo kubectl apply -f https://docs.projectcalico.org/v3.1/getting-started/kubernetes/installation/hosted/rbac-kdd.yaml

I get this error, please help

unable to recognize "https://docs.projectcalico.org/v3.1/getting-started/kubernetes/installation/hosted/rbac-kdd.yaml": Get http://localhost:8080/api?timeout=32s: dial tcp 127.0.0.1:8080: connect: connection refused
unable to recognize "https://docs.projectcalico.org/v3.1/getting-started/kubernetes/installation/hosted/rbac-kdd.yaml": Get http://localhost:8080/api?timeout=32s: dial tcp 127.0.0.1:8080: connect: connection refused

UPDATE: Doesn't seem to be a permission issue, unlike other question

-- Jibin Mathew
kubectl
kubernetes

6 Answers

11/30/2019

For me the issue was that i had a non-root user that needed to run the command and to run the yaml file on the worker nodes, this non-root user needed to have a passwordless login to worker node and ability to become root. after configuring this non-root user to ssh to kubeworker without password, issue was resolved.

-- user3630391
Source: StackOverflow

7/26/2018

Found that it's an issue with kubectl not being configured properly.

Fixed the issued by using the following commands for calico network(change accordingly for your network addon plugin)

sudo kubeadm init --pod-network-cidr=192.168.0.0/16
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

and then run

sudo kubectl apply -f https://docs.projectcalico.org/v3.1/getting-started/kubernetes/installation/hosted/rbac-kdd.yaml

and follow the rest accordingly

-- Jibin Mathew
Source: StackOverflow

2/26/2019

Basically, when you start Kubernetes you start the services on port 6443, however the above command is searching for services running on port 8080. This is because when you run sudo, it runs using the variable from sudo which doesn't have information about the port on which Kubernetes is running, so it defaults to 8080. Run the following command as root user on the master node.

cp -i /etc/kubernetes/admin.conf $HOME/

Now run the kubectl apply command as below as root user.

kubectl apply -f https://docs.projectcalico.org/v3.1/getting-started/kubernetes/installation/hosted/rbac-kdd.yaml
-- Anirudha Singh
Source: StackOverflow

2/22/2020

I received this error when issuing a cluster command on Google Cloud Platform Cloud Shell.

It is discussed here

When you create a cluster using Google Cloud Console or using gcloud from a different computer, your environment's kubeconfig is not updated.

To autofix the kubeconfig use this gcloud command to fix the config:

gcloud container clusters get-credentials my-xyz-cluster --zone=us-central1-a

Once complete I was able to issue cluster changes without any error.

-- Leblanc Meneses
Source: StackOverflow

12/28/2018

The following issue leads me to the conclusion that the api-server might not be running.

I recommend to check the status of the kubelet service on the master node

systemctl status kubelet.service 

And check the logs for for more information to see why the api-server is not running and how to fix the problem

journalctl -xn --unit kubelet.service 
-- papanito
Source: StackOverflow

7/19/2019

I ran into the same problem and had to make the following change to my Ansible script:

- name: apply the flannel pod network add-on
  command: kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/a70459be0084506e4ec919aa1c114638878db11b/Documentation/kube-flannel.yml
  become: yes
  become_method: sudo
  become_user: <user>

During the Ansible playbook execution, there is a task which is running the kubectl apply command above. However, it is being executed by the root user. Because of this, the connection refused error occurs. To overcome this, I updated the playbook to use the become directive to sudo into my regular user account.

This post explains it:
https://www.jasonneurohr.com/articles/connection-refused-when-installing-flannel-on-kubernetes

-- John C
Source: StackOverflow