Monitoring JVM within a Docker container using Stackdriver

Rahul Gupta
Jun 10 · 4 min read

Introduction

Now that you have landed on this post, you must be running your Dockerized Java app inside a Google Compute Engine (GCE) VM instance and obviously want to monitor it. Well this post will show you how you can monitor the JVM using Stackdriver Monitoring agent and JVM plugin. Though there are many other methods available on the web that make use of different monitoring tools but why not use a GCP native tool to accomplish the same? It does make it simpler after all.

Java applications run inside a virtual machine called Java Virtual Machine (JVM). Java provides a set of tools, for management and networking of Java applications, called Java Management Extensions (JMX). So JMX basically enables monitoring of JVMs out of the box, all we need to do is enable it on a specific port that out agent can read and get all the good stuff like thread count, memory usage, garbage collection, open file descriptors, etc.

Deploying the Java App

If you already have the JMX remote enabled on your containerized app then you may move to the next section otherwise read on…

Within the dockerfile, ensure that the Java command to run the application includes the system properties to enable JMX remoting.

For example, if your java command to start the app is:

then you modify it to look like below:

Some things to note:

  • Choose a valid port of your choice instead of JMX_PORT (Example: 9001).
  • For this example, we disabled the SSL and authentication to JMX. Please enable and configure them if your use case requires it.

Create a GCE VM instance running linux, make sure a service account is attached to the VM instance while creating it. Once the instance is created, install Docker on it. Deploy the container using docker run command ensuring the JMX port is listening on the container.

  • JMX_LOCAL_PORT is the port mapped on GCE VM.
  • JMX_PORT is the same port that was set earlier within Dockerfile while creating the image.

Enable Stackdriver Monitoring

Now that the container is up and running with JMX listener enabled, we will install Stackdriver Monitoring agent and JVM plugin that will query the metrics from container and forward to Stackdriver Monitoring service.

For the agent to be able to upload metrics to Stackdriver Monitoring, it must have necessary IAM roles assigned to it at the project level. Grant roles/monitoring.metricWriter to the GCE instance service account at project level.

You can read more on setting up access control for Stackdriver Monitoring here.

Install the Stackdriver monitoring agent for linux.

You can read more on installing the Stackdriver monitoring agent here.

Enable Stackdriver JVM monitoring plugin by downloading and adding the configuration file to agent configuration.

Edit the downloaded JMX plugin configuration file (jvm-sun-hotspot.conf) to replace JMX_PORT with JMX_LOCAL_PORT port number that was set earlier while running docker container.

Restart the monitoring agent to let it pick up JVM plugin configuration.

Verify on Stackdriver Monitoring Console

Now that the agent is setup to collect and export JVM metrics, it is time to verify the same on Stackdriver Monitoring console.

Go to Stackdriver Monitoring console and select the workspace of your project. If it is a new project then a new workspace will be created for this project by default, alternatively you can add your project to existing workspace.

Read more on Stackdriver workspaces here.

On the left hand panel, navigate through Resources > Java Virtual Machine

Stackdriver Monitoring console showing JVM resource selector.

After you click on Java Virtual Machine and if everything was configured correctly, you should see your GCE instance name and the JVM metric charts.

Stackdriver JVM Monitoring window showing different collected metrics.

If you are unable to see your GCE VM Instance name on JVM monitoring console then follow the different suggestions in Stackdriver Monitoring agent troubleshooting guide.

Google Cloud Platform - Community

A collection of technical articles published or curated by Google Cloud Platform Developer Advocates. The views expressed are those of the authors and don't necessarily reflect those of Google.

Rahul Gupta

Written by

Strategic Cloud Engineer @ Google

Google Cloud Platform - Community

A collection of technical articles published or curated by Google Cloud Platform Developer Advocates. The views expressed are those of the authors and don't necessarily reflect those of Google.