eBPF คืออะไร?
Extended Berkeley Packet Filter
eBPF (Extended Berkeley Packet Filter) คือเทคโนโลยีที่ฝังอยู่ใน Linux Kernel ที่ช่วยให้สามารถรันโปรแกรมแบบ sandboxed ภายใน kernel ได้โดยตรง โดยไม่ต้องแก้ไข source code ของ kernel หรือโหลด kernel module
เดิมที eBPF ถูกออกแบบมาสำหรับ packet filtering ระดับต่ำ แต่ปัจจุบันได้พัฒนาจนกลายเป็นหนึ่งในเครื่องมือที่ทรงพลังที่สุดในวงการ Cloud Native
ทำไม eBPF ถึงสำคัญ?
- Deep Visibility: มองเห็น system behavior ในระดับ kernel ที่ไม่มี user-space tool ใดเทียบได้
- Zero Instrumentation: ไม่ต้องแก้ไข application code หรือเพิ่ม agents/sidecars
- Low Overhead: ใช้ทรัพยากรน้อย เพราะทำงานใน kernel space โดยตรง
- Safety: eBPF programs ถูก verify ก่อนรัน ไม่สามารถ crash kernel ได้
สถาปัตยกรรม eBPF ใน Kubernetes
eBPF ทำงานใน Kernel Space โดย hook เข้ากับ system calls, network events, และ tracepoints เพื่อเก็บข้อมูลแบบ real-time
Use Cases ของ eBPF ใน Kubernetes
Observability
เก็บ metrics, traces, และ logs โดยอัตโนมัติจากทุก service โดยไม่ต้อง instrument code
- Auto-discovery services
- Distributed tracing
- Network flow visibility
- Application latency
Security
ตรวจจับและป้องกันภัยคุกคามแบบ real-time ที่ระดับ kernel ก่อนที่จะส่งผลกระทบ
- Runtime security monitoring
- System call filtering
- Anomaly detection
- File integrity monitoring
Networking
แทนที่ iptables ด้วย high-performance networking ที่ปรับขนาดได้ดีกว่า
- Load balancing
- Service mesh data plane
- Network policies
- Distributed firewall
eBPF Tools ยอดนิยมสำหรับ Kubernetes
Cilium
Networking, Security, ObservabilityCNI plugin ที่ใช้ eBPF สำหรับ networking, security และ observability แทนที่ kube-proxy และให้ network policies ที่ทรงพลัง
# ติดตั้ง Cilium CLI
curl -L --remote-name https://github.com/cilium/cilium-cli/releases/latest/download/cilium-linux-amd64.tar.gz
sudo tar xzvfC cilium-linux-amd64.tar.gz /usr/local/bin
rm cilium-linux-amd64.tar.gz
# ติดตั้ง Cilium บน Kubernetes
cilium install
# ตรวจสอบสถานะ
cilium status
Pixie
Zero-instrumentation ObservabilityOpen-source observability tool จาก New Relic ที่ใช้ eBPF เก็บ telemetry โดยอัตโนมัติ ไม่ต้องแก้ไข application
# ติดตั้ง Pixie CLI
bash -c "$(curl -fsSL https://withpixie.ai/install.sh)"
# Deploy Pixie บน Kubernetes
px deploy
# รัน script ตัวอย่าง
px run px/service_stats
# เปิด UI
px auth login
Falco
Runtime SecurityCNCF project สำหรับ runtime security ที่ใช้ eBPF ตรวจจับพฤติกรรมผิดปกติและ threats แบบ real-time
# ติดตั้ง Falco ด้วย Helm
helm repo add falcosecurity https://falcosecurity.github.io/charts
helm repo update
helm install falco falcosecurity/falco \
--namespace falco --create-namespace \
--set driver.kind=ebpf \
--set tty=true
# ดู logs
kubectl logs -n falco -l app=falco -f
Tetragon
Security Observability & Runtime Enforcementจาก Isovalent (Cilium creators) สำหรับ security observability และ real-time enforcement ด้วย eBPF
# ติดตั้ง Tetragon ด้วย Helm
helm repo add cilium https://helm.cilium.io
helm repo update
helm install tetragon cilium/tetragon \
--namespace kube-system
# รัน tetragon CLI
kubectl exec -ti -n kube-system \
daemonset/tetragon \
-- tetra getevents -o compact
ตัวอย่างการใช้งาน: Cilium + Hubble
ขั้นตอนที่ 1: ติดตั้ง Cilium พร้อม Hubble
# ติดตั้ง Cilium พร้อม Hubble enabled
cilium install --version 1.16.0 \
--set hubble.enabled=true \
--set hubble.relay.enabled=true \
--set hubble.ui.enabled=true
# รอให้ Cilium ready
cilium status --wait
# เปิด Hubble UI
cilium hubble ui
ขั้นตอนที่ 2: สังเกตการณ์ Network Traffic
# ดู network flows ทั้งหมด
hubble observe
# ดู flows จาก namespace ที่กำหนด
hubble observe --namespace default
# ดู HTTP traffic
hubble observe --protocol http
# ดู flows ที่ถูก block โดย policy
hubble observe --verdict DROPPED
# Export เป็น JSON
hubble observe -o json > flows.json
ขั้นตอนที่ 3: สร้าง Network Policy
# สร้างไฟล์ network-policy.yaml
cat <
ตัวอย่างการใช้งาน: Pixie Zero-Instrumentation
Pixie Scripts ที่มีประโยชน์
# ดู service statistics
px run px/service_stats
# ดู network traffic
px run px/network_stats
# ดู HTTP requests
px run px/http_data
# ดู slowest requests
px run px/slowest_requests
# ดู JVM metrics (Java apps)
px run px/jvm_stats
# ดู DNS latency
px run px/dns_latency
# สร้าง custom script
px run -f my_script.pxl
ข้อดีของ Pixie
- ไม่ต้อง instrument code
- Auto-discovery services
- Real-time data collection
- Low overhead (<1% CPU)
- Free & Open Source
ข้อควรพิจารณา
- ต้องใช้ Linux Kernel 4.14+
- Requires BTF (BPF Type Format)
- ใช้ storage ~1GB per node
- Best for Kubernetes 1.22+
ตัวอย่าง Falco Security Rules
Custom Falco Rules
# custom-rules.yaml
- rule: Detect Shell in Container
desc: Alert when shell runs inside container
condition: >
spawned_process and container and
(proc.name = "bash" or proc.name = "sh" or proc.name = "zsh")
output: >
Shell spawned in container
(user=%user.name container=%container.id
process=%proc.name parent=%proc.pname)
priority: WARNING
tags: [container, shell]
- rule: Detect File Access in Sensitive Path
desc: Detect file read/write in sensitive directories
condition: >
open_read and container and
(fd.directory = "/etc" or fd.directory = "/root")
output: >
Sensitive file accessed in container
(file=%fd.name container=%container.id)
priority: WARNING
tags: [container, filesystem]
- rule: Detect Outbound Connection from Container
desc: Detect unexpected outbound connections
condition: >
outbound and container and not proc.name in (curl, wget)
output: >
Unexpected outbound connection
(connection=%fd.name container=%container.id)
priority: INFO
tags: [container, network]
# สร้าง ConfigMap สำหรับ custom rules
kubectl create configmap falco-custom-rules \
--from-file=custom-rules.yaml \
-n falco
# Patch Falco เพื่อใช้ custom rules
kubectl patch deploy falco -n falco --type=merge -p '
{
"spec": {
"template": {
"spec": {
"containers": [{
"name": "falco",
"volumeMounts": [{
"name": "custom-rules",
"mountPath": "/etc/falco/rules.d"
}]
}],
"volumes": [{
"name": "custom-rules",
"configMap": {"name": "falco-custom-rules"}
}]
}
}
}
}'
เปรียบเทียบ eBPF Tools
| Feature | Cilium | Pixie | Falco | Tetragon |
|---|---|---|---|---|
| Networking | ||||
| Observability | ||||
| Security Runtime | ||||
| Policy Enforcement | ||||
| Zero Instrumentation | ||||
| Service Mesh | ||||
| Open Source | ||||
| CNCF Project | Graduated | Sandbox | Graduated | Sandbox |
Best Practices สำหรับ eBPF
ตรวจสอบ Kernel Version
ใช้ Linux Kernel 4.14+ สำหรับ features พื้นฐาน หรือ 5.2+ สำหรับ BTF support ที่ช่วยให้ eBPF programs ทำงานได้หลากหลาย kernel versions
เริ่มจาก Observability
เริ่มต้นด้วยการใช้ eBPF สำหรับ observability ก่อนที่จะ implement security policies หรือ network changes
Test ใน Staging ก่อน
eBPF tools ทำงานที่ kernel level อาจส่งผลต่อ cluster ทั้งหมด ควร test ใน staging environment ก่อนเสมอ
Monitor Resource Usage
eBPF programs ใช้ทรัพยากรน้อย แต่ควร monitor overhead โดยเฉพาะใน cluster ขนาดใหญ่
ใช้ CO-RE (Compile Once, Run Everywhere)
Compile eBPF programs ด้วย BTF และ CO-RE เพื่อให้ทำงานได้ บนหลาย kernel versions โดยไม่ต้อง recompile
Integrate กับ Existing Tools
eBPF tools สามารถ integrate กับ Prometheus, Grafana, และ SIEM solutions ที่มีอยู่แล้ว
Kernel Requirements
# ตรวจสอบ kernel version
uname -r
# ควรได้ 5.2+ สำหรับ BTF support
# ตรวจสอบ BTF support
ls /sys/kernel/btf/vmlinux
# ถ้าไฟล์มีอยู่แสดงว่า BTF supported
# ตรวจสอบ eBPF features
bpftool feature
# ตรวจสอบว่า kernel compile ด้วย BPF support
grep BPF /boot/config-$(uname -r)
# สำหรับ Ubuntu/Debian
apt install linux-headers-$(uname -r) bpftool
# สำหรับ RHEL/CentOS
yum install kernel-devel bpftool
คำถามที่พบบ่อย (FAQ)
ใช่ eBPF programs ถูก verifier ตรวจสอบก่อนรันทุกครั้ง ไม่สามารถ crash kernel หรือ access memory ที่ไม่ได้รับอนุญาตได้ นี่เป็นสาเหตุที่ eBPF ปลอดภัยกว่า kernel modules แบบดั้งเดิม
ได้ แต่ต้องตรวจสอบ kernel version ของ worker nodes ส่วนใหญ่ managed Kubernetes ใหม่ๆ รองรับ BTF แล้ว EKS ต้องใช้ Amazon Linux 2 หรือ Bottlerocket, GKE ต้องใช้ Ubuntu nodes หรือ Container-Optimized OS เวอร์ชันใหม่
eBPF มีข้อดีหลายอย่าง: ใช้ทรัพยากรน้อยกว่า (ไม่มี sidecar overhead), ไม่ต้องแก้ไข application, มองเห็นได้ลึกกว่า แต่ sidecar ยังมีประโยชน์สำหรับ use cases บางอย่าง เช่น mTLS termination หรือ protocol-specific logic
ได้ eBPF programs หลายตัวสามารถทำงานพร้อมกันได้ แต่ควรระวังการใช้ทรัพยากรรวม และอาจมี conflict ในบางกรณี เช่น Cilium และ Calico อาจไม่สามารถใช้ร่วมกันได้โดยตรง
โดยทั่วไป eBPF overhead ต่ำมาก: CPU <1%, memory สำหรับ maps ~100MB-1GB ต่อ node, latency overhead ~microseconds ซึ่งต่ำกว่า sidecar ที่อาจมี overhead หลาย milliseconds
อนาคตของ eBPF: Cloud Native 2.0
eBPF กำลังกลายเป็น foundation ของ Cloud Native 2.0 หลายคนเชื่อว่า eBPF จะสำคัญเท่ากับ containers และ Kubernetes ในทศวรรษหน้า
Sidecar-less Service Mesh
Istio Ambient Mesh และ Cilium Service Mesh ใช้ eBPF แทน sidecar proxies
AI-Powered Observability
AI วิเคราะห์ eBPF data เพื่อ detect anomalies และ auto-remediate
Real-time Security Enforcement
Block threats ที่ kernel level ก่อนที่จะส่งผลกระทบต่อ applications
สรุป
eBPF คือเทคโนโลยีที่กำลังเปลี่ยนแปลงวงการ Cloud Native โดยให้ความสามารถในการ observe, secure และ control ระบบที่ระดับ kernel