This error is caused Kubeadm missing the environment variables it needs to run, and often happens after a restart of the master node.
To fix this issue, simply export the admin.conf on the master node.
sudo cp /etc/kubernetes/admin.conf $HOME sudo chown $(id -u):$(id -g) $HOME/admin.conf export KUBECONFIG=$HOME/admin.conf
Now run a “kubectl get” command and it should run without error:
kubectl get nodes
if that does not work, use below commands to disable swap.
The Kubelet service with not run unless swap is disabled on the all the master and worker nodes. Note that ‘swapoff -a’ immediately disables swap until server is reboot and swap is reinitialized with the ‘/etc/fstab’ configuration.
sudo -i swapoff -a # this disables swap immediately exit strace -eopenat kubectl get nodes
To avoid having to do this whenever the master node is restarted, add the command to your $HOME/.bashrc
echo "export KUBECONFIG=$HOME/admin.conf" > $HOME/.bashrc
Do this to permanently disable swap:
sudo sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab