# Cilium Network Policy 實戰:用 eBPF 幫你 K8s 微服務建立零信任網絡隔離
做咗 Kubernetes 一段時間嘅你,係咪仲用緊原生 NetworkPolicy?老實講,Kubernetes 內置嘅 NetworkPolicy 真係好雞 — 只支援 L3/L4(IP/Port)層級過濾,冇 DNS 過濾、冇 L7 HTTP 規則、冇加密、更加冇 Observability。今次同大家介紹 佢係一個用 eBPF 技術嘅 CNI plugin,可以幫你做到真正嘅零信任微服務隔離。
👉 延伸閱讀:Linux Server 安全加固 6 步搞掂 — Defense in Depth 實戰指南
## Cilium 係咩嚟㗎?
佢係一個開源 CNI(Container Network Interface)plugin,背後用 eBPF(extended Berkeley Packet Filter)技術直接喺 Linux Kernel 層面處理網絡流量。即係話,佢唔使行 sidecar proxy,唔使 iptables 逐條 rule 慢慢行,效率快幾十倍。最重要嘅係,佢支援 L7 Network Policy — 即係你可以限制某個 Pod 淨係可以 `GET /api/users`,唔俾佢 `POST /api/admin`。
Network Policy 有三種模式:
1. **Kubernetes NetworkPolicy** — 基本 L3/L4 規則
2. **CNP (CNP)** — 擴展版,支援 L7、DNS、ICMP
3. **CCNP (CCNP)** — 全 Cluster 層級
## Cilium 安裝 Step-by-Step
首先,你要有個 Kubernetes Cluster(我用 k3s 示範)。安裝方法好簡單,用 Helm:
# 加 Cilium Helm repo
helm repo add cilium https://helm.cilium.io/
helm repo update
# 安裝 Cilium(replace kube-proxy 模式)
helm install cilium cilium/cilium \
--namespace kube-system \
--set kubeProxyReplacement=true \
--set k8sServiceHost=YOUR_API_SERVER_IP \
--set k8sServicePort=6443
安裝完之後,verify 吓 Cilium 係咪行緊:
# Check Pod status
kubectl -n kube-system get pods -l k8s-app=cilium
# Run Connectivity test
cilium connectivity test
# Check Status check
cilium status
## CNP 實戰:L7 HTTP 過濾
以下係一個 CNP 範例 — 淨係容許 `frontend` pod 用 `GET` method 去 `backend` pod 嘅 `/api/products` endpoint,其他全部 block:
apiVersion: "cilium.io/v2"
kind: CNP
metadata:
name: "frontend-to-backend-l7"
spec:
endpointSelector:
matchLabels:
app: backend
ingress:
- fromEndpoints:
- matchLabels:
app: frontend
toPorts:
- ports:
- port: "8080"
protocol: TCP
rules:
http:
- method: "GET"
path: "/api/products"
呢個 CNP 嘅威力在於:就算 attacker 拎到 `frontend` pod shell,佢都冇辦法用 `POST` 或者 `DELETE` 去打 `backend`,因為 eBPF 喺 Kernel 層面直接 drop 咗唔符合 L7 rule 嘅 packet。唔使 sidecar,唔使 application code 改嘢。
## Cilium DNS 層級安全過濾
另一個獨有功能係 DNS-aware Network Policy。你可以限制 Pod 淨係可以 resolve 特定 domain:
apiVersion: "cilium.io/v2"
kind: CNP
metadata:
name: "restrict-dns"
spec:
endpointSelector:
matchLabels:
app: payment-svc
egress:
- toEndpoints:
- matchLabels:
"k8s:io.kubernetes.pod.namespace": kube-system
"k8s:k8s-app": kube-dns
toPorts:
- ports:
- port: "53"
protocol: UDP
rules:
dns:
- matchPattern: "*.stripe.com"
- matchPattern: "*.paypal.com"
呢個 policy 確保 `payment-svc` Pod 淨係可以 resolve Stripe 同 PayPal 嘅 domain,就算 attacker try 去 inject 惡意 DNS query 去 C2 server 都唔會成功。呢個設計真係屈機。
## Cilium Hubble Observability
仲內置 Hubble — 一個即時流量可觀測性工具,類似「K8s 版 Wireshark」:
# 啟用 Hubble:
helm upgrade cilium cilium/cilium \
--namespace kube-system \
--reuse-values \
--set hubble.relay.enabled=true \
--set hubble.ui.enabled=true
# Port-forward Hubble UI
kubectl -n kube-system port-forward svc/hubble-ui 12000:80
# CLI observe traffic flows
hubble observe --namespace production
Hubble 可以 show 到邊個 Pod 同邊個 Pod 傾偈、用咩 protocol、有冇俾 policy drop,對於 troubleshooting 同 audit 超級有用。
## Cilium vs Calico vs K8s 原生 NetworkPolicy
| 功能 | Cilium | Calico | K8s 原生 |
|——|——–|——–|———-|
| L3/L4 過濾 | ✅ | ✅ | ✅ |
| L7 HTTP 過濾 | ✅ | ❌ | ❌ |
| DNS 過濾 | ✅ | ❌ | ❌ |
| eBPF 加速 | ✅ | ✅ (慢啲) | ❌ |
| 流量加密 | ✅ (WireGuard) | ✅ | ❌ |
| Observability | Hubble | ❌ | ❌ |
睇完呢個表,如果仲用緊原生 NetworkPolicy,真係即刻轉 Cilium。
## 總結
eBPF 技術 + L7 Network Policy + DNS filtering + Hubble observability,係目前 Kubernetes 網絡安全最強組合。特別係喺零信任架構入面,呢個 CNI plugin 可以幫你做到真正嘅 micro-segmentation — 唔係淨係 IP/Port level,而係深入到 HTTP method、path、甚至 domain name level。
想試玩嘅,建議用 k3s 或者 kind 起個 local lab 試吓,安裝過程 5 分鐘搞掂。有咩問題歡迎留言一齊討論!
> 📎 **參考資料:** Cilium 官方文件 — eBPF-based Networking, Observability, Security | Kubernetes Network Policies 官方指南
> 📌 **延伸閱讀:** Falco + eBPF 容器安全監控實戰 | Docker安全掃描懶人包 | Linux Kernel 漏洞防護實戰
#Cilium #Kubernetes #eBPF #NetworkPolicy #CloudNative #零信任 #DevSecOps



