Cilium: Advanced CNI and network security for Kubernetes

Introduction
Cilium is an open-source project that provides networking, security, and observability for cloud-native environments such as Kubernetes clusters and other container orchestration platforms.
At the foundation of Cilium is a new Linux kernel technology called eBPF, which enables the dynamic insertion of powerful security visibility and control logic within Linux itself. Because eBPF runs inside the Linux kernel, Cilium security policies can be applied and updated without any changes to the application code or container configuration.
Cilium acts as a CNI (Container Network Interface) plugin for Kubernetes, but goes far beyond basic connectivity:
- Networking: It provides a highly scalable networking plane, supporting multi-cluster connectivity (Cluster Mesh) and replacing
kube-proxyfor service load balancing. - Security: It implements identity-aware network policies (L3-L7) that are decoupled from network addressing, allowing for more flexible and secure communication controls.
- Observability: Through its component Hubble, it offers deep visibility into network traffic, service dependencies, and operational metrics.
Cilium's performance advantage comes from its use of eBPF to bypass the legacy iptables-based networking stack traditionally used in Kubernetes.
- Efficient Lookups: Unlike
iptableswhich uses a linear list of rules (O(n) complexity), Cilium uses eBPF maps (hash tables) which provide O(1) lookups. This means performance remains stable even as the number of services and rules grows massively. - Reduced Overhead: eBPF programs run directly in the kernel, minimizing the overhead of context switching between user space and kernel space.
- Direct Routing: Cilium can perform more efficient routing and load balancing decisions earlier in the packet processing path.
Installation
We can install Cilium easily with Helm:
Configuration
Let's create a custom configuration to support:
- Load Balancing Layer 7 with Envoy
- Gateway API
- Hubble
- L2 announcements
- Hubble UI
We apply the configuration with:
Layer 2 Announcements and IPAM
L2 Announcements makes services visible and reachable on the local area network. This is primarily for on-premises deployments without BGP, serving as a solid alternative to MetalLB.
Warning
L2 announcements are not stable yet. I encountered many various issues, IP unreachable, recover requiring a restart of cilium, etc... DO NOT USE IN PRODUCTION YET
When we use this feature, it responds to ARP queries for ExternalIPs and/or LoadBalancer IPs. These IPs are Virtual IPs (not installed on network devices) on multiple nodes. One node at a time will respond to the ARP queries with its MAC address, performing load balancing as a north/south load balancer.
The advantage over NodePort services is that each service can use a unique IP, allowing multiple services to use the same port numbers. With L2 announcements, if a node goes down, the service VIP simply migrates to another node and continues to work.
We can control the IP pool with LoadBalancer IP Address Management (IPAM):
Then create a layer 2 configuration with the interfaces we want IPs to be announced on (from the hosts network interfaces):
In the Helm chart values, we need to have the following configuration (same as the values-overrides.yaml above):
Then apply the configuration:
Now if we deploy a service with as a load balancer, it will use the IP pool we defined above:
Once deployed, we can check the service with:
Note
ICMP is not supported by Cilium L2 announcements. You have to use arpping to check if the IP is reachable or use netcat to check if the port is open.
Network Policies
Cilium implements Network Policies using CiliumNetworkPolicy resources. Cilium Network Policies are more permissive than Kubernetes Network Policies, as they can apply to all pods in a cluster, not just those in a namespace. It allows you to fine grain filter traffic to and from pods.
Here is an example on a Gateway API, to allow only local traffic to a service:
Tip
If you're not familiar with NetworkPolicies and need assistance, you can use the Online Network Policy Editor to generate a policy based on your requirements. You can generate native Kubernetes NetworkPolicy or CiliumNetworkPolicy objects.
Troubleshooting
L2 Announcements
On the official site, you'll find useful information. It's a complete procedure.