Skip to content
Kubernetes tools · Chapter 10 of 20 Day 8: Examining the Network

Day 8: Examining the Network

Goal: Check DNS, Services and ports with netshoot, capture traffic with tcpdump in an ephemeral container, use Hubble to see which connections a NetworkPolicy drops, and open a policy in a targeted way · Duration: approx. 80 min · Prerequisite: Day 7

Briefing

  • Most “network problems” in Kubernetes are not network problems: a Service is missing, a selector does not match, a port is wrong, a NetworkPolicy drops traffic. The art is to tell them apart quickly.
  • netshoot is a container image with all the network tools that normal images lack: dig, curl, nc, tcpdump, ip, iperf3, mtr and many more. You start it as a separate Pod or as an ephemeral container in the network namespace of an existing Pod.
  • Hubble is Cilium’s observability layer. It sees every connection that the eBPF data path handles, with Pod names instead of IPs and with the reason why a packet was dropped. In the lab, Cilium has been running with Hubble Relay and Hubble UI since Day 0.
  • Order of troubleshooting: name → address → port → permission. Does the name resolve? Does the Pod IP answer directly? Is the port listening? Does the policy let it through?
Network troubleshooting: name → address → port → permission
Question Tool
Does the name resolve? dig, /etc/resolv.conf in the Pod
Does the service answer? curl, nc -zv
What goes over the wire? tcpdump in an ephemeral container
Who is dropped and why? hubble observe --verdict DROPPED
What does the traffic look like as a whole? Hubble UI

Drill 1: netshoot as a Toolbox

kubectl -n laden run netshoot --image=nicolaka/netshoot:v0.17 --restart=Never -- sleep infinity
kubectl -n laden wait --for=condition=Ready pod/netshoot --timeout=180s
kubectl -n laden exec netshoot -- dig +short web.laden.svc.cluster.local
kubectl -n laden exec netshoot -- dig +search +short api
kubectl -n laden exec netshoot -- cat /etc/resolv.conf

Expected (IPs will differ):

pod/netshoot created
pod/netshoot condition met
10.96.202.242
10.96.66.188
search laden.svc.cluster.local svc.cluster.local cluster.local localdomain
nameserver 10.96.0.10
options ndots:5

Unlike applications, dig only uses the search list with +search. The resolv.conf explains why api is enough: because of ndots:5, the resolver appends each name with fewer than five dots to the search domains in turn. localdomain at the end comes from the VM and is passed on to its DNS server.

Now the services themselves:

kubectl -n laden exec netshoot -- curl -s -m3 http://api:8080/
kubectl -n laden exec netshoot -- nc -zv -w2 web 443
kubectl -n laden exec netshoot -- nc -zv -w2 cache 6379

Expected (shortened, names and IPs will differ):

Hostname: api-7d5f77c79b-gjmtc
IP: 127.0.0.1
IP: ::1
IP: 10.244.1.91
IP: fe80::84ea:f6ff:fe31:27f0
RemoteAddr: 10.244.2.209:58444
nc: connect to web (10.96.202.242) port 443 (tcp) failed: Connection refused
command terminated with exit code 1
nc: getaddrinfo for host "cache" port 6379: Try again
command terminated with exit code 1

Three answers, three different failure patterns:

  • api answers. whoami shows the Pod that answered, and netshoot’s IP as RemoteAddr.
  • web on port 443: Connection refused. The name resolves, and the connection is rejected immediately. The Service only has port 80. Not a timeout, but an active rejection.
  • cache: the name does not resolve at all. The problem is not the port but the name.

Follow up on the last case, step by step:

kubectl -n laden exec netshoot -- dig cache.laden.svc.cluster.local | grep status
kubectl -n laden get svc
C=$(kubectl -n laden get pod -l app=cache -o jsonpath='{.items[0].status.podIP}')
kubectl -n laden exec netshoot -- nc -zv -w2 $C 6379
kubectl -n laden exec netshoot -- sh -c "printf 'PING\r\n' | nc -w2 $C 6379"

Expected:

;; ->>HEADER<<- opcode: QUERY, status: NXDOMAIN, id: 60308
NAME   TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
api    ClusterIP   10.96.66.188    <none>        8080/TCP   119m
web    ClusterIP   10.96.202.242   <none>        80/TCP     119m
Connection to 10.244.2.193 6379 port [tcp/redis] succeeded!
+PONG

Redis is running and answers on the Pod IP. There is just no Service cache. It has been like this since Day 0; so far, the shop has only used the cache via the Pod IP, which breaks after the next restart of the Pod. A kubectl -n laden expose deploy cache --port 6379 would fix it. Leave the bug in place until Day 9; there you create the Service and see what a checking tool says when it does not fit.

Save your progress? Sign up for free

Drill 2: Capturing with tcpdump

nginx has no tcpdump. An ephemeral container with netshoot shares the network namespace with all containers of the Pod and therefore sees the same eth0. For capturing, it needs the capabilities NET_ADMIN and NET_RAW: --profile=netadmin provides them.

W=$(kubectl -n laden get pods -l app=web -o jsonpath='{.items[0].metadata.name}')
WIP=$(kubectl -n laden get pod $W -o jsonpath='{.status.podIP}')
BIP=$(kubectl -n laden get pod werkbank -o jsonpath='{.status.podIP}')
kubectl -n laden debug $W --image=nicolaka/netshoot:v0.17 --target=nginx --profile=netadmin \
  --container=sniffer -- tcpdump -i eth0 -nn -c 8 "tcp port 80 and host $BIP"

The capture runs in the background and waits for eight packets from the werkbank (workbench). Generate them, directly to the Pod IP, so that they are sure to land on this Pod:

kubectl -n laden exec werkbank -- wget -qO- http://$WIP/ >/dev/null
kubectl -n laden logs $W -c sniffer

Expected (times, ports and sequence numbers will differ, options shortened):

tcpdump: verbose output suppressed, use -v[v]... for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), snapshot length 262144 bytes
13:21:41.428045 IP 10.244.2.14.59568 > 10.244.2.250.80: Flags [S], seq 57986471, win 64860, …, length 0
13:21:41.428092 IP 10.244.2.250.80 > 10.244.2.14.59568: Flags [S.], seq 2252444979, ack 57986472, …, length 0
13:21:41.428176 IP 10.244.2.14.59568 > 10.244.2.250.80: Flags [.], ack 1, win 507, …, length 0
13:21:41.429363 IP 10.244.2.14.59568 > 10.244.2.250.80: Flags [P.], seq 1:76, ack 1, …, length 75: HTTP: GET / HTTP/1.1
13:21:41.429377 IP 10.244.2.250.80 > 10.244.2.14.59568: Flags [.], ack 76, …, length 0
13:21:41.429628 IP 10.244.2.250.80 > 10.244.2.14.59568: Flags [P.], seq 1:234, ack 76, …, length 233: HTTP: HTTP/1.1 200 OK
13:21:41.429679 IP 10.244.2.14.59568 > 10.244.2.250.80: Flags [.], ack 234, win 506, …, length 0
13:21:41.429707 IP 10.244.2.250.80 > 10.244.2.14.59568: Flags [P.], seq 234:1130, ack 76, …, length 896: HTTP
8 packets captured
12 packets received by filter
0 packets dropped by kernel

Read it from the top: handshake (S, S., .), the request GET /, the response 200 OK (233 bytes of headers), then the content (896 bytes, the same number as in the nginx log from Day 7). The filter on the werkbank IP keeps out the kubelet’s checks, which would otherwise interfere every ten seconds.

For Wireshark: With -w /tmp/web.pcap instead of -c 8, tcpdump writes a file. You fetch it with kubectl cp -c sniffer laden/$W:/tmp/web.pcap web.pcap and open it locally. The container must still be running for this; a capture with -G/-W or a sleep afterwards keeps it alive.

Save your progress? Sign up for free

Drill 3: A Policy That Drops

The team wants only web to reach api from now on. It creates a NetworkPolicy:

mkdir -p ~/werkzeuge/tag08 && cd ~/werkzeuge/tag08
cat > api-policy.yaml <<'EOF'
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: api-nur-von-web
  namespace: laden
spec:
  podSelector:
    matchLabels: {app: api}
  policyTypes: [Ingress]
  ingress:
  - from:
    - podSelector:
        matchLabels: {app: web}
    ports:
    - port: 8080
EOF
kubectl apply -f api-policy.yaml
kubectl -n laden exec netshoot -- curl -s -m3 -o /dev/null -w "%{http_code}\n" http://api:8080/
kubectl -n laden exec $W -c nginx -- wget -qO- -T3 http://api:8080/ | head -1

Expected:

networkpolicy.networking.k8s.io/api-nur-von-web created
000
command terminated with exit code 28
Hostname: api-7d5f77c79b-725kc

From netshoot: 000 and exit code 28, i.e. a timeout, not “refused”. This is what a dropping policy looks like from outside: the packets simply disappear. From web, it still works. But why exactly, and who else is affected? Hubble tells you.

Save your progress? Sign up for free

Drill 4: Hubble on the Command Line

ARCH=$(dpkg --print-architecture)
curl -sL https://github.com/cilium/hubble/releases/download/v1.20.4/hubble-linux-${ARCH}.tar.gz | tar xz -C /tmp hubble
sudo install /tmp/hubble /usr/local/bin/hubble
hubble version

Expected:

hubble v1.20.4@HEAD-3e268bb compiled with go1.27.3 on linux/amd64

The CLI talks to Hubble Relay. cilium-cli sets up a forward to localhost:4245; leave it running in a second terminal:

cilium hubble port-forward
hubble status

Expected (numbers will differ):

Healthcheck (via localhost:4245): Ok
Current/Max Flows: 12,285/12,285 (100.00%)
Flows/s: 15.15
Connected Nodes: 3/3

Current/Max is a ring buffer per node: Hubble keeps the latest flows in memory, not forever. Now the dropped connections in laden (shop):

hubble observe -n laden --verdict DROPPED --last 3

Expected:

Nov 24 13:22:00.861: laden/netshoot:33560 (ID:2285) <> laden/api-7d5f77c79b-725kc:8080 (ID:17414) Policy denied DROPPED (TCP Flags: SYN)
Nov 24 13:22:01.884: laden/netshoot:33560 (ID:2285) <> laden/api-7d5f77c79b-725kc:8080 (ID:17414) policy-verdict:none INGRESS DENIED (TCP Flags: SYN)
Nov 24 13:22:01.885: laden/netshoot:33560 (ID:2285) <> laden/api-7d5f77c79b-725kc:8080 (ID:17414) Policy denied DROPPED (TCP Flags: SYN)

Everything is in one line: source and destination with Pod names, port, direction (INGRESS), reason (Policy denied), and that it happens at the first packet (SYN). The lines repeat because TCP sends the SYN again after one second. The ID is the Cilium identity, which is derived from a Pod’s labels; all Pods with the same labels share it.

The cross-check, everything that arrives at api:

hubble observe --to-pod laden/api --protocol tcp --last 6 -o compact

Expected (excerpt):

Nov 24 13:22:03.580: laden/web-67649cfddb-6d48k:35516 (ID:37619) -> laden/api-7d5f77c79b-725kc:8080 (ID:17414) policy-verdict:L3-L4 INGRESS ALLOWED (TCP Flags: SYN)
Nov 24 13:22:03.581: laden/web-67649cfddb-6d48k:35516 (ID:37619) -> laden/api-7d5f77c79b-725kc:8080 (ID:17414) to-endpoint FORWARDED (TCP Flags: SYN)
Nov 24 13:22:03.581: laden/web-67649cfddb-6d48k:35516 (ID:37619) -> laden/api-7d5f77c79b-725kc:8080 (ID:17414) to-endpoint FORWARDED (TCP Flags: ACK)

--to-pod expects namespace/prefix and cannot be combined with -n; hubble tells you so clearly if you try. For an overview instead of single lines, JSON helps:

hubble observe -n laden --verdict DROPPED --last 50 -o json | jq -r '.flow | [.source.pod_name, .destination.pod_name, .drop_reason_desc] | @tsv' | sort | uniq -c

Expected (number will differ):

      6 netshoot    api-7d5f77c79b-725kc    POLICY_DENIED

Only netshoot is affected. If the checkout (kasse) had already used the api, it would show up here too.

DNS in Hubble: hubble observe --protocol dns stays empty in the lab. Hubble only sees DNS queries with names once a CiliumNetworkPolicy with a DNS rule routes the traffic through Cilium’s DNS proxy. That is material for the Cilium course.

Save your progress? Sign up for free

Drill 5: Hubble UI

kubectl -n kube-system port-forward --address 0.0.0.0 svc/hubble-ui 12000:80

Open http://<VM-IP>:12000 and select the namespace laden at the top left. Generate a few requests (kubectl -n laden exec netshoot -- curl -s -m2 http://api:8080/ and … http://web/), then:

Hubble UI: service map of the namespace laden

The map groups by the label app. Pods without this label, here netshoot and werkbank (both only have run=… from kubectl run), appear as boxes with the name of the namespace. The dashed red line to api on port 8080 is the dropped traffic. world with ICMPv4 is outbound traffic.

Via Any verdict → Dropped, only the dropped flows remain:

Hubble UI: only dropped flows

The UI shows the same as the CLI. Its advantage is the map for someone who does not know the namespace; the CLI is better for filters, scripts and tickets, because you can copy text.

Save your progress? Sign up for free

Exercise: Classifying Errors

Assign the cause to each finding and name the command with which you confirm it:

  1. curl: (28) Connection timed out from netshoot to api:8080
  2. nc: connect to web … port 443 (tcp) failed: Connection refused
  3. nc: getaddrinfo for host "cache" … Try again
  4. curl to a Service IP hangs, but to each Pod IP behind it, it works

Solution

  1. A NetworkPolicy drops: hubble observe -n laden --verdict DROPPED.
  2. Port not open (the Service only has 80): kubectl -n laden get svc web.
  3. No Service with this name: kubectl -n laden get svc, dig cache.laden.svc.cluster.local.
  4. Service selector or targetPort does not match: kubectl get endpointslices -l kubernetes.io/service-name=… (no endpoints or wrong port). You have not reproduced this case here; Day 9 finds it.

Save your progress? Sign up for free

Challenge

From now on, the checkout is to query prices from api. Extend the policy so that Pods with app=kasse are additionally allowed on port 8080, but netshoot still is not. Prove both with Hubble, not just with curl.

Solution

kubectl -n laden patch networkpolicy api-nur-von-web --type=json \
  -p '[{"op":"add","path":"/spec/ingress/0/from/-","value":{"podSelector":{"matchLabels":{"app":"kasse"}}}}]'
kubectl -n laden get netpol api-nur-von-web -o jsonpath='{.spec.ingress[0].from}{"\n"}'
kubectl -n laden exec deploy/kasse -- wget -qO- -T3 http://api:8080/ | head -1
kubectl -n laden exec netshoot -- curl -s -m3 -o /dev/null -w "%{http_code}\n" http://api:8080/

Expected:

networkpolicy.networking.k8s.io/api-nur-von-web patched
[{"podSelector":{"matchLabels":{"app":"web"}}},{"podSelector":{"matchLabels":{"app":"kasse"}}}]
Hostname: api-7d5f77c79b-gjmtc
000
command terminated with exit code 28

Two entries under from are an or. Had you written app: kasse into the same podSelector as app: web, it would have been an and, and no Pod has both labels.

hubble observe --from-label app=kasse --type policy-verdict --last 1 -o compact
hubble observe --to-pod laden/api --verdict DROPPED --last 1 -o compact

Expected (excerpt):

Nov 24 13:23:26.526: laden/kasse-74bbfbb76-dgbgt:34508 (ID:64345) -> laden/api-7d5f77c79b-gjmtc:8080 (ID:17414) policy-verdict:L3-L4 INGRESS ALLOWED (TCP Flags: SYN)
Nov 24 13:22:32.158: laden/netshoot:47812 (ID:2285) <> laden/api-7d5f77c79b-gjmtc:8080 (ID:17414) Policy denied DROPPED (TCP Flags: SYN)
Nov 24 13:23:29.116: laden/netshoot:35178 (ID:2285) <> laden/api-7d5f77c79b-725kc:8080 (ID:17414) Policy denied DROPPED (TCP Flags: SYN)

Two lines despite --last 1: the value applies per node, and the two api Pods run on different nodes.

The file api-policy.yaml does not know about the checkout yet. Add it there, otherwise the next kubectl apply takes the permission away again (as in the Challenge of Day 7).

Save your progress? Sign up for free

Checkpoint

hubble version
kubectl -n laden get netpol
kubectl -n laden get pod netshoot -o jsonpath='{.status.phase}{"\n"}'

Expected (age will differ):

hubble v1.20.4@HEAD-3e268bb compiled with go1.27.3 on linux/amd64
NAME              POD-SELECTOR   AGE
api-nur-von-web   app=api        5m
Running

netshoot stays in place for the next days.

Interview Note

  • Timeout or refused? Refused: target reached, nothing listening on the port. Timeout: packets get lost, typical for a NetworkPolicy, firewall or wrong route.
  • How do you capture in a Pod without tcpdump? An ephemeral container with netshoot and --profile=netadmin, because all containers of a Pod share the network namespace.
  • How do you find out which policy drops? With Cilium: hubble observe --verdict DROPPED. Without Hubble, all that remains is to read all policies that apply to the target Pod and think.

Enterprise Note

  • Hubble keeps flows only in the ring buffer. For evidence (“who talked to the database yesterday?”), you export flows (Hubble Exporter to files, metrics to Prometheus) and keep them.
  • Like busybox, netshoot belongs in your own registry. A debug image with tcpdump and NET_RAW in production is a tool for attackers as well as for admins; Pod Security Admission set to restricted prevents --profile=netadmin in sensitive namespaces.
  • Without Cilium, similar views exist only partially: Calico Enterprise, Retina from Microsoft, or service mesh telemetry (Linkerd, Istio) for L7.

Contents

More