How to Expose Jmx in Kubernetes?

Photo by israel palacio on Unsplash

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

 by the author.

--

--

--

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

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Debug CSS Grid with Firefox DevTools

Singular Value Decomposition Example In Python

Oasis Internship Experience

Code Generation from Remote APIs

images/hub.png

Calculating the area under an AnimationCurve in Unity

Infinite Problems

Interpreter and Compiler

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Maciej

Maciej

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

More from Medium

Kubernetes monitor using Prometheus and Thanos , (2)Deployment

Using Kubernetes sidecar containers

Is Kubernetes a DIY platform?

How to install Metrics Server on Kubernetes cluster