Remote JMX Connection to Openhift (or Kubernetes) pod


11 May 2019  Michal Fabjanski  1 min read.

Remote Debugging Applications on Openshift pods

In this post I would like to share my last challenge at work - remote debugging of an application running on Openhift. This method is also valid for Kubernetes pods.

Set up JMX

The first thing you have to do is to enable and configure JMX flags. Start your program with following parameters:

-Dcom.sun.management.jmxremote 
-Dcom.sun.management.jmxremote.port=3000
-Dcom.sun.management.jmxremote.rmi.port=3001
-Dcom.sun.management.jmxremote.ssl=false 
-Dcom.sun.management.jmxremote.authenticate=false 
-Djava.rmi.server.hostname=127.0.0.1

JMX use RMI for the communication between the JMX client and the remote JVM. That’s why I’ve set two ports. JMX client will connect to hostname - 127.0.0.1:3001. You probably wonder how it can work if there is no RMI server running on localhost. This is because the next step is to set up Openshift/Kubernetes port forwarding.

Port forwarding

Thanks to port forwarding feature you can forward one or more local ports to a pod. You need to be locally logged on the Openhift. If you do not have it yet - I recommend downloading OpenShift Client Tools (Windows, Linux) Use following command to start the proxy and forward ports to the remote pod:

oc login #login to Openshift
oc project #switch to your project
oc port-forward <POD-NAME> 8080 3000 3001

Remember to replace with the name of your pod. After that you shuld see output:

oc port-forward my-app-46ztp 8080 3000 3001
Forwarding from 127.0.0.1:8080 -> 8080
Forwarding from 127.0.0.1:3000 -> 3000
Forwarding from 127.0.0.1:3001 -> 3001

JVisualVM connection

If you have started proxy on the machine where you run JVisualVM, you can connect locally to RMI port:

 visualvm --openjmx localhost:3000