Java Remote Debug on local cloud stands (docker/Kubernetes), obvious things (as it seemed to me)

Sometimes we can't catch a bug locally, the application is too complex in configuration or just crooked hands and QA can reproduce it in 2 clicks, unlike us.

Let our stands be either Docker or Kubernetes clusters.

  1. Enable debugging in the JVM process:

agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005 

Example Dockerfile:

# Use an official OpenJDK runtime as a parent image
FROM openjdk:17-jdk-alpine

# Set the working directory in the container
WORKDIR /app

# Copy the jar file into the container at /app
COPY target/myapp.jar /app/myapp.jar

# Expose the application port and the debug port (8080), DO NOT EXPOSE DEBUG PORT BY DEFAULT!!
EXPOSE 8080 8080

# Specify the command to run the Java application with remote debugging enabled
CMD ["java", "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005", "-jar", "myapp.jar"] 

2. Open the port:

Docker:

docker stop <container_id>
docker rm <container_id>
docker run -p 8080:8080 -p 5005:5005 my-java-app
*если вам важно сохранить состояние то не забудьте: docker commit <container_id> new_image_name

Kubernetes:

service.yaml

apiVersion: v1
kind: Service
metadata:
  name: myapp-service
spec:
  selector:
    app: my-java-app # This should match the label of the pod
  ports:
  - protocol: TCP
    port: 8080   # Port exposed by the service
    targetPort: 8080   # Port the container listens on
  - protocol: TCP
    port: 5005   # Port exposed by the service for debugging
    targetPort: 5005   # Debug port in the container
  type: NodePort  # Exposes the service on a port on each node
kubectl apply -f service.yaml

or simply: kubectl port-forward pod/<pod_name> 8080:8080 5005:5005

Pod IP: kubectl get pod <pod_name> -o wide (для подключения дебагера)

3. We connect our debugger via port and debug.

  1. Close debug ports. Do not ignore this step, otherwise you will get a breakpoint stop on production.

If you have rancher or at least argo-cd some of the steps are done through the UI, but “this is not our method”

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *