How to monitor java application memory usage in Docker

邮差的信 提交于 2019-11-30 05:09:59

To connect to a java process running in a docker container running in boot2docker with visualvm you can try the following:

Start your java process using the following options:

java -Dcom.sun.management.jmxremote.port=<port> \
-Dcom.sun.management.jmxremote.authenticate=false \
-Dcom.sun.management.jmxremote.ssl=false \
-Dcom.sun.management.jmxremote.rmi.port=<port> \
-Djava.rmi.server.hostname=<boot2docker_ip> \
<Main>

You need to run your image with --expose <port> -p <port>:<port>.

Then "Add JMX Connection" in visualvm with <boot2docker_ip>:<port>.

It shouldn't be much different without boot2docker.

To monitor it's usage, you need to get it's real Process ID. If you are running tomcat directly in the container, then it should be:

DOCKER_ROOT_PROC=`(docker inspect -f "{{ .State.Pid }}" my_container)`

If you are using something like Phusion's baseimage, then your java process will be a child of that process. To see the hierarchy use:

pstree $DOCKER_ROOT_PROC

Once you have that, you can write your script using

ps -o pid,cmd --no-headers --ppid $DOCKER_ROOT_PROC

In your script recursively to find the java process you want to monitor (with some Regular Expression filtering, of course). Then finally you can use this to get your java application's memory usage in kilobytes:

ps -o vsz -p $JAVAPROCESS

I don't know if this can be used with jconsole, but it is a way of monitoring the memory usage.

xh3b4sd

To monitor docker containers I recommend Google's cAdvisor project. That way you have a general solution to monitor docker containers. Just run your app, whatever that is, in a docker container, and check things like cpu and memory usage. Here you have an http API as well as a web ui.

lucasvc

I tried the Pierre's answer (also answered here) but no way.

At the end I could connect using a SSH tunnel.

cAdvisor mentioned above will not help with monitoring Tomcat running inside the container. You may want to take a look at SPM Client docker container, which does exactly that! It has the agents for monitoring a number of different applications running in Docker - Elasticsearch, Solr, Tomcat, MySQL, and so on: https://github.com/sematext/docker-spm-client

For the memory usage monitoring of your application in Docker, you can also launch an ejstatd inside your Docker container (calling mvn -Djava.rmi.server.hostname=$HOST_HOSTNAME exec:java -Dexec.args="-pr 2222 -ph 2223 -pv 2224" & from the ejstatd folder before launching your main container process), exposing those 3 ports to the Docker host using docker run -e HOST_HOSTNAME=$HOSTNAME -p 2222:2222 -p 2223:2223 -p 2224:2224 myimage.

Then you will be able to connect to this special jstatd daemon using JVisualVM for example, adding a "Remote Host" specifying your Docker hostname as "Host name" and adding a "Custom jstatd Connections" (in the "Advanced Settings") by setting "2222" to "Port".

Disclaimer: I'm the author of this open source tool.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!