Kubernetes Networking — Part 1

Kubernetes Networking — Part 1 In this article, we’re going to explore the Kubernetes networking model in its fundamentals and how it was adapted for implementation in AKS and GKE. Initial Definitions Container Runtime Interface (CRI): It i

Source comparison and review notes
  • Automated pair-review warning (AI bilingual content review): Both sources overgeneralize CNI as the pod-to-pod communication standard and state pod-to-pod communication is without NAT; actual networking behavior depends on the selected CNI and network mode.

In this article, we’re going to explore the Kubernetes networking model in its fundamentals and how it was adapted for implementation in AKS and GKE.


Initial Definitions

Container Runtime Interface (CRI):

  • It is a plugin used in Kubernetes that enables Kubelet (a process that runs in each of the worker nodes and communicates with the Control Plan API) to use different Container Runtimes, such as Containerd.
  • CRI is the primary communication protocol between Kubelet and the Container Runtime.
  • The CRI is responsible for performing all isolation configuration tasks for each of the pods. These configurations are standardized.

Network Namespaces in the CRI:

  • One of the tasks performed by the CRI is the configuration of Network Namespaces on Linux. Network Namespaces are the mechanism by which we can create independent network environments within the same operating system.
  • Each namespace has its own network stack: interfaces, routing tables, firewall rules, etc.
  • In the context of the Container Runtime Interface (CRI), network namespaces are responsible for providing network isolation for containers.

Container Network Interface (CNI):

  • CNI is a specification for the standard of communication between pods. Also called the Pod Network.
  • These are Plugins that respect certain standards and can be implemented in the Kubernetes cluster as an Add-On.
  • CNI defines how network plugins should create and manage container networks, allowing flexibility for any compatible plugin to configure the network to the specific needs of the cluster.
  • CNI facilitates the connection between pods, assigning a unique IP address to each one and allowing communication between them, either within the same node or on different nodes in the cluster.
  • The Pod Network is Cluster-Wide.
  • Some classic plugins: Calico, Flannel, or Weave.

Networking Fundamentals in Kubernetes

Networking is a FUNDAMENTAL part of the design and implementation of any Kubernetes cluster.

There are 4 types of communication that we should focus on:


Container-to-Container

  • This is solved through the implementation of Pods as the minimum unit of Container deployment.
  • Kubernetes Clusters implement a Pod Network using a CNI Plugin such as Calico or Weave.
  • Containers within a Pod can communicate directly with each other through the localhost network interface, as they share the same IP address and network space. This means that if a container in one pod needs to access another, it can do so directly using localhost or 127.0.0.1, making this communication fast and efficient.

Pod-to-Pod

  • It also makes use of the Pod Network implemented through the CNI Plugin.
  • Each Pod has its Network Namespace shared by all the containers it runs. They communicate through localhost.
  • Being cluster-wide, Pod-to-Pod communication allows pods to communicate with each other, regardless of the node they are on.
  • Unique IPs are assigned to each pod; a range must be assigned to be used by the entire cluster.
  • The CNI Plugin is responsible for updating and replicating the routing tables for communication between the Pods on all nodes.
  • Communication is made WITHOUT NAT.
  • We can implement Network Policies for the restriction of these communications, which by default do not have any restrictions in the cluster.

Pod-to-Service

  • Pods are ephemeral: they can be destroyed, created, or modified as needed. They are not reliable for network communication.
  • This is why the concept of Services was born in Kubernetes, these allow you to provide an IP (long-lived) or hostname for several backend pods. Pods may change, but the service remains constant.

External-to-Service

  • For external communication, Ingress can be used, which allows services to be accessed from outside the cluster.
  • The Ingress enables L7 communication (application). It allows you to use the HTTP/S protocol-aware configuration mechanism that understands concepts such as URIs, hostnames, paths, among others. It allows mapping traffic in an “intelligent” way to different backends.
  • There are different Ingress Controllers that we can use such as NGINX, Traefik, or some specific Cloud Providers such as HTTPS Load Balancers in GCP or App Gateway in Azure.
  • There is also a more simplified and less configurable mechanism that is the Service Type: LoadBalancer that is normally integrated with an L4 LB (network) in Cloud Providers.

IP Assignment

Within the Cluster, IPs are assigned as follows:

  • The CNI Plugin assigns the IPs of the Pods.
  • Kube-apiserver assigns the IPs of the Services.
  • Kubelet or the cloud-controller-manager assigns the IPs of the Nodes.

Kube-Proxy

  • It is an optional component since its work can be replaced by the CNI Plugin that is implemented.
  • Its main task is to run on each Worker Node and implement part of the Concept of Services in Kubernetes.
  • Maintains network rules on nodes that allow communication to pods from sessions in and out of the cluster.

Using CIDR Pods Per Node

As we talked about earlier, we may or may not use a specific range of Pods, i.e., a CIDR assigned especially for Pods. This value is extremely important in the planning of our cluster since it will be the one that determines the number of nodes that our Node Pool or cluster can have.

Number of Pods per Node

This value determines the size of the CIDR Blocks that are assigned to each of the nodes in GKE. The pods are then assigned from this range in each of the nodes.

This configuration has a default value at the cluster level, but this value can be overwritten at the level of each Node Pool in the cluster.

That is, the creation of nodes is limited to the number of Pod IPs available in the Pod Address Range.

The Max Pods per Node setting is immutable after being assigned to a Node Pool. That is why it is very important to take this into account in our planning of each of the groups of nodes.


Example:

If you set the maximum number of pods per node to 110 and the secondary IP address range for pods in the cluster to /21, Kubernetes will assign a range of IPs /24 to each node. This means that each node will have enough space to assign up to 110 IP addresses to the pods it hosts.

  • In this case, a /24 range has 256 possible IP addresses (2^8), while the /21 range for the cluster is wider and contains multiple /24 subranges.
  • The maximum number of nodes allowed is calculated by subtracting the bits of difference between the two ranges:
    2^(24–21) = 2³ = 8.
  • Therefore, with this configuration, the cluster can have a maximum of 8 nodes.

If we set the maximum number of pods to 8 per node and configure the secondary IP address range for pods in /21, Kubernetes will assign a /28 range to each node.

  • A /28 range has 16 possible IP addresses (2^4), enough to accommodate up to 8 pods on each node.
  • The maximum number of nodes in the cluster is calculated as:
    2^(28–21) = 2⁷ = 128.
  • Thus, with this configuration, the cluster can support up to 128 nodes.

In the next post, we’re going to look at comparing the network models in GKE, AKS, and EKS.

Mateo Di Loreto

If you like what i do and want to support my content you can Buy Me a Coffe

Thanks for reading. The learning continues in the next article.