Sub topic 3
Setting up a debugging toolbox
This chapter assumes you can use kubectl get, describe, logs and exec. It also assumes you know what Pods, Deployments and namespaces are, and that you understand IP addresses, ports and DNS A records. Before you debug a Service, you need a way to look at the cluster from the same position as your application. This chapter sets up that toolbox.
Why the viewpoint matters
Your laptop is outside the cluster. Service IPs and cluster DNS names usually do not work from there. A test that fails on your laptop tells you little about what a Pod sees. Run your tests from inside the cluster.
You have three main ways to do that:
| Tool | Use it when | Cost |
|---|---|---|
| `kubectl exec` | The target Pod's image already has `curl`, `wget` or `nslookup` | None, but many production images have no shell |
| Temporary Pod | You want a clean client with known tools | Starts a new Pod, with its own network identity |
| Ephemeral debug container | You need the exact network namespace of one Pod | Needs a cluster version that supports it, and permission to use it |
A target to debug
Apply this Deployment and Service. It works as written, so you can break each part on purpose in later chapters.
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: nginx:1.27
ports:
- containerPort: 80
readinessProbe:
httpGet:
path: /
port: 80
---
apiVersion: v1
kind: Service
metadata:
name: web
spec:
selector:
app: web
ports:
- port: 8080
targetPort: 80A temporary client Pod
A temporary Pod gives you a shell with basic tools. The --rm flag deletes it when you exit. --restart=Never makes it a plain Pod instead of a managed one.
kubectl run tmp-debug --rm -it --restart=Never --image=busybox:1.36 -- sh
# inside the Pod:
nslookup web.default.svc.cluster.local
wget -qO- http://web.default.svc.cluster.local:8080The first command tests DNS. The second tests the Service on its port, 8080. If the first works and the second fails, the problem is past DNS. Keeping those two steps separate is the basis of the workflow in the last chapter.
An ephemeral debug container
Sometimes you need to test from a specific Pod. An ephemeral container is a temporary container added to a running Pod. It shares the Pod's network namespace, so it sees the same IP, DNS settings and network policy treatment as the application.
kubectl debug -it web-7d9c5b6f4-abcde --image=nicolaka/netshoot --target=webReplace the Pod name with one from kubectl get pods. The netshoot image includes curl, dig, tcpdump and other tools, so you do not have to rebuild the application image.
What to read first
Start with the Service and its Pods. These commands show whether the Service has endpoints and whether the selector matches running, ready Pods.
kubectl get service web -o wide
kubectl get endpointslices -l kubernetes.io/service-name=web
kubectl get pods -l app=web -o wide
kubectl describe service webCompare three things: the SELECTOR column of the Service, the labels on the Pods, and the addresses in the EndpointSlice. Later chapters explain each in detail.
Exercise
Why might curl to a Service IP from your laptop fail even though the Service is healthy, and what is a better test?
Show answer
Service IPs and cluster DNS names are only reachable from inside the cluster network. Run the test from a temporary Pod or an ephemeral debug container, so the request starts where your application's requests start.
Exercise
You need to test connectivity from one specific Pod, but its image has no shell or curl. Which tool do you use, and which flag selects the container to share a namespace with?
Show answer
Use kubectl debug with a tools image such as nicolaka/netshoot. The --target flag names the container whose process namespace to share. The network namespace is shared by the whole Pod.