DEV Community

Brachi Packter
Brachi Packter

Posted on • Edited on

2

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

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay