blogbyAndrew

CNI là gì?

Kubernetes LogoCNCF Logo

CNI (Container Network Interface) là một dự án của Cloud Native Computing Foundation (CNCF) bao gồm specification và libraries để cấu hình network interfaces trong Linux containers. CNI chỉ tập trung vào hai việc:

CNI không quản lý DNS, service discovery, hay load balancing - đó là việc của các thành phần khác trong Kubernetes.

CNI Architecture - Orchestrator, CNI Library, Plugins

Kubernetes Network Model

Kubernetes đặt ra các yêu cầu cơ bản cho networking:

  1. Mỗi Pod có một IP duy nhất trong toàn cluster
  2. Pod trên mọi node có thể giao tiếp với nhau KHÔNG cần NAT
  3. Agents trên node (kubelet, system daemons) có thể giao tiếp với tất cả Pod trên node đó

Nghe đơn giản, nhưng triển khai thực tế rất khác nhau tùy cloud provider. Hãy xem GKE và AWS EKS giải quyết bài toán này như thế nào.

Kubernetes Node Network Namespaces - veth pairs kết nối Pod namespaces với host

GKE: Routes-Based vs VPC-Native

Google Kubernetes Engine cung cấp hai chế độ networking:

Routes-Based Mode

GKE Routes-Based Mode - Custom static routes giữa các node

Đặc điểm:

Networking bên trong node: Pods có network namespace riêng biệt. Node kết nối với Pod qua veth pair, tất cả veth interfaces kết nối vào bridge cbr0 (Layer 2).

GKE Node Internal Networking - veth pairs, cbr0 bridge, eth0

VPC-Native Mode (Khuyến nghị)

GKE VPC-Native Mode - Alias IP ranges per node

VPC-Native sử dụng Alias IP Ranges - một network interface có thể có nhiều IP ranges. Ưu điểm so với Routes-Based:

Routes-BasedVPC-Native
Pod IP routable trong VPCKhông
Firewall granularityPer-nodePer-pod range
Cần custom routesKhông
On-premises accessPhức tạpQua VPN/Interconnect
NEG supportKhông

VPC-Native mode không có nhược điểm so với Routes-Based. Google khuyến nghị luôn sử dụng VPC-Native.

AWS EKS: VPC CNI

AWS EKS

AWS EKS sử dụng Amazon VPC CNI - cách tiếp cận hoàn toàn khác: mỗi Pod nhận một IP thực từ VPC subnet.

Kiến trúc

AWS VPC CNI Architecture - kubelet, VPC CNI, L-IPAM, ENIs

Hai thành phần chính

1. CNI Binary - chạy trên root filesystem của node:

2. ipamd (IP Address Management Daemon) - chạy liên tục trên mỗi node:

Pod networking bên trong node

text
Pod Network Namespace:
+--------------------------------------+
|  eth0: 172.31.23.163/32              |
|                                      |
|  Route table:                        |
|  default via 169.254.1.1 dev eth0    |
|  169.254.1.1 dev eth0 scope link     |
|                                      |
|  ARP:                                |
|  169.254.1.1 -> MAC(veth_host_side)  |
+--------------------------------------+

Nhận ra pattern này chưa? Đây chính xác là kỹ thuật Secondary IP với Link-Local Gateway mà chúng ta đã thực hành trong bài Container Networking from Scratch!

Giới hạn

Số Pod trên mỗi node bị giới hạn bởi instance type:

text
Max Pods = (Số ENI tối đa) x (Số IP mỗi ENI - 1)

Ví dụ t3.medium:
  3 ENIs x (6 IPs - 1) = 15 Pods

Ví dụ m5.xlarge:
  4 ENIs x (15 IPs - 1) = 56 Pods

Security Groups cho Pods

GKE: Firewall Rules

GKE sử dụng VPC Firewall Rules với các thành phần:

Thành phầnMô tả
NetworkVPC network
Priority0-65535 (thấp hơn = ưu tiên cao hơn)
DirectionIngress / Egress
ActionAllow / Deny
TargetAll instances / Tags / Service accounts
SourceIP ranges / Service accounts / Tags
DestinationIP ranges (v4/v6)
ProtocolTCP / UDP / ICMP / ...
PortsSpecific ports hoặc all

Với VPC-Native mode, firewall rules có thể target specific pod IP ranges thay vì toàn bộ node - giúp security granular hơn nhiều.

AWS EKS: Security Groups for Pods

AWS cho phép gán security group riêng cho từng Pod (không chỉ cho node). Cơ chế này sử dụng trunk ENI:

AWS Security Groups for Pods - Primary ENI, Secondary ENI, Trunk ENI với branch ENIs

Kích hoạt bằng cách set ENABLE_POD_ENI=true trên VPC CNI, sau đó tạo SecurityGroupPolicy CRD:

yaml
apiVersion: vpcresources.k8s.aws/v1beta1
kind: SecurityGroupPolicy
metadata:
  name: my-security-group-policy
  namespace: my-namespace
spec:
  podSelector:
    matchLabels:
      role: my-role
  securityGroups:
    groupIds:
      - sg-0123456789abcdef0

Mỗi Pod với label matching sẽ nhận ENI riêng với security group chỉ định, thay vì dùng chung ENI với node.

So sánh các CNI phổ biến

CiliumFlannel
FlannelCalicoCiliumAWS VPC CNI
Data planeVXLAN / host-gwiptables / eBPFeBPFVPC native
Network PolicyKhôngCó (L3-L7)Security Groups
IPAMhost-localCalico IPAMCilium IPAMENI-based
EncryptionKhôngWireGuardWireGuard / IPsecVPC encryption
ObservabilityCơ bảnCơ bảnHubble (rất mạnh)VPC Flow Logs
ComplexityThấpTrung bìnhCaoThấp (managed)
Use caseDev/testProduction generalProduction advancedAWS only

CNI Performance Benchmarking

Khi chọn CNI, performance là yếu tố quan trọng. Methodology benchmark bao gồm:

  1. Preparation - Setup nodes, Kubernetes, CNI
  2. Idle Performance - CPU/memory overhead sau khi cài CNI
  3. Pod-to-Pod TCP Bandwidth - Single stream và multiple streams (8 threads)
  4. Pod-to-Pod UDP Bandwidth - Single stream và multiple streams
  5. Pod-to-Service TCP/UDP Bandwidth - Qua kube-proxy (iptables vs IPVS)

Các cấu hình thường được so sánh:

Tham khảo chi tiết: Benchmark Kubernetes Network Plugins over 40Gbit/s (2024)

Tổng kết

Cloud / PlatformCNI ApproachĐặc điểm
GKE (Routes-Based)Custom static routes + cbr0 bridgeLegacy, Pod IP không routable trong VPC
GKE (VPC-Native)Alias IP rangesRecommended, Pod IP natively routable
AWS EKSVPC CNI + ENI + Secondary IPsPod nhận IP thực từ VPC
AWS EKS (SG for Pods)Trunk ENI + VLANPer-pod security groups
Self-managed (Flannel)VXLAN overlayĐơn giản, dev/test
Self-managed (Calico)iptables / eBPFNetwork policy, production
Self-managed (Cilium)eBPF nativeL7 policy, Hubble observability

Tiếp theo

Tài nguyên tham khảo