This guide walks you through setting up a remote debugging environment for Go applications deployed on a Kubernetes cluster using Delve and VS Code. The steps apply to any Go service on Kubernetes, using the Scality COSI driver as an example.
Ensure the following are installed and configured before starting:
-
Kubernetes Cluster: A running Kubernetes cluster, such as Minikube, Docker Desktop Kubernetes, or a remote cluster.
-
kubectl: Installed and configured for your cluster.
-
VS Code: Installed, with the Go extension.
-
COSI Driver: Clone the repository and navigate to the directory:
git clone git@github.com:scality/cosi-driver.git && cd cosi-driver
-
Delve: Installed locally.
Build the Docker image with Delve by running:
make delve
Deploy the COSI driver to Kubernetes using Kustomize. This deployment is configured to run Delve in wait mode, meaning it won’t start the COSI service until a debugger attaches.
kubectl apply -k kustomize/overlays/debug
Wait until the pod is ready to ensure the deployment succeeded:
kubectl wait --namespace container-object-storage-system --for=condition=ready pod --selector=app.kubernetes.io/name=scality-cosi-driver --timeout=120s
Identify the pod name for the COSI driver:
kubectl get pods -n container-object-storage-system
Forward port 2345
from the Kubernetes pod to your local machine to connect VS Code to the Delve debugger:
kubectl port-forward -n container-object-storage-system pod/<pod-name> 2345:2345
Replace <pod-name>
with the actual name of the pod.
-
Confirm Delve is installed locally.
-
Open VS Code and create a
launch.json
file under the.vscode
directory. -
Add the following configuration to
launch.json
:{ "version": "0.2.0", "configurations": [ { "name": "Remote Debug Scality COSI Driver", "type": "go", "request": "attach", "mode": "remote", "remotePath": "/app", "port": 2345, "host": "127.0.0.1", "apiVersion": 2, "trace": "verbose" } ] }
- Run Port Forwarding: Ensure port forwarding is active (Step 4).
- Initiate Debugging in VS Code:
- Open VS Code, set breakpoints in your Go code, and press F5 to start debugging with the "Remote Debug Scality COSI Driver" configuration.
- Inspect Variables and Stack: You can now inspect variables, step through the code, and debug your Go application as it runs on Kubernetes.
- Delve Not Found: Verify Delve is installed in your Docker image and available at
/dlv
. - Port Forwarding Issues: Confirm the Kubernetes pod is running, and that port
2345
is open. - Breakpoints Not Hit: Ensure the code in Kubernetes matches the local code in VS Code.
- Connection Timeout: Check firewall rules, network policies, and Kubernetes pod permissions if the debugger cannot connect.