Friday, June 13, 2014

Enable JMX on Amazon EC2

I ran into an issue this last week enabling JMX for a Java process on an Amazon EC2 instance.  After a couple of hours researching (and finding a couple of answers on StackOverflow), I wanted to share a complete answer to how this is done.

1.  Unblock RMI and JMX Ports.

First, you have to ensure both the JMX and RMI ports are available to the process that's going to make the JMX connection.  One issue many devs/admins are having within EC2 is that their JMX clients are unable to establish a connection to RMI.  Therefore, you need to make sure your instance's security policy, as well as your firewall (if using one), allow both ports to be accessed.  In this example, I'm specifying 9011 and 9010 for JMX and RMI respectively.

2.  Resolve the Public DNS Name of your EC2 Instance.

Another problem is that when your JMX client makes a connection, the JMX server returns the wrong connection information for the server's RMI port.  If you don't override the java.rmi.server.hostname, the server will return the default hostname for the EC2 instance, which is not publicly resolvable.  To fix this, you can request the public DNS name of an instance from the "metadata server" within EC2.  A call made by an EC2 instance to the URL will return the public DNS name of that instance.  In the example below, you will see the call made using wget for this information.

3.  Prefer the IPv4 Stack.

Finally, the last issue we ran into was the JMX server binding to IPv6 addresses instead of IPv4.  You can instruct Java to prefer the IPv4 stack with the following flag:

The rest of the configuration is pretty standard.  This is a fragment of a BASH script showing the composition of JAVA_OPTS you may use when starting your Java process:




RMI_HOST=`wget -q -O -`

JAVA_OPTS="${JAVA_OPTS} -Djava.rmi.server.hostname=${RMI_HOST}"


I would like to thank the StackOverflow commentors for help with this issue.  You can see the original posts on these threads:

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.