Skip to main content

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.

How DebugHero connects your IDE to DelveThe IDE plugin calls debugd on your machine. debugd uses the Kubernetes API to add an ephemeral container to the target pod and to forward the Delve port. Delve in the ephemeral container attaches to your Go process.your machineIDE pluginVS Code, GoLanddebugdlocalhost:58090Kubernetes clustertarget podapp containeryour Go processephemeral containerdebug server + dlv (Delve)HTTPKubernetes API:ephemeral container,exec, port-forwardptrace attach
debugd runs on your machine and uses your kubeconfig. Nothing is installed in the cluster in advance.

Components​

ComponentRuns onWhat it does
debugdYour machineA daemon that listens on port 58090. It starts debug sessions with the Kubernetes API, forwards ports, and keeps the status of each session.
debugctlYour machineA CLI for debugd. It starts sessions, lists them, shows the Delve logs, and logs you in and out.
IDE pluginYour machineThe VS Code extension or the GoLand plugin. It starts a session through debugd and connects the IDE debugger to Delve.
Debug containerTarget podAn 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:

  1. The --kubeconfig flag of debugctl debug, or the Kubeconfig path field in the IDE.
  2. The KUBECONFIG environment variable. debugctl reads it from your shell. The IDE plugins read it from the environment of the IDE and show it in the Kubeconfig path field.
  3. ~/.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.

note

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_PTRACE capability, which Delve needs to attach to a process of another user,
  • uses the RuntimeDefault seccomp 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:

Delve command in the debug container (log flags not shown)
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​

ChangeHow long it stays
The debug container runs, with Delve attached to your processUntil 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 specUntil 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​

ItemLocation
debugd logs~/.debughero/logs/debugd.log, rotated
Login credentialsThe OS keychain (macOS Keychain, or the Secret Service on Linux)
Port-forwardslocalhost only

Next steps​