How to Choose and Build the Perfect CNI Plugin for Kubernetes
This article explains what the Container Network Interface (CNI) is, how Kubernetes uses CNI plugins, guides the step‑by‑step configuration process, compares Overlay, Routing and Underlay implementations, and provides practical criteria for selecting or building a CNI plugin that fits specific environments and performance needs.
What Is CNI?
The Container Network Interface (CNI) is the standard API that Kubernetes uses to invoke network plugins. A CNI plugin implements a set of API calls that configure a pod’s network namespace, assigning IP addresses and setting up routing.
Using CNI in Kubernetes
Kubernetes reads CNI configuration files (typically located in /etc/cni/net.d) to determine which plugin to use. The basic workflow is:
Place a CNI configuration file (e.g., xxnet.conf) on each node.
Install the binary plugin referenced by the configuration.
When a pod is created, the kubelet invokes the plugin to set up the pod’s network.
After the plugin finishes, the pod’s network is ready.
Many plugins, such as Flannel, provide a one‑click kubectl apply manifest that installs both the configuration and the binary on every node.
CNI Implementation Modes
CNI plugins fall into three architectural categories:
Overlay : Pods receive IPs from a separate address space; inter‑node traffic is encapsulated in tunnels (e.g., VXLAN). This mode does not depend on the underlying network.
Routing : Pods and hosts have different subnets, but cross‑node traffic is routed directly without encapsulation. It requires the underlying network to provide L2 reachability.
Underlay : Pods share the same layer‑2 network as the host. Networking relies heavily on the physical infrastructure.
How to Choose the Right CNI Plugin
1. Environment Constraints
Virtualized environments (e.g., OpenStack) often restrict L2 connectivity, making Overlay plugins such as Flannel‑vxlan or Calico‑ipip the viable choice.
Bare‑metal clusters have fewer restrictions, allowing Underlay or Routing plugins (e.g., Cilium‑bgp, Flannel‑host‑gw, SR‑IOV).
Public clouds provide their own CNI solutions (e.g., Alibaba Cloud’s Terway) that are optimized for the cloud’s networking APIs.
2. Functional Requirements
Security : If you need NetworkPolicy enforcement, select plugins that support it (e.g., Calico, Weave).
External connectivity : For workloads that must communicate with VMs or physical machines, choose Underlay plugins or routing‑based solutions that expose pod IPs to the external network.
Service discovery & load balancing : Not all plugins expose pod IPs to kube‑proxy. Verify that the chosen plugin can work with Kubernetes Services if you rely on them.
3. Performance Requirements
Pod creation speed : Overlay and Routing modes are fast because they only manipulate kernel interfaces. Underlay may be slower due to additional physical network configuration.
Network throughput & latency : Overlay adds encapsulation overhead, which can hurt high‑performance workloads (e.g., ML, big data). Underlay or routing modes typically deliver better raw performance.
After filtering by environment, functionality, and performance, you should have a short list of candidate plugins.
Developing Your Own CNI Plugin
If existing plugins do not meet your needs, you can build a custom CNI plugin. A typical implementation consists of two parts:
A binary that configures the pod’s virtual Ethernet (veth) pair and assigns a unique IP address.
A daemon that runs on each node, learns all pod IPs, and establishes the necessary connectivity (e.g., creating overlay tunnels, programming routes, or updating OVS flow tables).
Step 1 – Attach a Network Interface to the Pod
Create a veth pair, placing one end in the pod’s network namespace and the other in the host’s namespace.
Allocate a unique IP address from a cluster‑wide CIDR, ensuring no collisions.
Configure the pod’s interface with the IP and set up the appropriate routing table.
On the host, add a route that forwards traffic for the pod’s IP to the host‑side veth endpoint.
Step 2 – Connect Pods Across Nodes
The CNI daemon watches the Kubernetes API server to learn every pod’s IP and node location.
It then creates the necessary cross‑node connectivity: overlay tunnels, VPC routes, or BGP sessions, depending on the chosen implementation mode.
Finally, it programs Linux routing tables, forwarding databases (FDB), or Open vSwitch flow tables so that traffic to any pod IP is correctly forwarded.
Conclusion
When building a Kubernetes cluster, select a CNI plugin that matches your environment’s constraints, functional needs, and performance goals. If no existing plugin satisfies those criteria, you can develop a custom plugin by implementing a binary for pod interface setup and a daemon for cross‑node networking.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Alibaba Cloud Native
We publish cloud-native tech news, curate in-depth content, host regular events and live streams, and share Alibaba product and user case studies. Join us to explore and share the cloud-native insights you need.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
