在 AWS Fargate 中监控 JVM

Joh*_*ohn 5 java spring jmx docker aws-fargate

我目前一直在尝试将 VisualVM(一个监控 JVM、堆和内存使用情况等的程序)连接到在 Docker 容器中的 AWS Fargate 上运行的 Spring Boot 应用程序(Java 应用程序)。

我一直在相应地公开 JMX 端口,并且在本地运行 Docker 容器时,我能够通过 JMX 端口进行连接。但是,在 Fargate 上运行 Java App 时,我还没有找到通过 JMX 连接到 Container 的方法。我尝试将 VM 参数 -Djava.rmi.server.hostname 设置为容器的 IP 地址,但是当我尝试通过 JMX 连接时,它仍然无法这样做。有没有人有这方面的经验?

供参考的 JMX 命令:

-Dcom.sun.management.jmxremote \
-Dcom.sun.management.jmxremote.local.only=false \
-Dcom.sun.management.jmxremote.authenticate=false \
-Dcom.sun.management.jmxremote.ssl=false \
-Djava.rmi.server.hostname=172.17.0.2 \
-Dcom.sun.management.jmxremote.port=9090\
-Dcom.sun.management.jmxremote.rmi.port=9090\
-jar java-api.jar server```

Run Code Online (Sandbox Code Playgroud)

Sne*_*tel 8

以下更改有助于我将 Visual VM 连接到 AWS Fargate(私有 VPC)中部署的 Spring Boot 应用程序

  • jvm参数
-Dcom.sun.management.jmxremote \
-Dcom.sun.management.jmxremote.port=1099 \
-Dcom.sun.management.jmxremote.rmi.port=1099 \
-Dcom.sun.management.jmxremote.ssl=false \
-Dcom.sun.management.jmxremote.authenticate=false \
-Dcom.sun.management.jmxremote.local.only=false \
-Djava.rmi.server.hostname=127.0.0.1
Run Code Online (Sandbox Code Playgroud)
  • 导出端口 1099(在 dockerfile 和 cloudformation 模板中 - PortMappings->ContainerPort)

  • 容器安全组接受来自 vpc(跳转服务器)中现有 EC2 之一的 1099(tcp 和 udp)传入流量

  • 使用EC2(跳转服务器)将ssh端口转发到fargate中运行的任务(使用fargate中运行的任务的私有IP)

在本地运行以下命令

ssh -l <user> -L 127.0.0.1:1099:<task-private-ip-in-fargate>:1099 <ec2-ip(jump server)>
Run Code Online (Sandbox Code Playgroud)
  • 在 127.0.0.1:1099 上使用 JMX 连接来连接 VisualVM