Skip to content

antony@notes:~/kubernetes$ cat "Cilium-Tetragon-Overview.md"

Cilium Tetragon Overview

2025-09-18· kubernetes

Cilium Tetragon Overview

Cilium Tetragon 能力

Cilium 的 Tetragon 組件以 eBPF 技術為基礎,提供了強大的即時安全可觀測性與 Runtime Enforcement 能力。

Tetragon 能偵測並對具安全意義的事件做出反應,例如:

  • 程式執行事件
  • System call 活動
  • I/O 活動,包括網路與檔案存取

在 Kubernetes 環境中使用時,Tetragon 具備 Kubernetes 感知能力──也就是它能識別 namespace、Pods 等 Kubernetes 身分資訊──從而可以針對個別 Workload 來設定安全事件的偵測規則。

image

Deploy Tetragon

helm repo add cilium https://helm.cilium.io
helm repo update
helm install tetragon ${EXTRA_HELM_FLAGS[@]} cilium/tetragon -n kube-system

確認 tetragon 部屬成功

kubectl -n kube-system get pods -l "app.kubernetes.io/instance=tetragon"

執行結果 :

NAME                                 READY   STATUS    RESTARTS   AGE
tetragon-2fp24                       2/2     Running   0          5m11s
tetragon-7z89d                       2/2     Running   0          5m11s
tetragon-cntz4                       2/2     Running   0          5m11s
tetragon-kj669                       2/2     Running   0          5m11s
tetragon-mktc9                       2/2     Running   0          5m11s
tetragon-operator-6767dc8c8d-cqhcq   1/1     Running   0          5m11s
tetragon-sw7mz                       2/2     Running   0          5m11s

Deploy demo application

kubectl create -f https://raw.githubusercontent.com/cilium/cilium/v1.15.3/examples/minikube/http-sw-app.yaml

檢查 pod 狀態

$ kubectl get pods
NAME                        READY   STATUS    RESTARTS   AGE
deathstar-8c4c77fb7-4tsl8   1/1     Running   0          25s
deathstar-8c4c77fb7-g55dt   1/1     Running   0          25s
tiefighter                  1/1     Running   0          25s
xwing                       1/1     Running   0          25s

實作 Tetragon 核心功能

1. Execution Monitoring

Tetragon 的核心是追蹤 Kubernetes 叢集、虛擬機器和裸機系統中所有執行情況。這為 Tetragon 將所有系統行為歸因於特定二進位檔案及其相關元資料(Container、Pod、節點和叢集)奠定了基礎。

1.1. 觀察 Tetragon 執行事件

要開兩個終端機 在第一個終端機執行

# 使用此指令來取得與「xwing」Pod 位於同一 Kubernetes 節點上的 Tetragon Pod 的名稱:
POD=$(kubectl -n kube-system get pods -l 'app.kubernetes.io/name=tetragon' -o name --field-selector spec.nodeName=$(kubectl get pod xwing -o jsonpath='{.spec.nodeName}'))

# 使用 kubectl exec 定位它來執行 tetra getevents 命令
kubectl exec -ti -n kube-system $POD -c tetragon -- tetra getevents -o compact --pods xwing

在第二個終端機執行以下命令,讓 xwing pod 去存取一個網站

kubectl exec -ti xwing -- bash -c 'curl https://ebpf.io/applications/#tetragon'

在第一個終端機會看到以下訊息

🚀 process default/xwing /usr/bin/bash -c "curl https://ebpf.io/applications/#tetragon"
🚀 process default/xwing /usr/bin/curl https://ebpf.io/applications/#tetragon
💥 exit    default/xwing /usr/bin/curl https://ebpf.io/applications/#tetragon 0

2. File Access Monitoring

可以透過 YAML 設定檔將追蹤政策加入 Tetragon,以擴充其基本的執行追蹤功能。這些政策會在核心層進行過濾,確保只有「有興趣的事件」會從執行於核心中的 BPF 程式發佈到使用者空間。這樣的設計可確保即使在繁忙的系統中也能維持低負載。

以下的說明是延伸自「執行監控」的範例,加入了監控 Linux 中敏感檔案的政策。所使用的政策為 file_monitoring.yaml,你可以視需求檢閱並擴充。該政策中所監控的檔案是一組良好的基礎集合。

2.1. Apply the tracing policy

kubectl apply -f https://raw.githubusercontent.com/cilium/tetragon/main/examples/quickstart/file_monitoring.yaml

2.2. 觀察 Tetragon 文件訪問事件

在第二個終端機執行以下命令,產生一個讀取敏感檔案的事件

kubectl exec -ti xwing -- bash -c 'cat /etc/shadow'

會在第一個終端機看到以下訊息

🚀 process default/xwing /usr/bin/bash -c "cat /etc/shadow"
🚀 process default/xwing /usr/bin/cat /etc/shadow
📚 read    default/xwing /usr/bin/cat /etc/shadow
📚 read    default/xwing /usr/bin/cat /etc/shadow
📚 read    default/xwing /usr/bin/cat /etc/shadow
📚 read    default/xwing /usr/bin/cat /etc/shadow
💥 exit    default/xwing /usr/bin/cat /etc/shadow 0

根據追蹤政策,Tetragon 會在嘗試寫入敏感目錄(例如嘗試寫入 /etc 目錄)時產生寫入事件。

在第二個終端機執行以下命令

kubectl exec -ti xwing -- bash -c 'echo foo >> /etc/bar'

會在第一個終端機看到以下訊息

🚀 process default/xwing /usr/bin/bash -c "echo foo >> /etc/bar"
📝 write   default/xwing /usr/bin/bash /etc/bar
📝 write   default/xwing /usr/bin/bash /etc/bar
💥 exit    default/xwing /usr/bin/bash -c "echo foo >> /etc/bar" 0

3. Network Monitoring

首先,需要找到 K8s 叢集正在使用的 Pod CIDR 和 Service CIDR。

export PODCIDR=$(kubectl get cm kubeadm-config -n kube-system -o jsonpath='{.data.ClusterConfiguration}' | grep -oP 'podSubnet:\s*\K\S+')
export SERVICECIDR=$(kubectl get cm kubeadm-config -n kube-system -o jsonpath='{.data.ClusterConfiguration}' | grep -oP 'serviceSubnet:\s*\K\S+')

取得這些資訊後,可以自訂策略,以排除流向 PODCIDR 和 SERVICECIDR 環境變數中儲存的網路的網路流量。使用 envsubst 執行此操作,然後使用 kubectl apply 將策略套用至您的 Kubernetes 叢集:

curl -s https://raw.githubusercontent.com/cilium/tetragon/main/examples/quickstart/network_egress_cluster.yaml | envsubst | kubectl apply -f -

一旦應用了追蹤策略,您就可以然後在 xwing Pod 中執行 curl 命令來訪問我們最喜歡的網站之一。

在第二個終端機執行以下命令

kubectl exec -ti xwing -- bash -c 'curl https://ebpf.io/applications/#tetragon'

會在第一個終端機看到以下訊息

🚀 process default/xwing /usr/bin/bash -c "curl https://ebpf.io/applications/#tetragon"
🚀 process default/xwing /usr/bin/curl https://ebpf.io/applications/#tetragon
🔌 connect default/xwing /usr/bin/curl tcp 10.0.5.114:44810 -> 172.67.71.235:443
💥 exit    default/xwing /usr/bin/curl https://ebpf.io/applications/#tetragon 0

看到在 tetra getevents 命令的輸出中報告了一個 connect 事件

可以透過向我們的服務發送 curl 請求來確認內核 BPF 過濾器是否未對叢集內部流量產生事件,若無 connect 事件產生,即表示未產生事件。

在第二個終端機執行以下命令

kubectl exec -ti xwing -- bash -c 'curl -s -XPOST deathstar.default.svc.cluster.local/v1/request-landing'

會在第一個終端機看到以下訊息

🚀 process default/xwing /usr/bin/bash -c "curl -s -XPOST deathstar.default.svc.cluster.local/v1/request-landing"
🚀 process default/xwing /usr/bin/curl -s -XPOST deathstar.default.svc.cluster.local/v1/request-landing
💥 exit    default/xwing /usr/bin/curl -s -XPOST deathstar.default.svc.cluster.local/v1/request-landing 0

4. Policy Enforcement

Tetragon 的追蹤策略支援監控內核函式以報告事件,例如檔案存取事件或網路連線事件,並對這些相同的內核函式強制執行限制。 在 Tetragon 中使用內核過濾提供了關鍵的效能提升,通過限制從內核到使用者空間的事件傳遞。 內核過濾還使 Tetragon 能夠在內核層級強制執行策略限制。 例如,當檢測到策略違規時,通過向進程發送 SIGKILL,該進程將無法繼續執行。 如果策略強制執行是通過系統呼叫觸發的,則意味著應用程式將無法從該系統呼叫返回,並將被終止。

限制 Kubernetes 上的網路流量

export PODCIDR=$(kubectl get cm kubeadm-config -n kube-system -o jsonpath='{.data.ClusterConfiguration}' | grep -oP 'podSubnet:\s*\K\S+')
export SERVICECIDR=$(kubectl get cm kubeadm-config -n kube-system -o jsonpath='{.data.ClusterConfiguration}' | grep -oP 'serviceSubnet:\s*\K\S+')

得到 PodCIDR 和 SERVICECIDR 後,即可自訂並套用強制執行策略。

curl -s https://raw.githubusercontent.com/cilium/tetragon/main/examples/quickstart/network_egress_cluster_enforce.yaml | envsubst  | kubectl apply -n default -f -

若要產生 Tetragon 將報告的事件,請使用 curl 連接到 Kubernetes 叢集外部的網站:

kubectl exec -ti xwing -- bash -c 'curl https://ebpf.io/applications/#tetragon'

你會得到以下錯誤訊息

command terminated with exit code 137

並在第一個終端機看到以下訊息

🚀 process default/xwing /usr/bin/bash -c "curl https://ebpf.io/applications/#tetragon"
🚀 process default/xwing /usr/bin/curl https://ebpf.io/applications/#tetragon
🔌 connect default/xwing /usr/bin/curl tcp 10.0.5.114:38040 -> 104.26.5.27:443
🔌 connect default/xwing /usr/bin/curl tcp 10.0.5.114:38040 -> 104.26.5.27:443
💥 exit    default/xwing /usr/bin/curl https://ebpf.io/applications/#tetragon SIGKILL

與叢集內部目標建立網路連線將如預期般運作:

kubectl exec -ti xwing -- bash -c 'curl -s -XPOST deathstar.default.svc.cluster.local/v1/request-landing'

並在第一個終端機看到以下訊息

🚀 process default/xwing /usr/bin/bash -c "curl -s -XPOST deathstar.default.svc.cluster.local/v1/request-landing"
🚀 process default/xwing /usr/bin/curl -s -XPOST deathstar.default.svc.cluster.local/v1/request-landing
💥 exit    default/xwing /usr/bin/curl -s -XPOST deathstar.default.svc.cluster.local/v1/request-landing 0

強制執行檔案存取限制

以下內容擴展了「檔案存取監控」的範例,以確保敏感檔案不被讀取。所使用的策略是 file_monitoring_enforce.yaml,您可以根據需要進行審查和擴展。觀察策略與強制執行策略之間的唯一區別是,強制執行策略新增了一個 action 區塊,該區塊會在檢測到操作違規時,發送 SIGKILL 信號終止應用程式,並使該操作返回錯誤。

kubectl delete -f https://raw.githubusercontent.com/cilium/tetragon/main/examples/quickstart/file_monitoring.yaml
kubectl apply -f https://raw.githubusercontent.com/cilium/tetragon/main/examples/quickstart/file_monitoring_enforce.yaml

接下來,嘗試讀取敏感文件(定義的策略中包含的文件之一):

kubectl exec -ti xwing -- bash -c 'cat /etc/shadow'

得到以下訊息

E0715 21:25:27.582529   11112 websocket.go:297] Unknown stream id 1, discarding message
                                                                                       command terminated with exit code 137

並在第一個終端機看到以下訊息

🚀 process default/xwing /usr/bin/bash -c "cat /etc/shadow"
🚀 process default/xwing /usr/bin/cat /etc/shadow
📚 read    default/xwing /usr/bin/cat /etc/shadow
📚 read    default/xwing /usr/bin/cat /etc/shadow
💥 exit    default/xwing /usr/bin/cat /etc/shadow SIGKILL

嘗試讀取或寫入不屬於強製檔案策略的檔案不會受到影響 :

kubectl exec -ti xwing -- bash -c 'echo foo >> bar; cat bar'

得到以下訊息

foo

並在第一個終端機看到以下訊息

🚀 process default/xwing /usr/bin/bash -c "echo foo >> bar; cat bar"
🚀 process default/xwing /usr/bin/cat bar
💥 exit    default/xwing /usr/bin/cat bar 0