My pods doesnt have internet access in k8s

I have created a cluster in k8s, but the pods I created is not having internet access.

I have created the cluster in a fresh ubuntu 18.04 machine using kubeadm, with flannel network plugin. I using docker as the container runtime.

Below is the commands which I used to install docker, kubeadm, kubelet and kubectl.
and also the steps to create cluster.

sudo update-alternatives --set iptables /usr/sbin/iptables-legacy
sudo update-alternatives --set ip6tables /usr/sbin/ip6tables-legacy
sudo update-alternatives --set arptables /usr/sbin/arptables-legacy
sudo update-alternatives --set ebtables /usr/sbin/ebtables-legacy

sudo apt-get update -y && sudo apt-get install 
  apt-transport-https ca-certificates curl software-properties-common openssh-server -y

### Add Docker’s official GPG key
curl -fsSL https://download.docker.com/linux/ubuntu/gpg |sudo  apt-key add -

### Add Docker apt repository.
sudo add-apt-repository 
  "deb [arch=amd64] https://download.docker.com/linux/ubuntu 
  $(lsb_release -cs) 
  stable"

## Install Docker CE.
sudo apt-get update && sudo  apt-get install 
  containerd.io=1.2.10-3 
  docker-ce=5:19.03.4~3-0~ubuntu-$(lsb_release -cs) 
  docker-ce-cli=5:19.03.4~3-0~ubuntu-$(lsb_release -cs)
cat  <<EOF | sudo tee /etc/docker/daemon.json
{
  "exec-opts": ["native.cgroupdriver=systemd"],
  "insecure-registries": ["gopirengaraj:5001"],
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "100m"
  },
  "storage-driver": "overlay2"
}
EOF

sudo mkdir -p /etc/systemd/system/docker.service.d

# Restart docker.
sudo systemctl daemon-reload
sudo systemctl restart docker

sudo apt-get update && sudo apt-get install -y apt-transport-https curl
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
cat <<EOF | sudo tee /etc/apt/sources.list.d/kubernetes.list
deb https://apt.kubernetes.io/ kubernetes-xenial main
EOF
sudo apt-get update
sudo apt-get install -y kubelet kubeadm kubectl
sudo apt-mark hold kubelet kubeadm kubectl

After installation, I used the below steps to create Kubernetes cluster

sudo kubeadm init --pod-network-cidr=10.240.0.0/16
mkdir -p $HOME/.kube
sudo cp  /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/2140ac876ef134e0ed5af15c65e414cf26827915/Documentation/kube-flannel.yml
kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.0-beta6/aio/deploy/recommended.yaml
kubectl apply -f Dashboard/dashboard-adminuser.yaml
kubectl apply -f Dashboard/cluster-role-binding.yml
kubectl -n kubernetes-dashboard describe secret $(kubectl -n kubernetes-dashboard get secret | grep admin-user | awk '{print $1}')

Cluster is created successfully, able to access the dashboard and able to create containers.
but the containers in the pods don’t have internet access.

My core DNS pod is throwing the below errors.

[ERROR] plugin/errors: 2 5097456227146284065.2749918219024011569.
HINFO: unreachable backend: read udp 192.168.1.5:35245->103.8.44.5:53:
i/o timeout [ERROR] plugin/errors: 2
5097456227146284065.2749918219024011569. HINFO: unreachable backend: read udp 192.168.1.5:43704->8.8.8.8:53: i/o timeout [ERROR] plugin/errors: 2 5097456227146284065.2749918219024011569. HINFO:
unreachable backend: read udp 192.168.1.5:36364->103.8.44.5:53: i/o
timeout [ERROR] plugin/errors: 2
5097456227146284065.2749918219024011569. HINFO: unreachable backend: read udp 192.168.1.5:46141->103.8.44.5:53: i/o timeout [ERROR] plugin/errors: 2 5097456227146284065.2749918219024011569. HINFO:
unreachable backend: read udp 192.168.1.5:50167->103.8.44.5:53: i/o
timeout [ERROR] plugin/errors: 2
5097456227146284065.2749918219024011569. HINFO: unreachable backend: read udp 192.168.1.5:34083->8.8.8.8:53: i/o timeout [ERROR] plugin/errors: 2 5097456227146284065.2749918219024011569. HINFO:
unreachable backend: read udp 192.168.1.5:36951->8.8.8.8:53: i/o
timeout

I guess the problem is with the kubelet, which is not picking the right DNS entries from the host machine.

Source: StackOverflow