/ cloud

Kubernetes From Scratch Part 2 – Networking

Kubernetes From Scratch Part 2 – Networking

In our last blog post, we created a pseudo cluster to show how Kubernetes works inside. Today we are going to add a second node and make sure the cluster utilizes it.

SECOND NODE

What about having more than one node? What if we would like to schedule pods on 2 nodes? It is as simple as running kubelet on another node and making sure it connects to our API Server.

First of all, we assume that we have the first node from our previous blog post, with 10.135.53.41 internal IP, running API Server, etcd and nginx deployment.

Now let’s create a second one, ssh to it and run kubelet there.

now on node1, we can check if the node has been recognized:

This is how our nodes look like now:

1

Next, let’s scale up our nginx deployment:

we can see that they are scheduled on both nodes.

Wait, can you see that pods have duplicated IP addresses? This is because we don’t have a way to manage IP address for pods among all nodes, this also means that there is no communication between pods located on different nodes.
To fix that we have to introduce another cluster component – network fabric.

KUBERNETES NETWORKING USING FLANNEL

Kubernetes makes specific assumptions about networking in the cluster:

  • pods can communicate with each other by using unique pod’s IP address
  • nodes can communicate with pods using unique pod’s IP address
  • the IP that a container sees itself as is the same IP that others see it as

Kubernetes assumes that each pod and service in a cluster has a unique IP address and can communicate with other pods using their IP addresses. To achieve that we need a way to assign subnet of IP address for each node and ask Docker to use it when spawning containers, then we have to establish a non-NAT communication between these IP address. There is a lot of ways to do that, here we are going to focus on Flannel.

Flannel is one of the easiest ways to achieve these assumptions. Basically, Flannel runs as an agent on each node and is responsible for allocating a subnet for that node out of configured address space. That subnet is used by docker to obtain IP addresses for pods. Each subnet together with node’s IP address is stored in etcd and is readable by all agents. This allows flannel to obtain node location for given pod’s IP and forward traffic to that node.

This is how our networking will look like, it also shows how flannel works in the big picture.

2

Applying what we’ve just learned, now we can run flannel on our nodes. The tricky part is to configure docker to use flannel. This is what we are going to do:

  • run flannel on both nodes pointing it to our etcd
  • use mk-docker-opts.sh shipped with flannel. It generates script file with docker environment variables. The file is sourced by docker.
  • insert initial flannel configuration to etcd using etcdctl

and now we can check pods’ IP addresses and try to ping them on different nodes:

Sweet, we have a pod to pod communication and this is how our nodes look like now:

3-1

LOAD BALANCING BETWEEN NODES

So now we have both nodes fully capable of running pods, what about receiving traffic?
Currently, we accept traffic only on the first node, it will be forwarded to pods on the second node (by flannel) but this is not high availability solution – the first node is single a point of failure.
To solve that we should install Kube Proxy on all worker nodes, after doing that we can add an icing on our cake – DigitalOcean Load Balancer and balance ingress between nodes.

Everything works, we could even assume that we have production ready cluster 😀 but of course, we are far from that.

This is how our cluster looks now:

4-1

WRAPPING UP

In this blog post, we have learned how nodes in the cluster communicate together and how pods are exposed to the outer world through services. There are more aspects of Kubernetes that we need to cover before we can say that our cluster is production ready. We are going to cover them in future blog posts, so stay tuned!

Kubernetes From Scratch Part 2 – Networking
Share this