How to Expose Jmx in Kubernetes?

Maciej
1 min readNov 24, 2021

Introduction

In this short post I will show you how we can easily expose Jmx in Kubernetes. If we want to temporarily debug our application with JMX the easiest way is forward JMX port from Kubernetes pod to your local PC with kubectl port-forward.

Let’s start

Add JVM options to your application

-Dcom.sun.management.jmxremote
-Dcom.sun.management.jmxremote.authenticate=false
-Dcom.sun.management.jmxremote.ssl=false
-Dcom.sun.management.jmxremote.local.only=false
-Dcom.sun.management.jmxremote.port=5000
-Dcom.sun.management.jmxremote.rmi.port=5000
-Djava.rmi.server.hostname=127.0.0.1

Important part is:

  • We need use same port as jmxremote.port and jmxremote.rmi.port. This is needed to forward one port only.
  • As rmi.server.hostname we should pass 127.0.0.1. This is needed for JMX connection to work via port-forwarding.

Forward the JMX port (5000) to your local PC via kubectl

C:\Tools\cmder
λ kubectl port-forward "your-app-pod" 5000

Open connection with jconsole to your local port 5000

C:\Tools\cmder
λ jconsole 127.0.0.1:5000

In this way makes it possible to debug any Java pod via JMX without having to publicly expose JMX via K8 service which is better from security perspective then expose this JMX via service.

Source: https://giphy.com/gifs/animation-spongebob-rubber-hose-moFzjcoCTbawHPbvLz

--

--

Maciej

DevOps Consultant. I’m strongly focused on automation, security, and reliability.