Day 12: Kubernetes Services
Goal: You build a small Kubernetes cluster with Cilium on
netz, follow a request to a service all the way to the pod, find out where the ClusterIP “lives” (nowhere), read endpoints and readiness, and get a NodePort through two firewalls · Duration: approx. 100 min · Prerequisite: Day 11 (Docker onnetz), about 2 GB of free memory, internet access for roughly 1 GB of images
Briefing
- A service gives a group of pods a fixed name and a fixed address, the ClusterIP. Pods come and go, their addresses change; the ClusterIP stays.
- Which pods sit behind it is decided by the service’s selector. Kubernetes writes the matching, ready pods into EndpointSlices. Ready means: the readiness probe is green.
- Who translates ClusterIP → pod address? Classically
kube-proxy with iptables rules on every node. In this
cluster Cilium replaces kube-proxy with eBPF – and
translates already at
connect()in the pod. That has consequences for debugging, which you will see today. - The cluster is built with kind (Kubernetes in
Docker): every node is a Docker container on
netz. So everything from Day 11 still applies – bridge, veth, NAT,DOCKER-USER.
| Term | What it is | Where you see it |
|---|---|---|
| ClusterIP | virtual address of the service, on no interface | kubectl get svc |
| EndpointSlice | list of pod addresses and ports behind a service | kubectl get endpointslices |
| NodePort | port on every node that leads to the service (30000–32767) | kubectl get svc, column PORT(S) |
| Readiness | “pod may receive traffic” | column READY in kubectl get pods |
Drill 1: A cluster on netz
Three tools, each a single binary: kind builds the
cluster, kubectl talks to it, cilium installs
and checks the network.
# on netz
# architecture of the VM (amd64 or arm64) for the downloads
ARCH=$(dpkg --print-architecture)
# download kind v0.33.0 and install it to /usr/local/bin
curl -sLo /tmp/kind https://kind.sigs.k8s.io/dl/v0.33.0/kind-linux-${ARCH}
sudo install /tmp/kind /usr/local/bin/kind
# kubectl in the cluster's version (1.37.0)
curl -sLo /tmp/kubectl https://dl.k8s.io/release/v1.37.0/bin/linux/${ARCH}/kubectl
sudo install /tmp/kubectl /usr/local/bin/kubectl
# cilium CLI v0.20.3: download the archive, unpack only the binary to /usr/local/bin
curl -sL https://github.com/cilium/cilium-cli/releases/download/v0.20.3/cilium-linux-${ARCH}.tar.gz | sudo tar xz -C /usr/local/bin cilium
# check the versions
kind version
kubectl version --client | head -1
cilium version --client | head -1Expected:
kind v0.33.0 go1.26.7 linux/amd64
Client Version: v1.37.0
cilium-cli: v0.20.3 compiled with go1.27.3 on linux/amd64
Every kind node starts many services in its container that watch the
file system (inotify). The Ubuntu defaults are not enough for that;
without higher limits, pods hang with
too many open files.
# on netz
# set higher inotify limits permanently
printf "fs.inotify.max_user_instances = 512\nfs.inotify.max_user_watches = 524288\n" | sudo tee /etc/sysctl.d/99-kind.conf >/dev/null
# reload all sysctl files, show only the two lines
sudo sysctl --system | grep inotifyExpected:
fs.inotify.max_user_instances = 512
fs.inotify.max_user_watches = 524288
The cluster: one control plane, one worker, without kind’s default CNI and without kube-proxy – Cilium takes over both in a moment.
# on netz
# working directory for the Kubernetes days
mkdir -p ~/ndbg/k8s && cd ~/ndbg/k8s
# cluster description for kind
cat > kind.yaml <<'EOF'
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
name: ndbg
networking:
disableDefaultCNI: true # no kindnet, Cilium comes next
kubeProxyMode: none # no kube-proxy, Cilium replaces it
nodes:
- role: control-plane
- role: worker
EOF
# create the cluster (downloads the node image the first time, roughly 450 MB)
sudo kind create cluster --config kind.yaml
# store the kubeconfig for user ubuntu, readable only by that user
mkdir -p ~/.kube && sudo kind get kubeconfig --name ndbg > ~/.kube/config && chmod 600 ~/.kube/config
# nodes with address
kubectl get nodes -o wide | awk '{print $1, $2, $3, $6}'Expected (end of the kind output, then the nodes):
✓ Joining worker nodes 🚜
Set kubectl context to "kind-ndbg"
...
NAME STATUS ROLES INTERNAL-IP
ndbg-control-plane NotReady control-plane 172.18.0.2
ndbg-worker NotReady <none> 172.18.0.3
NotReady is correct: without a CNI, no pod gets a
network. The nodes have addresses from 172.18.0.0/16 – that
is the Docker network kind, a bridge on netz
like docker0 on Day 11.
# on netz
# install Cilium 1.20.5:
# kubeProxyReplacement=true services via eBPF instead of kube-proxy
# k8sServiceHost/k8sServicePort address of the API server (without kube-proxy nobody knows 10.96.0.1)
cilium install --version 1.20.5 --set kubeProxyReplacement=true \
--set k8sServiceHost=ndbg-control-plane --set k8sServicePort=6443
# wait until everything runs (up to 5 min), strip color codes, show only the key lines
cilium status --wait --wait-duration 5m 2>/dev/null | sed 's/\x1b\[[0-9;]*[A-Za-z]//g' | grep -E "Cilium:|Operator:|Cluster Pods"
# nodes ready now?
kubectl get nodesExpected:
/¯¯\__/¯¯\ Cilium: OK
\__/¯¯\__/ Operator: OK
Cluster Pods: 3/3 managed by Cilium
NAME STATUS ROLES AGE VERSION
ndbg-control-plane Ready control-plane 4m13s v1.37.0
ndbg-worker Ready <none> 3m58s v1.37.0
free -h then shows roughly 1.7 GB in use. The cluster
needs no more than that for the next days.
Save your progress? Sign up for free
Drill 2: A service and its endpoints
web is two pods running agnhost, a test
server from the Kubernetes project: GET /hostname returns
the name of the pod that answers. The readiness probe checks whether the
file /tmp/bereit exists – so you can take a pod out of
rotation on purpose in Drill 4.
# on netz
cd ~/ndbg/k8s
# deployment with two pods and a service in front
cat > web.yaml <<'EOF'
apiVersion: apps/v1
kind: Deployment
metadata: {name: web}
spec:
replicas: 2
selector: {matchLabels: {app: web}}
template:
metadata: {labels: {app: web}}
spec:
containers:
- name: web
image: registry.k8s.io/e2e-test-images/agnhost:2.53
# first create the ready file, then start the HTTP server on 8080
command: ["sh", "-c", "touch /tmp/bereit && exec /agnhost netexec --http-port=8080"]
ports: [{name: http, containerPort: 8080}]
readinessProbe:
exec: {command: ["cat", "/tmp/bereit"]} # ready as long as the file exists
periodSeconds: 2
---
apiVersion: v1
kind: Service
metadata: {name: web}
spec:
selector: {app: web} # all pods with app=web
ports: [{name: http, port: 80, targetPort: http}] # service port 80 → container port "http" (8080)
EOF
kubectl apply -f web.yaml
# tools pod: netshoot brings curl, dig, tcpdump, ip and ss
# --command -- sleep infinity the pod does nothing, it waits for kubectl exec
kubectl run werkzeug --image=nicolaka/netshoot:v0.17 --command -- sleep infinity
# wait until all three pods are ready
kubectl wait --for=condition=Ready pod -l app=web --timeout=180s
kubectl wait --for=condition=Ready pod/werkzeug --timeout=180sThen the three views of the same service:
# on netz
# pods with address and node
kubectl get pods -o wide | awk '{print $1, $2, $6, $7}'
# the service with its ClusterIP
kubectl get svc web
# the EndpointSlice: which pod addresses and which port behind it
kubectl get endpointslices -l kubernetes.io/service-name=webExpected (names and addresses differ):
NAME READY IP NODE
web-795c654bcf-cfkpp 1/1 10.244.1.171 ndbg-worker
web-795c654bcf-wt9gp 1/1 10.244.1.32 ndbg-worker
werkzeug 1/1 10.244.1.135 ndbg-worker
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
web ClusterIP 10.96.157.184 <none> 80/TCP 60s
NAME ADDRESSTYPE PORTS ENDPOINTS AGE
web-2lp95 IPv4 8080 10.244.1.171,10.244.1.32 61s
The service speaks port 80, the endpoints port 8080 – the translation belongs to the service. All pods run on the worker, because the control plane does not accept regular pods. Six requests by name:
# on netz
# sort | uniq -c count identical lines
for i in 1 2 3 4 5 6; do kubectl exec werkzeug -- curl -s http://web/hostname; echo; done | sort | uniq -cExpected (the distribution is random):
4 web-795c654bcf-cfkpp
2 web-795c654bcf-wt9gp
No round robin: Cilium picks a random endpoint for every new connection, just like kube-proxy.
Save your progress? Sign up for free
Drill 3: Where does the ClusterIP live?
The address 10.96.157.184 answers HTTP. Is it on an
interface anywhere?
# on netz
# ClusterIP into a variable
IP=$(kubectl get svc web -o jsonpath='{.spec.clusterIP}')
# addresses and routes in the tools pod
kubectl exec werkzeug -- ip -br addr
kubectl exec werkzeug -- ip route
# does the ClusterIP answer ping? (-W1 = wait 1 s)
kubectl exec werkzeug -- ping -c2 -W1 $IPExpected:
lo UNKNOWN 127.0.0.1/8 ::1/128
eth0@if11 UP 10.244.1.135/32 fe80::1453:dfff:fe8b:690/64
default via 10.244.1.250 dev eth0 mtu 1450
10.244.1.250 dev eth0 scope link
PING 10.96.157.184 (10.96.157.184) 56(84) bytes of data.
--- 10.96.157.184 ping statistics ---
2 packets transmitted, 0 received, 100% packet loss, time 1037ms
command terminated with exit code 1
No interface carries the ClusterIP, and ping stays
silent. A service translates ports (TCP 80, UDP 53 …);
ICMP has no port, so there is nothing to translate. “ping
doesn’t work” proves nothing for a ClusterIP – always test with
the real protocol.
What actually goes over the wire? tcpdump in the pod
while curl calls the ClusterIP:
# on netz
# timeout 4 tcpdump ... & capture in the background, ends after 2 packets or 4 s
# -c 2 only the first two packets (SYN and SYN-ACK)
kubectl exec werkzeug -- sh -c "timeout 4 tcpdump -ni eth0 -c 2 tcp port 80 or tcp port 8080 2>/dev/null & sleep 1; curl -s -o /dev/null http://$IP/hostname; wait"Expected (addresses, ports and times differ):
21:42:30.804241 IP 10.244.1.135.54928 > 10.244.1.32.8080: Flags [S], seq 2430736578, win 64860, options [mss 1410,sackOK,TS val 1676270001 ecr 0,nop,wscale 7], length 0
21:42:30.804435 IP 10.244.1.32.8080 > 10.244.1.135.54928: Flags [S.], seq 3157553470, ack 2430736579, win 64308, options [mss 1410,sackOK,TS val 462801241 ecr 1676270001,nop,wscale 7], length 0
curl called 10.96.157.184:80, but the wire
already shows the pod address with port
8080. Cilium translates at connect() in
the kernel, before the first packet is created (socket
LB). With kube-proxy you would see the ClusterIP here; the
translation would only happen on the node. For troubleshooting this
means: a capture filtered on the ClusterIP finds nothing in this
cluster.
And on the node? kube-proxy writes chains like
KUBE-SVC-… into iptables. Here:
# on netz
# How many KUBE-SVC chains are there on the worker? (the node is a Docker container)
sudo docker exec ndbg-worker sh -c "iptables-save | grep -c KUBE-SVC"
# Cilium's service table on the worker
C=$(kubectl -n kube-system get pod -l k8s-app=cilium --field-selector spec.nodeName=ndbg-worker -o name)
# sed -n "/$IP/,+1p" the line with the ClusterIP and the next one
kubectl -n kube-system exec $C -c cilium-agent -- cilium-dbg service list | sed -n "/$IP/,+1p"Expected (grep -c reports 0 matches
with exit code 1):
0
6 10.96.157.184:80/TCP ClusterIP 1 => 10.244.1.32:8080/TCP (active)
2 => 10.244.1.171:8080/TCP (active)
Not a single KUBE-SVC chain. The truth about services is
in Cilium’s eBPF tables: frontend (ClusterIP and port)
→ backends (pod and port), each with a state. If a
backend shows up there that does not exist, or one is missing, the fault
lies between Kubernetes and Cilium; if the table is correct, it lies in
the pod.
Save your progress? Sign up for free
Drill 4: Readiness decides
A pod that runs is not yet a pod that receives traffic. Take one out of rotation:
# on netz
# name of the first web pod
P1=$(kubectl get pod -l app=web -o jsonpath='{.items[0].metadata.name}')
# delete the ready file: from now on the readiness probe fails
kubectl exec $P1 -- rm /tmp/bereit
# wait for three probe runs
sleep 6
kubectl get pods -l app=web
# endpoints with their state
kubectl get endpointslices -l kubernetes.io/service-name=web -o jsonpath='{range .items[*].endpoints[*]}{.addresses[0]} ready={.conditions.ready} {.targetRef.name}{"\n"}{end}'
# Cilium table
kubectl -n kube-system exec $C -c cilium-agent -- cilium-dbg service list | sed -n "/$IP/,+1p"
# four requests: who answers?
for i in 1 2 3 4; do kubectl exec werkzeug -- curl -s http://web/hostname; echo; done | sort | uniq -cExpected (names and addresses differ):
NAME READY STATUS RESTARTS AGE
web-69856b5777-2np6w 0/1 Running 0 13s
web-69856b5777-p9vjn 1/1 Running 0 10s
10.244.1.221 ready=false web-69856b5777-2np6w
10.244.1.70 ready=true web-69856b5777-p9vjn
6 10.96.157.184:80/TCP ClusterIP 1 => 10.244.1.70:8080/TCP (active)
2 => 10.244.1.221:8080/TCP (maintenance)
4 web-69856b5777-p9vjn
Running, but 0/1: the pod is alive but gets
nothing. The EndpointSlice keeps it with ready=false,
Cilium sets it to maintenance. All four requests go to the
other pod. Put it back into rotation:
# on netz
# create the file again; after the next probe run the pod is ready
kubectl exec $P1 -- touch /tmp/bereit
sleep 5
kubectl get pods -l app=web --no-headers | awk '{print $1, $2}'Expected:
web-69856b5777-2np6w 1/1
web-69856b5777-p9vjn 1/1
If all pods are not ready, the service has no
endpoints, and a connection fails immediately with
Could not connect to server. That looks exactly like a
service that does not listen (Day 5) – which is why
kubectl get endpointslices belongs at the start of every
service troubleshooting session.
Save your progress? Sign up for free
Drill 5: NodePort
From outside the cluster there is no ClusterIP. A NodePort opens the same port on every node:
# on netz
# service type NodePort, fixed port 30080
kubectl patch svc web -p '{"spec":{"type":"NodePort","ports":[{"name":"http","port":80,"targetPort":"http","nodePort":30080}]}}'
kubectl get svc web
# from the VM to the worker and to the control plane
curl -s -m3 http://172.18.0.3:30080/hostname; echo
curl -s -m3 http://172.18.0.2:30080/hostname; echoExpected (the answering pod can change):
service/web patched
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
web NodePort 10.96.157.184 <none> 80:30080/TCP 2m38s
web-69856b5777-p9vjn
web-69856b5777-p9vjn
The control plane (172.18.0.2) answers too, although no
web pod runs there: Cilium on that node forwards the
connection to the worker. 80:30080 reads as “service port
80, 30080 on the nodes”.
Save your progress? Sign up for free
Exercise
A colleague creates a second service for the same pods:
# on netz
cd ~/ndbg/k8s
cat > shop.yaml <<'EOF'
apiVersion: v1
kind: Service
metadata: {name: shop}
spec:
selector: {app: web}
ports: [{port: 80, targetPort: 8081}]
EOF
kubectl apply -f shop.yaml“web works, shop doesn’t – and they are the
same pods.” Find the cause with three commands and fix it without
touching the pods.
Solution
# on netz
# 1: the error
kubectl exec werkzeug -- curl -sS -m3 http://shop/hostname
# 2: endpoints exist – but with which port?
kubectl get endpointslices -l kubernetes.io/service-name=shop
# 3: which port does the container listen on?
kubectl get pods -l app=web -o jsonpath='{.items[0].status.podIP} {.items[0].spec.containers[0].ports[0].containerPort}{"\n"}'Expected:
curl: (7) Failed to connect to shop:80 after 41 ms: Could not connect to server
command terminated with exit code 7
NAME ADDRESSTYPE PORTS ENDPOINTS AGE
shop-9f8kp IPv4 8081 10.244.1.221,10.244.1.70 4s
10.244.1.221 8080
Endpoints are there, so the selector is right. But they point to port 8081, while the container listens on 8080. Cross-check directly at the pod, then fix it:
# on netz
PIP=$(kubectl get pods -l app=web -o jsonpath='{.items[0].status.podIP}')
# wrong port: rejected; right port: answer
kubectl exec werkzeug -- curl -sS -m3 http://$PIP:8081/hostname
kubectl exec werkzeug -- curl -s -m3 http://$PIP:8080/hostname; echo
# correct targetPort in the service (JSON patch: replace exactly this one field)
kubectl patch svc shop --type=json -p '[{"op":"replace","path":"/spec/ports/0/targetPort","value":8080}]'
kubectl exec werkzeug -- curl -s -m3 http://shop/hostname; echoExpected:
curl: (7) Failed to connect to 10.244.1.221:8081 after 0 ms: Could not connect to server
command terminated with exit code 7
web-69856b5777-2np6w
service/shop patched
web-69856b5777-2np6w
Rule of thumb: no endpoints → selector or readiness.
Endpoints, but rejected → targetPort or
the service in the pod. Better than a number is a named
port as with web (targetPort: http): if the
port in the container changes, the service follows on its own.
Save your progress? Sign up for free
Challenge
The NodePort works from the VM. Now the client from Day
0 should reach it – that is, via the router, like a machine from another
network:
# on netz
sudo ip netns exec client curl -sS -m4 http://172.18.0.2:30080/hostnameExpected:
curl: (28) Connection timed out after 4001 milliseconds
Use tcpdump and counters to find out where the packet
gets stuck, and let it through. Day 11 helps – but not completely.
Solution
First the path: where does the SYN show up, and where does it stop showing up?
# on netz
# -i any all interfaces of the VM; the column after the time shows which one
sudo timeout 6 tcpdump -ni any -c 3 "tcp port 30080" 2>/dev/null &
sleep 1; sudo ip netns exec client curl -s -m3 http://172.18.0.2:30080/hostname >/dev/null; waitExpected (shortened):
21:44:19.801910 a-client P IP 10.1.0.10.60006 > 172.18.0.2.30080: Flags [S], ...
21:44:19.801920 a-router Out IP 10.1.0.10.60006 > 172.18.0.2.30080: Flags [S], ...
21:44:19.802001 up0 In IP 192.168.100.2.60006 > 172.18.0.2.30080: Flags [S], ...
The SYN arrives via the router (masqueraded to
192.168.100.2) on up0 – and never appears on
the kind bridge. The VM drops it. The first suspicion from Day 11: the
FORWARD policy DROP. So the rules for up0 → kind bridge in
DOCKER-USER, as learned there:
# on netz
# name of the kind bridge: br- plus the first 12 characters of the network ID
BR=br-$(sudo docker network inspect kind -f "{{.Id}}" | cut -c1-12); echo $BR
# allow from the router to the kind bridge, replies back
sudo iptables -I DOCKER-USER -i up0 -o $BR -j ACCEPT
sudo iptables -I DOCKER-USER -i $BR -o up0 -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
sudo ip netns exec client curl -sS -m4 http://172.18.0.2:30080/hostname
# Did the FORWARD policy drop anything at all?
sudo iptables -L FORWARD -v -n | head -1Expected (bridge name differs):
br-1eb80005bd1a
curl: (28) Connection timed out after 4001 milliseconds
Chain FORWARD (policy DROP 0 packets, 0 bytes)
Still nothing – and the policy counts 0 packets. So
the packet dies earlier, before routing. Before FORWARD comes
PREROUTING, and there is also the raw table:
# on netz
# raw table with counters
sudo iptables -t raw -L PREROUTING -v -n | head -5Expected:
Chain PREROUTING (policy ACCEPT 995K packets, 1632M bytes)
pkts bytes target prot opt in out source destination
11 660 DROP 0 -- !br-1eb80005bd1a * 0.0.0.0/0 172.18.0.2
0 0 DROP 0 -- !br-1eb80005bd1a * 0.0.0.0/0 172.18.0.3
0 0 DROP 6 -- !lo * 0.0.0.0/0 127.0.0.1 tcp dpt:43551
There are the eleven SYNs. Newer Docker versions protect every
container: packets to its address are dropped if they don’t come via its
own bridge – “direct access from outside” is blocked, access is only
possible via published ports. But a NodePort is not a port published by
Docker. For the lab you allow up0 explicitly in front of
it:
# on netz
# before Docker's DROP rules: accept packets from the router to the kind network
sudo iptables -t raw -I PREROUTING -i up0 -d 172.18.0.0/16 -j ACCEPT
sudo ip netns exec client curl -sS -m4 http://172.18.0.2:30080/hostname; echoExpected:
web-69856b5777-p9vjn
Two blocks in a row, both from Docker, both only found with counters:
raw against direct access, filter/FORWARD
against forwarding. The first fix wasn’t wrong, just not enough. The
three rules live in memory; Docker rebuilds its rules on every restart,
which is why Appendix A sets them again after
the rebuild.
Save your progress? Sign up for free
Checkpoint
# on netz
# two nodes ready
kubectl get nodes --no-headers | awk '{print $1, $2}'
# web with two ready pods, NodePort 30080
kubectl get deploy web --no-headers | awk '{print $1, $2}'
kubectl get svc web --no-headers | awk '{print $1, $2, $5}'
# tools pod is running
kubectl get pod werkzeug --no-headers | awk '{print $1, $3}'
# client reaches the NodePort
sudo ip netns exec client curl -s -m4 -o /dev/null -w '%{http_code}\n' http://172.18.0.2:30080/hostnameExpected:
ndbg-control-plane Ready
ndbg-worker Ready
web 2/2
web NodePort 80:30080/TCP
werkzeug Running
200
Clean up
You no longer need shop. The cluster, web
and werkzeug stay for Days 13 to 15.
# on netz
kubectl delete svc shopExpected:
service "shop" deleted from default namespace
Interview note
“A service is unreachable. How do you proceed?” First
kubectl get endpointslices: no endpoints → selector or
readiness; endpoints present → test targetPort and the
service in the pod directly via the pod IP. Then the translation itself:
with kube-proxy iptables-save | grep KUBE-SVC, with Cilium
cilium-dbg service list. Bonus points: ping to
a ClusterIP proves nothing, and with socket LB a capture in the pod
never sees the ClusterIP, but the pod address right away.
Enterprise note
In many clusters the question “kube-proxy or eBPF?” is documented
nowhere – and it decides which tools show anything at all during an
incident. Record in the operations manual who translates services, which
commands show the service table and whether socket LB is active. And
test NodePorts and LoadBalancers from outside after
every update of the container runtime or firewall: rules like Docker’s
in raw do not show up in any Kubernetes resource.