Video in progress

Strong Middle ⚙️ JVM and memory

What is JMX, and what do you configure to monitor a prod JVM through JMX safely?

JMX is a standard that exposes the internal state of the JVM and the application through MBeans: heap, GC, threads, connection pools. Remote connections are closed by default; if you open them and leave out authentication and TLS, you open the way to remote code execution. On prod I usually keep JMX local and export metrics through Micrometer/Prometheus.

📖 Answer

Every JVM has MBeans like java.lang:type=Memory, java.lang:type=GarbageCollector,name=G1 Young Generation and java.lang:type=Threading. HikariCP, Tomcat and the Kafka client register their own MBeans too. VisualVM and JMC read them through JMX.

Opening it remotely:

-Dcom.sun.management.jmxremote.port=9010
-Dcom.sun.management.jmxremote.rmi.port=9010
-Dcom.sun.management.jmxremote.authenticate=true
-Dcom.sun.management.jmxremote.ssl=true
-Dcom.sun.management.jmxremote.access.file=/etc/jmx/access
-Dcom.sun.management.jmxremote.password.file=/etc/jmx/password

Half the articles on the internet give it with authenticate=false and ssl=false, and that is an open door. Through JMX you can load an MBean or call diagnostic operations, which in practice is remote code execution.

The rules:

  • the port only on the internal network, restricted with a firewall or NetworkPolicy;
  • the password file with 600 permissions (otherwise the JVM won't start);
  • if possible, only through kubectl port-forward or an SSH tunnel;
  • a readonly role is enough for monitoring.

On prod I often do it differently: I don't open JMX to the outside at all. Spring Boot Actuator + Micrometer expose the same data (jvm.memory.used, jvm.gc.pause, hikaricp.connections.active) over HTTP in Prometheus format. For old JMX-only systems there is the Prometheus JMX exporter agent. If you need deep diagnostics, use jcmd inside the pod: it works through a local attach and doesn't need a network port.