Remote debug a Spring Boot application in Kubernetes

Remote debug a Spring Boot application in Kubernetes

Sometimes, debugging an application locally is insufficient to pinpoint the underlying issue. Additionally, there are times when the infrastructure limitations do not allow one to debug an application. In such scenarios, remote debugging is a reasonable approach. However, that requires some preparations. In this article, we cover how to remote debug a Spring Boot application deployed in Kubernetes.

To remote debug a Spring Boot application deployed in Kubernetes, we have to take the following steps:

  • Enable JVM remote debug and expose a debug port
  • Expose the debug port through Kubernetes deployment
  • Port forward the debug port to the local

Enable JVM remote debug and expose a debug port

When running the application with the java command, we need to attach JWDP (Java Debug Wire Protocol) to the JVM process as follows:

$ java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005 \
-jar myApp.jar

That exposes the port 5005 as a debug port that we can attach to a remote debugger (e.g., IntelliJ debugger).

For Kubernetes deployment, you should apply the changes to your Dockerfile and build the image again.

Expose the debug port through Kubernetes deployment

The next step is to expose the debug port (5005 in this case) in Kubernetes and make it accessible through the cluster first and then outside via port forwarding.

For that, we need to edit the Kubernetes deployment file. For Helm, you should edit the values.yml file in your Helm chart. Nonetheless, go through your deployment. Most probably your application is already exposing a port. Just find it and add a line after that. For example,

- containerPort: 8080
  name: http
- containerPort: 5005
  name: debug

Of course, if you don’t want to go through the process of publishing a Helm chart, you can edit the deployment via the kubectl command,

$ kubectl -n [namespace] edit deployment [deployment_name]

Port forward the debug port

The last step is to port forward the debug port, 5005, to local. That can be done via kubectl as follows:

$ kubectl -n [namespace] port-forward [pod_name] 5005:5005

Lastly, we must attach the debugger to the remote JVM on port 5005. For that purpose, we use IntelliJ.

We need to create a remote debug configuration like below,

Remote Debugger IntelliJ

Conclusion

In this article, we covered how to remote debug a Spring Boot application in Kubernetes. Although the process is not as simple as local debugging, it requires only a few small steps to connect to a Spring Boot application running in a Kubernetes pod to debug.

Inline/featured images credits