Unable to install a Pod network add-on due to refused connection to localhost:8080
I'm trying to create a kubernetes cluster (my first :D) using kubeadm under the control plane node - a VM Centos7-.
Environment:
- kubeadm version : v1.21.1
- Kubernetes version: v1.21.1
- VM Centos 7 (2 CPU/2 G RAM/20G memory)
- VMWare workstation : 16
- Kernel : Linux 3.10.0-1160.el7.x86_64
- Docker : Community 19.03.8
- Pod network add-on : Flannel
Here are the followed steps:
1- DISABLE SWAP:
sudo sed -i '/swap/d' /etc/fstab
sudo swapoff -a
To check :
cat /proc/meminfo | grep 'SwapTotal'
--> Returns 0 kB
2- DISABLE SELINUX: SELinux in permissive mode (effectively disabling it)
sudo setenforce 0
sudo sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config
3- IPTABLES: Letting iptables see bridged traffic + load br_netfilter module
sudo modprobe br_netfilter
cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
br_netfilter
EOF
cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
EOF
sudo sysctl --system
4- FIREWALL : Enable needed ports
sudo firewall-cmd --permanent --add-port=6443/tcp
sudo firewall-cmd --permanent --add-port=2379-2380/tcp
sudo firewall-cmd --permanent --add-port=10250/tcp
sudo firewall-cmd --permanent --add-port=10251/tcp
sudo firewall-cmd --permanent --add-port=10252/tcp
sudo firewall-cmd --permanent --add-port=8285/udp
sudo firewall-cmd --reload
5- HOSTNAME + HOSTS
To get the IP, I took the first one given by hostname -I
(gave two IPs)
sudo hostnamectl set-hostname master-node
sudo vi /etc/hosts
192.168.93.131 master-node
6- DOCKER:
sudo yum install -y yum-utils
sudo yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
sudo yum install -y containerd.io-1.2.13 docker-ce-19.03.8 docker-ce-cli-19.03.8
sudo systemctl start docker
sudo systemctl enable docker
sudo systemctl restart docker
sudo docker run hello-world
-- cgroup
sudo mkdir /etc/docker
sudo mkdir -p /etc/systemd/system/docker.service.d
sudo tee /etc/docker/daemon.json <<EOF
{
"exec-opts": ["native.cgroupdriver=systemd"],
"log-driver": "json-file",
"log-opts": {
"max-size": "100m"
},
"storage-driver": "overlay2"
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
sudo systemctl enable docker
7- KUBERNETES:
cat <<EOF | sudo tee /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-\$basearch
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
exclude=kubelet kubeadm kubectl
EOF
sudo yum install -y kubelet kubeadm kubectl --disableexcludes=kubernetes
sudo reboot
sudo systemctl enable --now kubelet
sudo systemctl start kubelet
8- KUBEADM INIT:
sudo kubeadm init --pod-network-cidr=10.244.0.0/16 --control-plane-endpoint=master-node
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
All these steps worked fine and when I tried to install a Pod network add-on with the command
sudo kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
I got this error :
The connection to the server localhost:8080 was refused - did you specify the right host or port?
When I check the status with :
systemctl status docker
systemctl status kubelet
I am able to see that the servers are running.
I reset the cluster twice (kubeadm reset + remove the config file manually) and found the same problem.
I'm probably doing something wrong but I'm unable to figure it out.
Any help would be highly appreciated and thanks a lot in advance.
Solution 1:
You copied the kubeconfig into $HOME
but then used sudo kubectl
which (because it is running as root) would look in /root/.kube/config
; removing the unnecessary sudo
from kubectl apply
will then cause it to look in the current user's home directory, or you can be 100% explicit by specifying kubectl --kubeconfig $HOME/.kube/config apply
to ensure you and kubectl are on the same page