Skip to content

Instantly share code, notes, and snippets.

@besteban1989
Last active January 11, 2024 04:29
Show Gist options
  • Save besteban1989/a52c9d1994576bd75344ae0a261921c8 to your computer and use it in GitHub Desktop.
Save besteban1989/a52c9d1994576bd75344ae0a261921c8 to your computer and use it in GitHub Desktop.
Java 17 K8s
# Check java settings within a pod
kubectl exec -it <pod> bash
java -XshowSettings:system -version
# Useful docs at https://developers.redhat.com/articles/2022/04/19/java-17-whats-new-openjdks-container-awareness#tuning_defaults_for_containers
# Setup options via JAVA_TOOL_OPTIONS, https://circleci.com/docs/java-oom/#javatooloptions
# Specify -XX:+UseZGC garbage collector for service with high memory requirements. https://www.linkedin.com/pulse/jdk-17-g1gc-vs-zgc-usage-core-exchange-application-performance-raza
- name: JAVA_TOOL_OPTIONS
value: -Dlogging.level.root=INFO -XX:+UseZGC -XX:InitialRAMPercentage=40.0 -XX:MaxRAMPercentage=60.0 -XX:ActiveProcessorCount=4
++
resources:
limits:
memory: "8Gi"
requests:
memory: "2Gi"
# Docker images
eclipse-temurin:17-jre-focal
eclipse-temurin:17-jre-alpine
maven:3.8.6-eclipse-temurin-17-focal
# Enable VisualVM
1. Add environment variable to the k8s deployment. (append the parameters in case the environment variable already exists)
- name: JAVA_TOOL_OPTIONS
value: "-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=9010 \
-Dcom.sun.management.jmxremote.rmi.port=9010 \
-Djava.rmi.server.hostname=127.0.0.1"
2. Add the port in the k8s deployment:
ports:
- containerPort: 9010
protocol: TCP
3. Do port-forward to the pod
kubectl port-forward <pod-name> 9010:9010
4. Open VisualVM -> Add JMX Connection
Specify 127.0.0.1:9010 and save it
5. Double click on app.jar
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment