Run pktvisord as a sidecar in a pod to observe that pod's network traffic
(eth0) and expose Prometheus metrics on :10853.
+------------------- Pod (shared network namespace) -------------------+
| |
| [ app ] [ traffic-gen (optional) ] [ pktvisord ] |
| nginx wget loop -> external URL sniffs eth0 |
| :10853/metrics |
+----------------------------------------------------------------------+
^
| Prometheus scrapes via pod annotations
Containers in a pod share one network namespace, so the pktvisord sidecar sees
the same eth0 as your application container.
- A Kubernetes cluster and
kubectl. - The target namespace must allow pods to add the
NET_RAWandNET_ADMINcapabilities (pktvisord captures in promiscuous mode — see the security note). Under the Pod Security Standards only theprivilegedlevel permits adding these (or an exemption / custom admission policy that allows them); bothbaselineandrestrictedreject them —baselineonly allows addingNET_BIND_SERVICE. This is about the namespace PSS level; the container itself is not run asprivileged: true— it only adds those two capabilities pktvisord needs to capture packets. - Prometheus (or Grafana Agent) configured to scrape pods by annotation — see Prometheus scrape configuration.
- The example captures
eth0, the pod's primary interface on most CNIs. If your pods use a different name, changeeth0in the manifest args (or useautoto let pktvisord pick the busiest interface). Find the name withkubectl exec <pod> -c app -- ls /sys/class/net(works withoutiproute2, which the minimal demo images don't ship). - The manifest's args are IPv4 — the default
status.podIPfamily, including on dual-stack clusters. On an IPv6-only or IPv6-primary cluster, change the sidecar args to bind IPv6 and use a/128host spec:-l ::(in place of-l 0.0.0.0) and-H $(POD_IP)/128(in place of/32).
# from the repo root (or use the bare filename from inside centralized_collection/k8s/)
kubectl apply -f centralized_collection/k8s/pktvisor-sidecar.yamlCreates a pktvisor-demo Deployment with three containers: your app (nginx
placeholder), an optional traffic generator, and the pktvisord sidecar.
kubectl rollout status deploy/pktvisor-demo
kubectl port-forward deploy/pktvisor-demo 10853:10853
# in another terminal:
curl -s localhost:10853/metrics | grep -E '^(dns|packets)_' | headYou should see non-zero packets_* and dns_* series (the traffic generator
drives them) — a populated /metrics is the "it's working" signal. pktvisord
aggregates over ~60s windows, so if the first scrape looks sparse, wait a few
seconds and retry. Note: the
published image redirects pktvisord's own stdout to a file, so
kubectl logs deploy/pktvisor-demo -c pktvisord is usually empty — rely on
/metrics, not logs. (If the sidecar CrashLoopBackOffs instead, re-check the
NET_RAW/NET_ADMIN capabilities and the -H .../32 CIDR.)
pktvisord watches eth0, so it sees the pod's ingress/egress — not
loopback. The optional traffic-gen container produces egress by wget-ing an
external URL (TARGET_URL, default http://example.com). To generate observable
ingress, drive the app from another pod or through a Service so the request
enters over eth0 — e.g.
kubectl run client --rm -it --image=busybox --restart=Never -- wget -qO- <pod-ip>:80.
(kubectl port-forward reaches the app over the pod's loopback, so that
traffic does not appear on eth0.) Repoint with TARGET_URL/INTERVAL, or
delete the traffic-gen container for real workloads.
No internet egress? If only external egress is blocked but in-cluster traffic
is allowed, the generator's DNS lookups still reach cluster DNS over eth0, so
dns_* populates; for richer packets_*, point TARGET_URL at an in-cluster
Service. Under a default-deny NetworkPolicy, egress to kube-dns is blocked
too — add an egress rule allowing DNS (UDP/TCP 53 to the kube-dns/CoreDNS pods),
otherwise name resolution itself fails and the metrics stay empty.
The pod carries the standard annotations:
prometheus.io/scrape: "true"
prometheus.io/port: "10853"
prometheus.io/path: "/metrics"A vanilla Prometheus discovers it with a pod job (no Prometheus Operator required):
scrape_configs:
- job_name: kubernetes-pods
kubernetes_sd_configs:
- role: pod
relabel_configs:
- source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_scrape]
action: keep
regex: "true"
- source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_path]
action: replace
target_label: __metrics_path__
regex: (.+)
- source_labels: [__address__, __meta_kubernetes_pod_annotation_prometheus_io_port]
action: replace
# first group matches a bracketed IPv6 literal or an IPv4/host (no colons)
regex: (\[.+\]|[^:]+)(?::\d+)?;(\d+)
replacement: $1:$2
target_label: __address__
- source_labels: [__meta_kubernetes_namespace]
target_label: namespace
- source_labels: [__meta_kubernetes_pod_name]
target_label: podrole: pod generates one target per declared container port, so a
multi-container pod (here app + pktvisord) is discovered as several targets
that these rules all rewrite to the same POD_IP:10853. Prometheus deduplicates
targets whose final label sets are identical, so the endpoint is still scraped
only once. Don't add a container-name/port keep to this shared job to
"fix" the duplication — it would drop every other annotated pod; if you want
explicit selection, do it inside a pktvisor-only scrape job.
Using the Prometheus Operator (kube-prometheus-stack)? Expose the sidecar with a
Serviceon port10853and create aServiceMonitorselecting it, instead of using annotations.
Import the community dashboard ID 14221, or the JSON at
../prometheus/grafana-dashboard-prometheus.json.
Add the sidecar to any existing pod spec:
- Add the three
prometheus.io/*pod annotations. For a Deployment/StatefulSet they go on the pod template (spec.template.metadata.annotations), not the top-level object metadata; for a bare Pod they go onmetadata.annotations. - Add the
pktvisordcontainer — including thePOD_IPdownward-API env, theNET_RAWandNET_ADMINcapabilities, andargs: ["pktvisord", "-l", "0.0.0.0", "-H", "$(POD_IP)/32", "eth0"]. - Remove the demo
appandtraffic-gencontainers.
The sidecar adds the NET_RAW and NET_ADMIN capabilities (it is not run as
a privileged container): NET_RAW opens the AF_PACKET/raw capture socket, and
NET_ADMIN is required because pktvisord opens the interface in promiscuous
mode — matching the project's documented bare-metal setcap cap_net_raw,cap_net_admin. Under the Pod Security Standards, both the baseline
and restricted levels reject adding these capabilities (baseline only allows
adding NET_BIND_SERVICE), so run this in a namespace at the privileged level
or with an exemption / custom policy that permits them. Note -l 0.0.0.0 exposes
/metrics on the pod IP (no auth) so Prometheus can scrape it — restrict with a
NetworkPolicy if needed.