Debug Spring Boot Apps on Red Hat OpenShift4
This story begins with a question from an OpenShift user about how to debug a Spring Boot application that runs as a container in OpenShift. Considering that quite a lot of coder friends use this framework as a microservice application, so I tried to make a tutorial for remote debugging java applications on OpenShift4.
Below picture shows this tutorial flow
Prerequisite
- Preinstalled OpenShift Container Platform 4 (OCP4)
- OpenShift client command line tools (oc tools) atau k8s cli. Download here.
- Your favorite IDE. Publisher uses Red Hat CodeReady Studio (Eclipse based) and VSCode for this tutorial.
- Deploy your Springboot apps on OpenShift (sample apps on GitHub). This time i use s2i method on OpenShift web console.
Step 1 : Set Debug Mode as Environment Variable
Add new environment variable JAVA_DEBUG=true on your Deployment or DeploymentConfig. Below picture use OpenShift web console to add environment variable.
Or, using oc or kubectl command line below:
$ kubectl set env deployment/springboot-sample-apps-git JAVA_DEBUG=true
on startup log, find below information for debug port.
Listening for transport dt_socket at address: 5005
Note: s2i java use port 5005 as default debug port. it might different based on your preferred base image.
Step 2 : Port Forward Debug port to Your Local Machine
find running pods on your namespace:
$ kubectl get pods | grep Running....springboot-sample-apps-git-XXXX 1/1 Running
Port forward the debug port on target pod
$ kubectl port-forward springboot-sample-apps-git-XXXX 5005:5005....Forwarding from 127.0.0.1:5005 -> 5005
Forwarding from [::1]:5005 -> 5005
Step 3 : Setup Remote Debug on Your IDE
Go to Debug Configuration > Remote Java Application
Connection Type: Standard (Socket Attach)
Host: localhost
Port: 5005
if you use VSCode, add below configuration launch.json on .vscode folder.
{ "version": "0.2.0", "configurations": [ { "type": "java", "name": "Debug (Attach)", "projectName": "springboot-sample-apps", "request": "attach", "hostName": "localhost", "port": 5005 }
]}
Step 4 : Voila! Debug Your Applications
Open apps on browser http://$YOUR_APPS_ROUTE/swagger-ui.html , try to execute /api/v1/home on swagger sandbox
Debug breakpoint on VSCode: