How to monitor java application memory usage in Docker

前端 未结 6 1185
刺人心
刺人心 2020-12-29 12:04

I run the java web application on tomcat in the Docker container.

Is there any way to monitor the memory usage of the java application? I try to use jconsole<

相关标签:
6条回答
  • 2020-12-29 12:32

    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.

    0 讨论(0)
  • 2020-12-29 12:36

    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.

    0 讨论(0)
  • 2020-12-29 12:37

    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.

    0 讨论(0)
  • 2020-12-29 12:44

    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.

    0 讨论(0)
  • 2020-12-29 12:53

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

    At the end I could connect using a SSH tunnel.

    0 讨论(0)
  • 2020-12-29 12:57

    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

    0 讨论(0)
提交回复
热议问题