DEV Community

Brachi Packter
Brachi Packter

Posted on • Edited on

Intelij remote debugging java app in GKE (Google K8S Engine)

Prepare the docker image

In order to debug a java application we need to add some command line arguments to the remote app.

  1. Set the arguments in the kubernetes deployment payload
DEBUG_OPTIONS="-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005"
  1. Pass it to the java command in Dockerfile
ENTRYPOINT [ "sh", "-c", "java $DEBUG_OPTIONS -jar app.jar" ]

Expose the port 5005 in the GKE service

In GCP console, go to Kubernetes Engine -> Service & Ingress, choose your service and add the debug port:

ports:
  - name: http
    nodePort: 30060
    port: 80
    protocol: TCP
    targetPort: 8080
  - name: debug
    nodePort: 30080
    port: 5005
    protocol: TCP
    targetPort: 5005

Forward the pods ports to your localhost:5005

gcloud container clusters get-credentials ${CLUSTER_NAME} --region us-central1 --project ${PROJECT_NAME} \
 && kubectl port-forward --namespace ${NAMESPACE} $(kubectl get pod --namespace ${NAMESPACE} --selector="app=${APP}" --output jsonpath='{.items[0].metadata.name}') 5005:5005

You can generate this command from the google console by clicking on the button "port forwarding" near your debug port:

Alt Text

And now debug it as usual from Intelij
Alt Text

Top comments (0)