Configure Kubernetes Network with Flannel

Configure Kubernetes Network with Flannel

Introduction

Network overlays in Kubernetes clusters are used to address containers, pods, services and external client connectivity.  Overlays are software components that decouple the physical infrastructure from networking services. Overlays encapsulate a packet-within-a-packet to achieve connectivity and routing. An overlay is a virtual network serving on the underlying network or underlay. An underlay is a physical infrastructure supporting connectivity.

The Kubernetes Network Model is that every pod gets its own IP address. You do not have to create link pods manually. Container-to-host port mapping is simplified. Pods are treated like Virtual Machines for port allocation, naming, service discovery, load balancing, application configuration, and migration. Pods can communicate with all nodes without Network Address Translation. And Agents (daemons or kubelets) on a node can communicate with all pods on that node.  

Flannel

Flannel is an open-source virtual network project managed by CoreOS network designed for Kubernetes. Each host in a flannel cluster runs an agent called flanneld. It assigns each host a subnet, which acts as the IP address pool for containers running on the host. Containers can then contact other containers directly, using their IP address. Flannel supports multiple backends for encapsulating packets. The recommended choice is Virtual Extensible LAN (VXLAN), which runs a Layer 2 network on top of a Layer 3 infrastructure. Flannel also supports host-gw, which maps direct routes between hosts in a manner similar to Calico.

Setup

We will be setting up our Kubernetes cluster network using the Flannel network add-on. And although the network plug-in is not provided out of the box, it is required for your cluster to run properly. So, for the pods to communicate with each other, a pod network add-on must be installed.

Kubernetes supports several different network add-on solutions. But for the purpose of this article, we will be using flannel. Moving on to installing the flannel network add-on the first thing that we need to do is there's a sysctl value that we need to set up on all our servers, including master node:

echo "net.bridge.bridge-nf-call-iptables=1" | sudo tee -a /etc/sysctl.conf

This will permanently set that value, which will remain set even if we restart the server.  

In order to apply that change which we just added to the sysctl.conf file, we have to run the following command:

sudo sysctl -p
-p:Load sysctl settings from the file specified or /etc/sysctl.conf if none is given

And we need to do this on all of our cluster nodes.

Moving forward,  we will install flannel using the yml template that's provided by CoreOS and their repositories on GitHub. This template is also going to create several different objects allowing Flannel to run successfully. And we are going to be doing this on our master node which is an important step. Make sure that you don't accidentally try to install it on one of your cluster nodes. Remember to run it just on the Kubernetes master:

kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/bc79dd1505b0c8681ece4de4c0d86c5cd2643275/Documentation/kube-flannel.yml
kubectl apply -f: Apply a configuration change to a resource from a file or stdin.

It is just a yml file which describes the objects required to be installed in a cluster to set up Flannel and get it working. This will also create a whole bunch of objects that are specified by that yml file. So, at this point, that's all! Flannel is installed and everything should be up and running.

You can see from the output that there are several objects that are created here and to verify this installation please run the following:

kubectl get nodes

And we see that our master server, as well as our cluster nodes, are in the Ready state. Earlier those were not ready as networking was not installed then. This helps us validate that we have set up our cluster network correctly.

All the back end system pods run in the kube-system namespace, and that includes our Flannel pods. Another way to make sure that the actual Flannel pods are up and running at this point, we have to run the following:

kubectl get pods -n kube-system

And you will see that we have our kube-flannel-ds-* pods running. So this validates that our cluster network is configured correctly.

At this point, we have set up a working and functional Kubernetes cluster.

Conclusion

Kubernetes supports a variety of different network solutions and plugins which can implement Kubernetes networking in a variety of different environments. As we see, Flannel is a networking solution that perfectly meets the functionality of the Kubernetes networking model, and it offers a simple networking model that sets up an environment which is suitable for most users and use cases when you only need the basics.