Networking is a central part of Kubernetes, but it can be challenging to understand exactly how it is expected to work and how it actually works. Overall, there are 4 distinct networking challenges to address:
It is a Cloud Native Computing Foundation project, consists of a specification and libraries for writing plugins to configure network interfaces in Linux containers, along with a number of supported plugins. CNI concerns itself only with network connectivity of containers and removing allocated resources when the container is deleted. Because of this focus, CNI has a wide range of support and the specification is simple to implement.
In other word, CNI plugin is responsible for inserting a network interface into the container network namespace (e.g., one end of a virtual ethernet (veth) pair) and making any necessary changes on the host (e.g., attaching the other end of the veth into a bridge). It then assigns an IP address to the interface and sets up the routes consistent with the IP Address Management section by invoking the appropriate IP Address Management (IPAM) plugin.
Flow of how it actually works:
note: CNI is called twice by K8s (kubelet) to set up loopback and eth0 interfaces for a pod.
- Basic commands are: ADD, DEL, CHECK and VERSION
- Plugins are executables
- Spawned by the runtime when network operations are desired
- Fed JSON configuration via stdin
- Also fed container-specific data via stdin
- Report structured result via stdout
They are an abstract way to expose an application running on a set of Pods as a network service, and a policy by which to access them through each other (This will mostly be used in microservice-based architectures). The set of Pods targeted by a Service is usually determined by a selector.
There are three main types of communications between Kubernetes services:
Every node in a Kubernetes cluster runs a kube-proxy and It is responsible for implementing a form of virtual IP for Services of type other than ExternalName.
There are three kinds of service proxy:
clusterIP
(which is virtual) and port
. The rules redirect that traffic to the proxy port which proxies the backend Pod and if failed and would automatically retry with a different backend PodclusterIP
and port
, and redirect that traffic to one of the Service's backend sets. For each Endpoint object, it installs iptables rules which select a backend Pod but only backends that test out as healthy. The traffic is handled by Linux netfilter without the need to switch between userspace and the kernel space.netlink
interface to create IPVS rules accordingly and synchronizes IPVS rules with Kubernetes Services and Endpoints periodically. It uses a hash table as the underlying data structure and works in the kernel space.