在运行在ECS集群上的Docker容器中公开JMX端口

我有一个在Docker容器中运行的Java应用程序。 我已经在ECS集群中部署了这个容器。 我想公开一个JMX端口,这样我就可以使用安装在该机器上的CollectD代理收集JVM统计信息。

我在Java应用程序中指定的JVM参数是

JAVA_OPTS="-Dspring.config.location=classpath:/base/ -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=8008 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Djava.net.preferIPv4Stack=true -Djava.rmi.server.hostname=10.0.7.118 

如果我在非docker环境中运行这个应用程序,我能够连接到这个JMX端口。 但是,我无法在Docker中做同样的事情。

我在“我的任务定义”中也给出了端口映射,所以这个端口可以暴露给外部世界。 我知道,如果我使用docker run命令运行这个docker,比我可以指定-p参数来进行端口映射,但是我不能在这里做同样的事情,因为我在部署这个映像的ECS集群上运行这个应用程序。 所以我必须依靠任务定义提供的端口映射。

TaskDefnition

  "ContainerDefinitions": [ { "Name": "MyApplication", "Cpu": "2048", "Essential": "true", "Image": "location of the image", "Memory": "8192", "MemoryReservation": "4096", "Environment": [ { "Name": "Test", "Value": { "Fn::GetAtt": [ "SomeAttrib", "SomeAccessKey" ] } } ], "PortMappings": [ { "HostPort": "8080", "ContainerPort": "8080" }, { "HostPort": "8008", "ContainerPort": "8008" } ] } 

通过各种链接后,我find了解决我的问题。 最后,JVM参数看起来像这样

 -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=8008 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Djava.net.preferIPv4Stack=true -Djava.rmi.server.hostname=127.0.0.1 -Dcom.sun.management.jmxremote.rmi.port=8008 -Dcom.sun.management.jmxremote.local.only=true 

添加-Dcom.sun.management.jmxremote.local.only = true为我做了诡计。 您可以根据您的要求将其设置为true或false。