前言
在 Kubernetes 叢集中,網路安全是一個至關重要的議題。傳統的 CNI(Container Network Interface)插件雖然能夠提供基本的網路連接功能,但在面對現代化的安全需求時往往力有未逮。Cilium 作為一個基於 eBPF 技術的 CNI 插件,不僅提供了強大的網路功能,更在安全性、可觀測性和效能方面帶來了革命性的改進。
本文將深入探討 Cilium 的核心概念、安裝配置、網路政策、加密功能以及可觀測性等面向,幫助讀者全面了解如何利用 Cilium 來強化 Kubernetes 叢集的網路安全。
Cilium 概述與 eBPF
什麼是 Cilium?
Cilium 是一個開源的網路和安全解決方案,專為 Kubernetes 和其他容器編排平台設計。它利用 Linux 核心的 eBPF(extended Berkeley Packet Filter)技術,在核心層級提供高效能的網路、安全和可觀測性功能。
eBPF 技術簡介
eBPF 是一種革命性的技術,允許在 Linux 核心中執行沙箱化的程式,而無需修改核心原始碼或載入核心模組。這為網路處理帶來了以下優勢:
- 高效能:封包處理直接在核心空間完成,避免了使用者空間與核心空間之間的切換開銷
- 安全性:eBPF 程式在執行前會經過驗證器檢查,確保不會造成系統崩潰
- 靈活性:可以動態載入和卸載程式,無需重新啟動系統
- 可觀測性:能夠在不影響效能的情況下收集詳細的網路指標
Cilium 架構
Cilium 的架構主要包含以下元件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
| ┌─────────────────────────────────────────────────────────────┐
│ Kubernetes Cluster │
├─────────────────────────────────────────────────────────────┤
│ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │
│ │ Cilium │ │ Cilium │ │ Cilium │ │
│ │ Agent │ │ Agent │ │ Agent │ │
│ │ (DaemonSet)│ │ (DaemonSet)│ │ (DaemonSet)│ │
│ └──────┬──────┘ └──────┬──────┘ └──────┬──────┘ │
│ │ │ │ │
│ ┌──────┴──────┐ ┌──────┴──────┐ ┌──────┴──────┐ │
│ │ eBPF │ │ eBPF │ │ eBPF │ │
│ │ Programs │ │ Programs │ │ Programs │ │
│ └─────────────┘ └─────────────┘ └─────────────┘ │
├─────────────────────────────────────────────────────────────┤
│ Cilium Operator │
│ (Deployment) │
├─────────────────────────────────────────────────────────────┤
│ Hubble (可觀測性) │
│ ┌─────────────┬─────────────┐ │
│ │ Hubble Relay│ Hubble UI │ │
│ └─────────────┴─────────────┘ │
└─────────────────────────────────────────────────────────────┘
|
- Cilium Agent:以 DaemonSet 形式在每個節點上執行,負責管理 eBPF 程式和處理網路政策
- Cilium Operator:負責叢集層級的操作,如 IPAM 管理和垃圾回收
- Cilium CLI:命令列工具,用於管理和除錯 Cilium
- Hubble:可觀測性平台,提供網路流量可視化
安裝與設定
先決條件
在安裝 Cilium 之前,請確保您的環境符合以下要求:
- Kubernetes 1.16 或更高版本
- Linux 核心 4.19 或更高版本(建議 5.4+)
- 支援的容器運行時(containerd、CRI-O 等)
安裝 Cilium CLI
首先,安裝 Cilium CLI 工具:
1
2
3
4
5
6
7
8
| # 下載 Cilium CLI
CILIUM_CLI_VERSION=$(curl -s https://raw.githubusercontent.com/cilium/cilium-cli/main/stable.txt)
CLI_ARCH=amd64
if [ "$(uname -m)" = "aarch64" ]; then CLI_ARCH=arm64; fi
curl -L --fail --remote-name-all https://github.com/cilium/cilium-cli/releases/download/${CILIUM_CLI_VERSION}/cilium-linux-${CLI_ARCH}.tar.gz{,.sha256sum}
sha256sum --check cilium-linux-${CLI_ARCH}.tar.gz.sha256sum
sudo tar xzvfC cilium-linux-${CLI_ARCH}.tar.gz /usr/local/bin
rm cilium-linux-${CLI_ARCH}.tar.gz{,.sha256sum}
|
使用 Cilium CLI 安裝
最簡單的安裝方式是使用 Cilium CLI:
1
2
3
4
5
6
7
8
| # 基本安裝
cilium install
# 驗證安裝狀態
cilium status --wait
# 執行連接性測試
cilium connectivity test
|
使用 Helm 安裝
對於生產環境,建議使用 Helm 進行更精細的配置:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| # 新增 Cilium Helm repository
helm repo add cilium https://helm.cilium.io/
helm repo update
# 安裝 Cilium
helm install cilium cilium/cilium --version 1.15.0 \
--namespace kube-system \
--set ipam.mode=kubernetes \
--set kubeProxyReplacement=true \
--set hubble.enabled=true \
--set hubble.relay.enabled=true \
--set hubble.ui.enabled=true \
--set encryption.enabled=true \
--set encryption.type=wireguard
|
重要配置選項
以下是一些常用的配置選項:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
| # values.yaml 範例
ipam:
mode: kubernetes # 或 cluster-pool
kubeProxyReplacement: true # 取代 kube-proxy
hubble:
enabled: true
relay:
enabled: true
ui:
enabled: true
encryption:
enabled: true
type: wireguard # 或 ipsec
bpf:
masquerade: true
clockProbe: true
preallocateMaps: true
operator:
replicas: 2
loadBalancer:
mode: dsr # Direct Server Return
|
驗證安裝
安裝完成後,驗證 Cilium 是否正常運作:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
| # 檢查 Cilium pods 狀態
kubectl -n kube-system get pods -l k8s-app=cilium
# 檢查 Cilium 狀態
cilium status
# 輸出範例:
# /¯¯\
# /¯¯\__/¯¯\ Cilium: OK
# \__/¯¯\__/ Operator: OK
# /¯¯\__/¯¯\ Envoy DaemonSet: disabled
# \__/¯¯\__/ Hubble Relay: OK
# \__/ ClusterMesh: disabled
#
# Deployment cilium-operator Desired: 2, Ready: 2/2
# DaemonSet cilium Desired: 3, Ready: 3/3
# Deployment hubble-relay Desired: 1, Ready: 1/1
# Deployment hubble-ui Desired: 1, Ready: 1/1
|
網路政策
Cilium 支援標準的 Kubernetes NetworkPolicy,同時也提供了更強大的 CiliumNetworkPolicy(CNP)和 CiliumClusterwideNetworkPolicy(CCNP)。
Kubernetes NetworkPolicy 範例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
| apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: api-allow
namespace: production
spec:
podSelector:
matchLabels:
app: api-server
policyTypes:
- Ingress
- Egress
ingress:
- from:
- podSelector:
matchLabels:
role: frontend
ports:
- protocol: TCP
port: 8080
egress:
- to:
- podSelector:
matchLabels:
app: database
ports:
- protocol: TCP
port: 5432
|
CiliumNetworkPolicy 範例
CiliumNetworkPolicy 提供了更豐富的功能,包括 L7 政策、DNS 規則等:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
| apiVersion: cilium.io/v2
kind: CiliumNetworkPolicy
metadata:
name: secure-api-policy
namespace: production
spec:
endpointSelector:
matchLabels:
app: api-server
ingress:
- fromEndpoints:
- matchLabels:
role: frontend
toPorts:
- ports:
- port: "8080"
protocol: TCP
rules:
http:
- method: "GET"
path: "/api/v1/.*"
- method: "POST"
path: "/api/v1/data"
headers:
- 'Content-Type: application/json'
egress:
- toEndpoints:
- matchLabels:
app: database
toPorts:
- ports:
- port: "5432"
protocol: TCP
- toFQDNs:
- matchPattern: "*.googleapis.com"
toPorts:
- ports:
- port: "443"
protocol: TCP
|
基於身份的安全
Cilium 使用身份(Identity)來識別端點,而非傳統的 IP 位址。這提供了更可靠的安全控制:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| apiVersion: cilium.io/v2
kind: CiliumNetworkPolicy
metadata:
name: identity-based-policy
spec:
endpointSelector:
matchLabels:
app: backend
ingress:
- fromEndpoints:
- matchLabels:
io.kubernetes.pod.namespace: trusted-namespace
role: internal-service
- fromEntities:
- cluster # 允許叢集內部流量
|
預設拒絕政策
建議實施預設拒絕(Default Deny)政策:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
| apiVersion: cilium.io/v2
kind: CiliumClusterwideNetworkPolicy
metadata:
name: default-deny-all
spec:
endpointSelector: {}
ingress:
- fromEntities:
- cluster
egress:
- toEntities:
- cluster
- toEndpoints:
- matchLabels:
io.kubernetes.pod.namespace: kube-system
k8s-app: kube-dns
toPorts:
- ports:
- port: "53"
protocol: UDP
rules:
dns:
- matchPattern: "*"
|
L7 應用層政策
Cilium 的一大優勢是能夠在應用層(L7)進行流量控制,支援 HTTP、gRPC、Kafka 等協定。
HTTP 政策
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
| apiVersion: cilium.io/v2
kind: CiliumNetworkPolicy
metadata:
name: http-policy
namespace: production
spec:
endpointSelector:
matchLabels:
app: web-api
ingress:
- fromEndpoints:
- matchLabels:
app: frontend
toPorts:
- ports:
- port: "80"
protocol: TCP
rules:
http:
# 只允許特定的 HTTP 方法和路徑
- method: "GET"
path: "/api/public/.*"
- method: "GET"
path: "/health"
- method: "POST"
path: "/api/v1/users"
headers:
- 'X-API-Key: [a-zA-Z0-9]+'
- method: "PUT"
path: "/api/v1/users/[0-9]+"
- method: "DELETE"
path: "/api/v1/users/[0-9]+"
headers:
- 'Authorization: Bearer .*'
|
gRPC 政策
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
| apiVersion: cilium.io/v2
kind: CiliumNetworkPolicy
metadata:
name: grpc-policy
namespace: microservices
spec:
endpointSelector:
matchLabels:
app: grpc-service
ingress:
- fromEndpoints:
- matchLabels:
app: grpc-client
toPorts:
- ports:
- port: "50051"
protocol: TCP
rules:
http:
# gRPC 使用 HTTP/2
- method: "POST"
path: "/myservice.v1.UserService/GetUser"
- method: "POST"
path: "/myservice.v1.UserService/ListUsers"
# 拒絕刪除操作
# - method: "POST"
# path: "/myservice.v1.UserService/DeleteUser"
|
Kafka 政策
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
| apiVersion: cilium.io/v2
kind: CiliumNetworkPolicy
metadata:
name: kafka-policy
namespace: data-pipeline
spec:
endpointSelector:
matchLabels:
app: kafka-broker
ingress:
- fromEndpoints:
- matchLabels:
role: kafka-producer
toPorts:
- ports:
- port: "9092"
protocol: TCP
rules:
kafka:
- role: "produce"
topic: "events"
- role: "produce"
topic: "logs"
- fromEndpoints:
- matchLabels:
role: kafka-consumer
toPorts:
- ports:
- port: "9092"
protocol: TCP
rules:
kafka:
- role: "consume"
topic: "events"
clientID: "event-processor-.*"
|
DNS 政策
Cilium 可以基於 DNS 名稱控制出站流量:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
| apiVersion: cilium.io/v2
kind: CiliumNetworkPolicy
metadata:
name: dns-policy
namespace: production
spec:
endpointSelector:
matchLabels:
app: external-client
egress:
# 允許 DNS 查詢
- toEndpoints:
- matchLabels:
io.kubernetes.pod.namespace: kube-system
k8s-app: kube-dns
toPorts:
- ports:
- port: "53"
protocol: UDP
rules:
dns:
- matchPattern: "*.example.com"
- matchPattern: "api.github.com"
# 基於 FQDN 的出站規則
- toFQDNs:
- matchName: "api.example.com"
- matchName: "api.github.com"
- matchPattern: "*.storage.googleapis.com"
toPorts:
- ports:
- port: "443"
protocol: TCP
|
加密與 mTLS
Cilium 提供了透明的網路加密功能,支援 WireGuard 和 IPsec 兩種加密方式。
WireGuard 加密
WireGuard 是一種現代化的 VPN 協定,提供高效能的加密:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| # 啟用 WireGuard 加密
helm upgrade cilium cilium/cilium \
--namespace kube-system \
--reuse-values \
--set encryption.enabled=true \
--set encryption.type=wireguard
# 驗證加密狀態
cilium encrypt status
# 輸出範例:
# Encryption: Wireguard
# Keys in use: 1
# Max Seq. Number: 0x0/0xffffffff
|
檢查節點之間的加密狀態:
1
2
3
4
5
| # 查看 WireGuard 介面
kubectl -n kube-system exec -it ds/cilium -- cilium encrypt status
# 檢查加密統計
kubectl -n kube-system exec -it ds/cilium -- cilium bpf tunnel list
|
IPsec 加密
對於需要使用 IPsec 的環境:
1
2
3
4
5
6
7
8
9
10
11
| # 生成 IPsec 金鑰
kubectl create -n kube-system secret generic cilium-ipsec-keys \
--from-literal=keys="3 rfc4106(gcm(aes)) $(echo $(dd if=/dev/urandom count=20 bs=1 2> /dev/null | xxd -p -c 64)) 128"
# 啟用 IPsec
helm upgrade cilium cilium/cilium \
--namespace kube-system \
--reuse-values \
--set encryption.enabled=true \
--set encryption.type=ipsec \
--set encryption.ipsec.keyFile=/etc/ipsec/keys
|
透明 mTLS(Mutual TLS)
Cilium 與 SPIFFE/SPIRE 整合,提供工作負載身份和 mTLS:
1
2
3
4
5
6
| # 安裝時啟用 mTLS
helm upgrade cilium cilium/cilium \
--namespace kube-system \
--reuse-values \
--set authentication.mutual.spire.enabled=true \
--set authentication.mutual.spire.install.enabled=true
|
設定需要 mTLS 的政策:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
| apiVersion: cilium.io/v2
kind: CiliumNetworkPolicy
metadata:
name: mtls-required
namespace: secure-namespace
spec:
endpointSelector:
matchLabels:
app: secure-service
ingress:
- fromEndpoints:
- matchLabels:
app: trusted-client
authentication:
mode: required # 強制要求 mTLS
toPorts:
- ports:
- port: "443"
protocol: TCP
|
加密效能考量
1
2
3
4
5
6
7
8
9
10
11
12
| # 針對高流量環境的優化設定
encryption:
enabled: true
type: wireguard
wireguard:
userspaceFallback: false
nodeEncryption: true # 節點間流量加密
# BPF 效能優化
bpf:
preallocateMaps: true
hostLegacyRouting: false
|
可觀測性(Hubble)
Hubble 是 Cilium 的可觀測性平台,提供網路流量的深度可視化和監控能力。
啟用 Hubble
1
2
3
4
5
6
7
8
9
10
11
| # 使用 Cilium CLI 啟用 Hubble
cilium hubble enable --ui
# 或透過 Helm 配置
helm upgrade cilium cilium/cilium \
--namespace kube-system \
--reuse-values \
--set hubble.enabled=true \
--set hubble.relay.enabled=true \
--set hubble.ui.enabled=true \
--set hubble.metrics.enabled="{dns,drop,tcp,flow,icmp,http}"
|
安裝 Hubble CLI
1
2
3
4
5
6
7
8
| # 下載 Hubble CLI
HUBBLE_VERSION=$(curl -s https://raw.githubusercontent.com/cilium/hubble/master/stable.txt)
HUBBLE_ARCH=amd64
if [ "$(uname -m)" = "aarch64" ]; then HUBBLE_ARCH=arm64; fi
curl -L --fail --remote-name-all https://github.com/cilium/hubble/releases/download/$HUBBLE_VERSION/hubble-linux-${HUBBLE_ARCH}.tar.gz{,.sha256sum}
sha256sum --check hubble-linux-${HUBBLE_ARCH}.tar.gz.sha256sum
sudo tar xzvfC hubble-linux-${HUBBLE_ARCH}.tar.gz /usr/local/bin
rm hubble-linux-${HUBBLE_ARCH}.tar.gz{,.sha256sum}
|
使用 Hubble 觀察流量
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
| # 設定 port-forward
cilium hubble port-forward &
# 觀察所有流量
hubble observe
# 觀察特定 namespace 的流量
hubble observe --namespace production
# 觀察特定 pod 的流量
hubble observe --pod production/api-server
# 觀察被拒絕的流量
hubble observe --verdict DROPPED
# 觀察 HTTP 流量
hubble observe --protocol http
# 觀察 DNS 流量
hubble observe --protocol dns
# 過濾特定來源和目標
hubble observe --from-pod frontend --to-pod backend
# 以 JSON 格式輸出
hubble observe --output json
# 持續追蹤流量(類似 tail -f)
hubble observe --follow
|
Hubble UI
1
2
3
4
5
6
| # 開啟 Hubble UI
cilium hubble ui
# 或手動設定 port-forward
kubectl port-forward -n kube-system svc/hubble-ui 12000:80
# 然後在瀏覽器開啟 http://localhost:12000
|
Hubble Metrics
Hubble 可以匯出 Prometheus 格式的指標:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| hubble:
enabled: true
metrics:
enabled:
- dns:query;ignoreAAAA
- drop
- tcp
- flow
- icmp
- http
serviceMonitor:
enabled: true # 如果使用 Prometheus Operator
dashboards:
enabled: true
namespace: monitoring
|
常用的 Hubble 指標:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| # DNS 查詢延遲
histogram_quantile(0.99, sum(rate(hubble_dns_response_types_total[5m])) by (le))
# HTTP 請求率
sum(rate(hubble_http_requests_total[5m])) by (method, path)
# 丟棄的封包
sum(rate(hubble_drop_total[5m])) by (reason)
# TCP 連線統計
sum(rate(hubble_tcp_flags_total[5m])) by (flag)
# 流量按 namespace 統計
sum(rate(hubble_flows_processed_total[5m])) by (source_namespace, destination_namespace)
|
設定 Grafana Dashboard
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
| apiVersion: v1
kind: ConfigMap
metadata:
name: hubble-grafana-dashboard
namespace: monitoring
labels:
grafana_dashboard: "1"
data:
hubble-dashboard.json: |
{
"dashboard": {
"title": "Hubble Network Overview",
"panels": [
{
"title": "Network Traffic",
"type": "graph",
"targets": [
{
"expr": "sum(rate(hubble_flows_processed_total[5m])) by (verdict)"
}
]
}
]
}
}
|
服務網格功能
Cilium 提供了內建的服務網格功能,可以在不需要 sidecar 的情況下實現服務網格的核心功能。
Sidecar-free 服務網格
傳統服務網格(如 Istio)需要在每個 pod 中注入 sidecar proxy,而 Cilium 使用 eBPF 在核心層級實現相同功能:
1
2
3
4
5
6
| # 啟用 Cilium Service Mesh
helm upgrade cilium cilium/cilium \
--namespace kube-system \
--reuse-values \
--set ingressController.enabled=true \
--set ingressController.loadbalancerMode=dedicated
|
Ingress Controller
Cilium 可以作為 Kubernetes Ingress Controller:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
| apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: my-ingress
annotations:
kubernetes.io/ingress.class: cilium
spec:
rules:
- host: api.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: api-service
port:
number: 80
|
Gateway API 支援
Cilium 完整支援 Kubernetes Gateway API:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
| apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
name: cilium-gateway
namespace: production
spec:
gatewayClassName: cilium
listeners:
- name: http
protocol: HTTP
port: 80
- name: https
protocol: HTTPS
port: 443
tls:
mode: Terminate
certificateRefs:
- name: tls-secret
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
name: api-route
namespace: production
spec:
parentRefs:
- name: cilium-gateway
hostnames:
- "api.example.com"
rules:
- matches:
- path:
type: PathPrefix
value: /api/v1
backendRefs:
- name: api-v1-service
port: 80
weight: 90
- name: api-v2-service
port: 80
weight: 10
|
流量管理
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
| apiVersion: cilium.io/v2
kind: CiliumEnvoyConfig
metadata:
name: traffic-management
namespace: production
spec:
services:
- name: api-service
namespace: production
backendServices:
- name: api-v1
namespace: production
- name: api-v2
namespace: production
resources:
- "@type": type.googleapis.com/envoy.config.listener.v3.Listener
name: api-listener
filter_chains:
- filters:
- name: envoy.filters.network.http_connection_manager
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.network.http_connection_manager.v3.HttpConnectionManager
route_config:
virtual_hosts:
- name: api
routes:
- match:
prefix: "/"
route:
weighted_clusters:
clusters:
- name: api-v1
weight: 80
- name: api-v2
weight: 20
|
負載平衡
Cilium 支援多種負載平衡模式:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
| # 使用 Maglev 一致性雜湊
loadBalancer:
algorithm: maglev
mode: dsr # Direct Server Return
acceleration: native
# Session affinity 設定
apiVersion: v1
kind: Service
metadata:
name: sticky-service
annotations:
service.cilium.io/affinity: "true"
service.cilium.io/affinity-timeout: "3600"
spec:
type: LoadBalancer
sessionAffinity: ClientIP
sessionAffinityConfig:
clientIP:
timeoutSeconds: 3600
|
與其他 CNI 比較
Cilium vs Calico
| 特性 | Cilium | Calico |
|---|
| 資料平面 | eBPF | iptables/eBPF |
| L7 政策 | 原生支援 | 需要額外元件 |
| 效能 | 優秀(核心層級處理) | 良好 |
| 可觀測性 | Hubble(內建) | 需要整合其他工具 |
| 加密 | WireGuard/IPsec | WireGuard/IPsec |
| 學習曲線 | 較陡 | 較平緩 |
| 服務網格 | 內建(sidecar-free) | 需要 Istio 等 |
| 社群支援 | 活躍 | 活躍 |
Cilium vs Flannel
| 特性 | Cilium | Flannel |
|---|
| 網路政策 | 完整支援 | 不支援 |
| L7 政策 | 支援 | 不支援 |
| 效能 | 優秀 | 良好 |
| 複雜度 | 較高 | 簡單 |
| 資源消耗 | 較高 | 低 |
| 功能豐富度 | 非常高 | 基本 |
| 適用場景 | 生產環境 | 開發/測試環境 |
Cilium vs Weave Net
| 特性 | Cilium | Weave Net |
|---|
| 加密 | WireGuard/IPsec | 內建加密 |
| 網路政策 | 完整 L3-L7 | L3-L4 |
| 多叢集 | ClusterMesh | Weave Mesh |
| DNS 政策 | 支援 | 不支援 |
| 可觀測性 | Hubble | 有限 |
選擇建議
選擇 Cilium 的情況:
- 需要 L7 網路政策
- 重視可觀測性
- 需要高效能網路
- 計劃使用服務網格功能
- 需要進階的安全功能
選擇 Calico 的情況:
- 團隊熟悉 iptables
- 需要與現有 BGP 基礎設施整合
- 較舊的 Linux 核心環境
選擇 Flannel 的情況:
最佳實踐
安全建議
- 實施預設拒絕政策
1
2
3
4
5
6
7
8
9
10
11
12
| apiVersion: cilium.io/v2
kind: CiliumClusterwideNetworkPolicy
metadata:
name: default-deny
spec:
endpointSelector: {}
ingress:
- fromEntities:
- cluster
egress:
- toEntities:
- cluster
|
- 使用 L7 政策限制 API 存取
- 啟用網路加密
- 定期審查網路政策
- 使用 Hubble 監控異常流量
效能優化
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| # 啟用 BPF 優化
bpf:
preallocateMaps: true
masquerade: true
hostLegacyRouting: false
lbExternalClusterIP: true
# 使用 native routing 模式
routingMode: native
autoDirectNodeRoutes: true
ipv4NativeRoutingCIDR: 10.0.0.0/8
# 啟用 DSR 模式
loadBalancer:
mode: dsr
acceleration: native
|
監控與告警
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
| # Prometheus 告警規則範例
groups:
- name: cilium
rules:
- alert: CiliumAgentDown
expr: up{job="cilium-agent"} == 0
for: 5m
labels:
severity: critical
annotations:
summary: "Cilium agent is down"
- alert: HighPacketDropRate
expr: rate(hubble_drop_total[5m]) > 100
for: 10m
labels:
severity: warning
annotations:
summary: "High packet drop rate detected"
- alert: PolicyEnforcementFailure
expr: rate(cilium_policy_enforcement_errors_total[5m]) > 0
for: 5m
labels:
severity: warning
annotations:
summary: "Policy enforcement failures detected"
|
故障排除
常見問題診斷
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
| # 檢查 Cilium 狀態
cilium status --verbose
# 檢查端點狀態
cilium endpoint list
# 檢查政策狀態
cilium policy get
# 檢查 BPF maps
cilium bpf policy get --all
# 診斷連接問題
cilium connectivity test
# 檢查 Hubble 狀態
hubble status
# 查看 Cilium agent 日誌
kubectl -n kube-system logs -l k8s-app=cilium -f
# 檢查特定 endpoint 的政策
cilium endpoint get <endpoint-id>
cilium bpf policy get <endpoint-id>
|
網路政策除錯
1
2
3
4
5
6
7
8
9
10
11
| # 追蹤封包流程
cilium monitor --type trace
# 查看政策決策
cilium monitor --type policy-verdict
# 檢查 dropped 封包
cilium monitor --type drop
# 使用 Hubble 分析流量
hubble observe --verdict DROPPED --follow
|
結論
Cilium 作為新一代的 Kubernetes CNI,透過 eBPF 技術提供了卓越的效能、安全性和可觀測性。其豐富的功能集,包括 L7 網路政策、透明加密、服務網格支援以及強大的 Hubble 可觀測性平台,使其成為現代 Kubernetes 部署的理想選擇。
雖然 Cilium 的學習曲線可能較陡峭,但其帶來的安全和運維效益使得這項投資非常值得。透過本文介紹的概念和範例,您應該能夠開始在您的 Kubernetes 叢集中部署和使用 Cilium,並逐步建立起完善的網路安全策略。
參考資源