Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save tuapuikia/bf871f03c4ecee18402aa15bee978fa6 to your computer and use it in GitHub Desktop.
Save tuapuikia/bf871f03c4ecee18402aa15bee978fa6 to your computer and use it in GitHub Desktop.
Profiling JVM on Kubernetes using VisualVM

Profiling JVM on Kubernetes using VisualVM

Enable JMX server

Edit Dockerfile to enable JMX server and change the hostname with the IP where the container will run:

FROM openjdk:8-jre-alpine
ADD ./target/app.jar app.jar
EXPOSE 8080
ENTRYPOINT java -Dcom.sun.management.jmxremote.rmi.port=9090 -Dcom.sun.management.jmxremote=true -Dcom.sun.management.jmxremote.port=9090 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.local.only=false -Djava.rmi.server.hostname=192.168.1.2 -jar app.jar

Add port mapping

Edit the Helm Chart service.yaml file adding the following code:

{{- if .Values.service.apply -}}
apiVersion: v1
kind: Service
...
spec:
  type: {{ .Values.service.type }}
  ports:
    - port: 9090
      targetPort: profiler
      protocol: TCP
      name: profiler
...

Change the service type to LoadBalancer

In the Helm Chart values.yaml file change the service to LoadBalancer:

...
service:
  apply: true
  type: LoadBalancer
  port: 80
...

Add a containerPort in the deployment file

In the Helm Chart deployment.yaml file add a containerPort:

apiVersion: apps/v1beta2
kind: Deployment
metadata:
  ...
spec:
  ...
  template:
    ...
    spec:
      ...
      containers:
        - name: {{ .Chart.Name }}
          ...
          ports:
            - name: profiler
              containerPort: 9090
              protocol: TCP

Use VisualVM to connect and visualize JVM values

Download and install the tool Visual VM.
After open the tool, right click Applications > Remote > Add Remote Host... In the host name put the same IP of Dockerfile, in this case 192.168.1.2 then click Ok.
Now right click in the IP bellow Remote, Add JMX Connection and put the same port of Dockerfile, in this case 9090.
The configuration is done, now double click the JMX connection you've created and the JVM statistics should appear.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment