1

I have two Kubernetes containers running in a pod, call them container A and B. A has a JMX server running. The Java options used to run the server are:

-Dcom.sun.management.jmxremote=true  
-Dcom.sun.management.jmxremote.host=127.0.0.1  
-Dcom.sun.management.jmxremote.port=1234  
-Dcom.sun.management.jmxremote.ssl=false  
-Dcom.sun.management.jmxremote.authenticate=false

-Dcom.sun.management.jmxremote.host=localhost flag is set specifically so that JMX server isn't exposed to external clients.
Container B wants to monitor A using JMX over localhost only. The connection to JMX server in A is established using the following code:

String serverUrl = "service:jmx:rmi:///jndi/rmi://127.0.0.1:1234/jmxrmi";
JMXConnector jmxConnector = JMXConnectorFactory.connect(serviceUrl, null);
MBeanServerConnection mBeanConn = jmxConnector.getMBeanServerConnection();

I was expecting to code to run without any issues since I'm able to connect to JMX port of A from B using telnet. However, when the Java code runs, an exception is thrown, first few lines of which are:

java.rmi.ConnectException: Connection refused to host: 10.8.0.88; nested exception is: 
    java.net.ConnectException: Connection refused (Connection refused)
    at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:623) ~[na:1.8.0_312]
    at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:216) ~[na:1.8.0_312]
    at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:202) ~[na:1.8.0_312]
    at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:132) ~[na:1.8.0_312]

The client is using the private IP to connect to RMI server instead of connecting using 127.0.0.1 and is failing as JMX server is listening only on localhost. I need help figuring out the best way to allow communication between containers A & B over localhost.

Arjan Singh Bal
  • 133
  • 1
  • 8
  • 1
    Dupe https://stackoverflow.com/questions/834581/remote-jmx-connection and https://stackoverflow.com/questions/856881/how-to-activate-jmx-on-my-jvm-for-access-with-jconsole from 2009 and https://stackoverflow.com/questions/27565320/ from 2014 -- for people who actually look rather than stumbling about. – dave_thompson_085 Jan 30 '22 at 00:53
  • See *Update* at the end of [this Answer](https://stackoverflow.com/a/856882/642706). – Basil Bourque Jan 30 '22 at 01:47
  • @BasilBourque it's the same issue to the one you linked. I probably couldn't find the answer as I wasn't searching for the keyword **jconsole**. Thanks for the help! – Arjan Singh Bal Jan 30 '22 at 08:04

1 Answers1

1

Answering my own question for others who stumble upon the same problem. Adding the option -Djava.rmi.server.hostname=127.0.0.1 to the Java options solves the issue.

Arjan Singh Bal
  • 133
  • 1
  • 8