Connection Refused when Installing Flannel on Kubernetes


During the setup of a Kubernetes master using Ansible, I ran into the following error when installing the Flannel pod network add-on as described in the Installing a pod network add-on in the Creating a single master cluster with kubeadm guide.

"unable to recognize \"https://raw.githubusercontent.com/coreos/flannel/a70459be0084506e4ec919aa1c114638878db11b/Documentation/kube-flannel.yml\": Get http://localhost:8080/api?timeout=32s: dial tcp [::1]:8080: connect: connection refused"

The command being executed was:

kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/a70459be0084506e4ec919aa1c114638878db11b/Documentation/kube-flannel.yml

During the initial kubeadm init, I had followed the steps provided in the output here under a regular user account, for context the commands are:

mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

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.

- 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: jason

Then execute the playbook supplying the required password.

ansible-playbook playbook.yml -i hosts --ask-pass --ask-become-pass

Then the command completed successfully.

For reference, the same issue occurs if you run the command as root directly outside of Ansible with the same error.

unable to recognize "https://raw.githubusercontent.com/coreos/flannel/a70459be0084506e4ec919aa1c114638878db11b/Documentation/kube-flannel.yml": Get http://localhost:8080/api?timeout=32s: dial tcp [::1]:8080: connect: connection refused