Java Remote Debug на стендах в локальных облаках (docker/Kubernetes), очевидные вещи (как мне казалось)

от автора

Иногда мы не можем локально поймать баг, приложение слишком сложно в конфигурации или просто кривые руки и QA может воспроизвести в 2 клика, в отличии от нас.

Пусть наши стенды это или docker или Kubernetes кластеры.

  1. Включаем дебаг в JVM процессе:

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

Пример 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. Открываем порт:

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

ну или просто : kubectl port-forward pod/<pod_name> 8080:8080 5005:5005

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

3. Подключаем наш дебагер по порту, дебажим.

  1. Закрываем дебажные порты. Не стоит игнорировать этот шаг, иначе словите breakpoint stop на проде.

Если у вас стоит rancher или хотя бы argo-cd часть шагов делает через UI , но «это не наш метод»


ссылка на оригинал статьи https://habr.com/ru/articles/844250/


Комментарии

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *