How it works
DebugHero has a daemon on your machine and a debug container that it adds to your pod. The daemon uses your kubeconfig, in the same way as kubectl. Nothing is installed in the cluster before a session starts.
Components
| Component | Runs on | What it does |
|---|---|---|
debugd | Your machine | A daemon that listens on port 58090. It starts debug sessions with the Kubernetes API, forwards ports, and keeps the status of each session. |
debugctl | Your machine | A CLI for debugd. It starts sessions, lists them, shows the Delve logs, and logs you in and out. |
| IDE plugin | Your machine | The VS Code extension or the GoLand plugin. It starts a session through debugd and connects the IDE debugger to Delve. |
| Debug container | Target pod | An ephemeral container with Delve 1.25 and a small HTTP server. DebugHero adds it to your pod for the session. |
Which cluster DebugHero uses
DebugHero finds the kubeconfig in the same order as kubectl:
- The
--kubeconfigflag ofdebugctl debug, or the Kubeconfig path field in the IDE. - The
KUBECONFIGenvironment variable.debugctlreads it from your shell. The IDE plugins read it from the environment of the IDE and show it in the Kubeconfig path field. ~/.kube/config.
KUBECONFIG can be a list of files (: on Linux and macOS). DebugHero merges the files, the same as kubectl. DebugHero uses the current context of the merged config.
If kubectl get pods works in your shell, DebugHero uses the same cluster.
debugd does not use its own environment for this. It often starts from another environment than your shell, for example from launchd or systemd. The CLI and the IDE plugins send the value to debugd.
A debug session
This is what happens when you start a session.
1. Start the session
The IDE plugin or debugctl debug sends the session details to debugd: the namespace, the pod labels, and optionally the container name, the process name, and an application port. debugd checks that you are logged in and that your plan allows a new session.
2. Find the pod and the container
debugd lists the pods in the namespace that match the labels and uses the first one. In that pod, it uses the container that you named. If you did not name a container, it uses the first container of the pod.
3. Add the debug container
debugd adds an ephemeral container with a new name, for example debughero-x7k2p9, to the pod, through the pods/ephemeralcontainers subresource. The pod does not restart. The container:
- targets your container (
targetContainerName), so that it sees the processes of your container, - runs as root (UID 0) with the
SYS_PTRACEcapability, which Delve needs to attach to a process of another user, - uses the
RuntimeDefaultseccomp profile.
debugd waits until the container runs.
4. Find the process
debugd lists the processes in your container. If you gave a process name, it uses the process with that name. Otherwise, it uses PID 1.
5. Attach Delve
debugd forwards the port of the server in the debug container. Then it asks the server to start Delve. Each request has your access token. The server finds the process and starts Delve:
dlv --listen=:<port> --headless=true --continue --accept-multiclient \
--only-same-user=false --api-version=2 attach <pid>
Delve listens on a random port between 40000 and 40999 inside the pod. --continue lets your process run until it reaches a breakpoint.
6. Forward the ports
debugd forwards the Delve port to a free port on localhost on your machine. If you gave an application port, it forwards that port too, with the same number on your machine.
7. Map the source paths
The debug server reads your binary through /proc/<pid>/root and finds the path where the binary was built. The IDE uses this path to map the files in the binary to your local source files. This is the substitutePath setting of Delve.
8. Connect the IDE
The session status changes to READY. Your IDE connects to the forwarded Delve port: VS Code through the Go extension (dlv-dap), GoLand through a Go Remote run configuration.
While the session runs, debugd checks the debug container every 5 seconds. If the container stops, for example because it ran out of memory, the session status changes to FAILED.
What changes in your cluster
| Change | How long it stays |
|---|---|
| The debug container runs, with Delve attached to your process | Until you stop the session, or 1 hour at most. When the session stops, Delve detaches, and your process continues to run. Then the debug container stops. |
An ephemeral container (debughero-<random>) in the pod spec | Until the pod is replaced. Kubernetes cannot remove an ephemeral container from a pod. Each session uses a new name, so you can start more sessions on the same pod. |
DebugHero does not change your Deployment or other workload objects. It does not install CRDs, operators, or other cluster-wide resources.
What stays on your machine
| Item | Location |
|---|---|
debugd logs | ~/.debughero/logs/debugd.log, rotated |
| Login credentials | The OS keychain (macOS Keychain, or the Secret Service on Linux) |
| Port-forwards | localhost only |