Profile Java workloads on Kubernetes
Turn on Java profiling with the CloudWatch Observability EKS add-on or the
amazon-cloudwatch-observability Helm chart, and send continuous profiles from
your Java workloads on Kubernetes to Amazon CloudWatch Profiles.
The add-on and the Helm chart use the same profiling setting. Only how you install the setting differs. Choose the variant that matches your cluster:
-
Amazon EKS add-on – For Amazon EKS clusters that use the CloudWatch Observability EKS add-on. You set the profiling value in the advanced configuration of the add-on.
-
Helm chart – For other Kubernetes clusters, and for Amazon EKS clusters that use the Helm chart instead of the add-on. You set the profiling value in the Helm chart.
Where a step differs between the two variants, this tutorial labels each variant.
When you finish, your Java pods collect their own profiles. The AWS Distro for OpenTelemetry (ADOT)
Java agent in each pod sends profiles in OpenTelemetry Protocol (OTLP) format to the
CloudWatch agent on its node. The CloudWatch agent signs each request to the CloudWatch
profiles endpoint with AWS Signature Version 4 (SigV4), using the credentials of its IAM
role, such as the node role or the instance role of your nodes.
CloudWatch stores the profiles under the service.name of each workload, with
cluster, namespace, and pod labels. You don't rebuild images or change entry points.
Time to complete: About 15 minutes, plus the time to restart your workloads
Cost to complete: There is no charge for Amazon CloudWatch Profiles during the preview. Standard Amazon EC2 charges apply for the cluster that you run, and Amazon EKS charges apply if the cluster runs on Amazon EKS.
Prerequisites
Before you begin, make sure that you have the following:
-
Java workloads that run on Linux nodes in a Kubernetes cluster: an Amazon EKS cluster if you use the add-on, or any Kubernetes cluster if you use the Helm chart.
-
The ADOT Java agent version 2.32.0 or later.
-
The CloudWatch agent version 1.300075.0 or later.
-
The
CloudWatchAgentServerPolicymanaged policy attached to the IAM role that the CloudWatch agent uses, such as the node role or the instance role of your nodes. This policy includes thecloudwatch:PutProfileRecordspermission that the signing identity needs. For more information, see IAM permissions required. -
Credentials with the
cloudwatch:GetProfileRecordspermission to view the profile data. For more information, see IAM permissions required.
Amazon EKS add-on variant – If you use the CloudWatch Observability EKS add-on, you also need the following:
-
An Amazon EKS cluster with Linux nodes.
-
The CloudWatch Observability EKS add-on (
amazon-cloudwatch-observability) versionv8.0.1-eksbuild.1or later, installed on the cluster. Earlier versions reject the profiling setting. To see the installed version, runaws eks describe-addon --cluster-name. For instructions, see Install the CloudWatch agent with the Amazon CloudWatch Observability EKS add-on or the Helm chart. For more information about the add-on, see OTel Container Insights (Recommended).cluster-name--addon-name amazon-cloudwatch-observability --query addon.addonVersion -
The add-on version
v8.0.1-eksbuild.1installs the CloudWatch agent version 1.300075.0 or later by default.
Helm chart variant – If you use the Helm chart, you also need the following:
-
A Kubernetes cluster with Linux nodes, and the
amazon-cloudwatch-observabilityHelm chart version8.0.0or later installed. For instructions, see Enable your applications on Kubernetes. -
The OTLP endpoint of the Helm chart turned on. This endpoint is on by default. Profiling requires
otlp.enabled,otlp.http.enabled, andagent.enabledset totrue, and an entry inagentsfor the node agent.
Step 1: Turn on Java profiling
Set manager.autoInstrumentationConfiguration.java.profiling.enabled to the
string "true". Follow the variant that matches your cluster: Amazon EKS add-on variant: Turn on Java profiling with the CloudWatch Observability EKS add-on or Helm chart variant: Turn on Java profiling with the Helm chart.
Profiles go to the OTLP endpoint that the CloudWatch agent serves on every node, on
HTTP port 4318 by default. The add-on and the Helm chart turn on this endpoint by default,
whether or not you turn on profiling. Profiling doesn't change the receivers of the agent.
Before you continue, read Restrict access to the OTLP ports.
By default, the add-on and the chart turn on wall-clock and memory profiling. The profiler collects
wall-clock profiles, which sample threads both on and off the CPU, and memory allocation
profiles. The ADOT Java agent leaves memory profiling off by default, so the operator sets
OTEL_AWS_PROFILER_MEMORY_ENABLED to true in each profiled
container. A value that the container sets takes precedence. To turn off memory profiling
for a container, set OTEL_AWS_PROFILER_MEMORY_ENABLED to false on
the container. To change other settings, see Opt out, turn off, and tune profiling.
Amazon EKS add-on variant: Turn on Java profiling with the CloudWatch Observability EKS add-on
The value must be the string "true", in quotation marks, not a
boolean.
The configuration values that you pass to the add-on replace its earlier configuration values. If the add-on already has configuration values, merge them with the profiling setting.
Helm chart variant: Turn on Java profiling with the Helm chart
If you pass a boolean, the install fails with an error that tells you to use
--set-string.
The only setting under java.profiling is enabled. Any
other key also makes the install fail.
To turn on Java profiling with the Helm chart
-
Run the following command. Replace
cluster-namewith the name of your cluster, and the example Region (us-east-1) with your own Region. If you installed the chart under a different release name, use that name. If you installed the chart with other values, include them in this command too.helm upgrade --install amazon-cloudwatch-observability \ aws-observability/amazon-cloudwatch-observability \ --namespace amazon-cloudwatch \ --set clusterName=cluster-name\ --set region=us-east-1\ --set-string manager.autoInstrumentationConfiguration.java.profiling.enabled=trueThe
--set-stringoption passes the value as a string.
Step 2: Verify that profiles arrive in CloudWatch
Before you verify, restart your Java workloads so that new pods pick up the setting. Profiling applies only to new pods.
To restart a Java workload
-
Restart the workload. Replace
deployment-nameandnamespacewith the values for your workload.kubectl rollout restart deployment/deployment-name-nnamespace -
Wait for the rollout to finish.
kubectl rollout status deployment/deployment-name-nnamespace
First confirm that the new pods have profiling turned on. Then confirm that CloudWatch receives the profile data. The first profiles arrive a few minutes after the pods start.
To verify that profiles arrive in CloudWatch
-
Check that a new pod has the
OTEL_AWS_PROFILER_ENABLEDenvironment variable. Replacepod-nameandnamespacewith the values for one of the restarted pods.kubectl get podpod-name-nnamespace-o yaml | grep -A 1 OTEL_AWS_PROFILERThe output shows the following environment variables:
-
OTEL_AWS_PROFILER_ENABLED, with the valuetrue -
OTEL_AWS_PROFILER_MEMORY_ENABLED, with the valuetrueunless the container sets it -
OTEL_AWS_PROFILER_ENDPOINT, which points to the CloudWatch agent -
OTEL_AWS_PROFILER_DATA_DIR -
OTEL_AWS_PROFILER_MAX_PAYLOAD_BYTES
If the container sets its own
OTEL_AWS_PROFILER_ENDPOINT, the output doesn't showOTEL_AWS_PROFILER_MAX_PAYLOAD_BYTES. You can also runkubectl describe podand check the environment and volumes of the application container. -
-
Find your workload and its flame graph in CloudWatch. For instructions, see Verify that profile data is arriving. Look for the
service.nameof your workload, and the following profile types:-
wall:wall:nanoseconds:wall:nanoseconds -
memory:alloc_objects:count:alloc_objects:count -
memory:alloc_space:bytes:alloc_space:bytes
The wall-clock flame graph includes threads that wait or sleep, not only threads that run on the CPU.
-
Opt out, turn off, and tune profiling
After you turn on profiling, you can exclude individual containers, turn profiling off for the cluster, or change how the ADOT Java agent profiles.
Opt out a container – To exclude a container,
set OTEL_AWS_PROFILER_ENABLED to a value other than "true", such
as "false". Set it in the env of the container, or in a ConfigMap
that the container loads with envFrom. The operator then doesn't add the
profiling variables or the volume mount to that container. A value from a Secret doesn't
opt the container out.
The following command sets the variable on a container of a deployment. Changing the pod template starts a rollout, so the new pods start without profiling.
kubectl set env deployment/deployment-name-nnamespace\ -ccontainer-nameOTEL_AWS_PROFILER_ENABLED=false
Turn off profiling – To turn off profiling for
the cluster, set manager.autoInstrumentationConfiguration.java.profiling.enabled
to the string "false" in the same way as in Step 1, in the configuration values
of the add-on or with the Helm command. Then restart your workloads.
Tune profiling – To change how the ADOT Java
agent profiles, such as the profiling mode, memory profiling, or the profile length, set
OTEL_AWS_PROFILER_* environment variables on the application container. The
add-on and the chart don't have settings for these options. For the variables and their defaults, see the ADOT Java agent in
Profile your application.
The following example sets the profiling mode in a container spec.
env: - name: OTEL_AWS_PROFILER_MODE value: "cpu"
Longer windows and memory profiling use more disk space. If a container needs more
than 160 MiB, mount your own volume at a path other than
/otel-profiler-data-java. Then set OTEL_AWS_PROFILER_DATA_DIR to
a directory on that volume.
Clean up
To stop profiling, turn it off as described in Opt out, turn off, and tune profiling. Then restart your
Java workloads. Replace deployment-name and
namespace with the values for your workload.
kubectl rollout restart deployment/deployment-name-nnamespace
If you created a cluster only for this tutorial, delete the cluster to stop its charges. Standard Amazon EC2 charges apply for the cluster, and Amazon EKS charges apply if the cluster runs on Amazon EKS.
Considerations
Keep the following in mind when you turn on Java profiling:
-
Profiling is experimental. Profile attributes and service names might change.
-
Profiling supports HotSpot Java virtual machines (JVMs) on Linux x64 and arm64.
-
The operator adds a disk-backed
emptyDirvolume namedopentelemetry-profiler-data-javato each profiled pod. It mounts the volume at/otel-profiler-data-javain each profiled container, and setsOTEL_AWS_PROFILER_DATA_DIRto/otel-profiler-data-java/. Because of this volume, profiling works in containers that usecontainer-namereadOnlyRootFilesystem. -
The size limit of the volume is 160 MiB for each profiled container. If the profiler data exceeds the limit, Kubernetes evicts the pod.
-
Only new pods are profiled. Restart a workload to start or stop profiling.
-
If a container already sets a profiling variable in
env, withvalueFrom, or in a ConfigMap that it loads withenvFrom, the operator keeps the value of the container. The injected value overrides a value that comes from a Secret inenvFrom. -
The operator doesn't profile the following containers. The pod still starts, and the operator log shows
Skipping Java profiling injectionwith the reason.-
Containers in Windows pods.
-
Containers in pods on AWS Fargate. These pods don't have a CloudWatch agent on their node.
-
Containers that might send OTLP data over gRPC and don't set their own
OTEL_AWS_PROFILER_ENDPOINT. This applies whenOTEL_EXPORTER_OTLP_PROTOCOLis set togrpcin any letter case inenvorenvFrom, or comes fromvalueFrom. It also applies whenJAVA_TOOL_OPTIONScontains-Dotel.exporter.otlp.protocol=grpc. -
Containers in pods that already have a volume named
opentelemetry-profiler-data-java, and containers that already mount a volume with that name or at/otel-profiler-data-java.
-
-
If you turn on
autoMonitor.monitorAllServices, the operator profiles every Java workload behind a Service. -
Profiles travel unencrypted inside the cluster, from the pod to the CloudWatch agent on its node.
-
Many busy profiled JVMs on one node increase the memory use of the CloudWatch agent. If needed, raise the memory limit in
agent.resources.
Restrict access to the OTLP ports
Profiling uses the OTLP endpoint of the CloudWatch agent on each node. The add-on and the
Helm chart turn on this endpoint by default, whether or not you turn on profiling. The endpoint listens on ports 4317
(gRPC) and 4318 (HTTP) without Transport Layer Security (TLS) or
authentication. The agent uses the host network, so it listens on every node IP address.
It accepts OTLP metrics, logs, traces, and profiles.
Kubernetes network policies usually don't apply to pods that use the host network, such as the CloudWatch agent. Restrict access to these ports with node security groups or a host firewall. Never expose the ports outside your cluster's network.
With the Helm chart, if you don't need the endpoint, set otlp.enabled to
false. This also turns off Java profiling, which requires the endpoint. If
manager.autoInstrumentationConfiguration.java.profiling.enabled is
"true", set it to "false" too, or the install fails.
Troubleshoot profiling on Kubernetes
The following issues can occur when you turn on Java profiling with the CloudWatch Observability EKS add-on or the Helm chart.
-
The add-on update fails (Amazon EKS add-on variant)
Check that the configuration values are valid JSON, that
enabledis the string"true", and thatjava.profilingcontains no other keys. If the update fails because of a conflict, see the--resolve-conflictsoption in Step 1. -
The Helm install fails (Helm chart variant)
The chart checks the profiling setting and fails the install with an error. Check the error message:
-
The message tells you to use
--set-string. You passed a boolean instead of a string. Pass the value with--set-string. -
The message says that a key
is not a setting.java.profilingaccepts onlyenabled. Remove the other key. To tune the profiler, setOTEL_AWS_PROFILER_*variables in theenvof the container. -
The message says that profiling
requires the node agent's OTLP HTTP endpoint. Setotlp.enabled,otlp.http.enabled, andagent.enabledtotrue. Keep the entry inagentswhose name matchesagent.name.
-
-
No profile data in CloudWatch
Check the following causes:
-
The pod started before you turned on profiling. Restart the workload, and check the pod for
OTEL_AWS_PROFILER_ENABLED. -
The operator skipped the container. Check the operator log for
Skipping Java profiling injectionand the reason. For the conditions, see Considerations. -
The container sets
OTEL_AWS_PROFILER_ENABLEDto a value other thantrue. Remove the variable from the container, or set it totrue. -
The container sets its own
OTEL_AWS_PROFILER_DATA_DIRto a directory that isn't writable. Point it to a writable volume, or remove it. -
The CloudWatch agent version is earlier than 1.300075.0, so the agent doesn't accept profiles. Your application keeps running. Update the add-on, or upgrade the agent.
-
The node role doesn't have the
cloudwatch:PutProfileRecordspermission. Attach theCloudWatchAgentServerPolicymanaged policy to the role that the CloudWatch agent uses.
-
-
CloudWatch agent pods are Pending or don't start
Check the following causes:
-
Another process on the node uses the OTLP host ports, so the agent pod stays Pending. Change the ports with
otlp.grpc.portandotlp.http.port, and then restart your Java workloads. -
A custom agent configuration opens its own OTLP receiver on
otlp.grpc.portorotlp.http.port. The agent can't bind the port, and it doesn't start. The chart doesn't check for this conflict. Move the custom receiver to other ports, or remove it.
-
-
Kubernetes evicts profiled pods
The profiler data exceeds the size limit of the
opentelemetry-profiler-data-javavolume, which is 160 MiB for each profiled container. Longer profile windows and memory profiling use more space. Mount your own volume at a path other than/otel-profiler-data-java, and setOTEL_AWS_PROFILER_DATA_DIRto a directory on it.
Next steps
Now that profile data arrives in CloudWatch, you can do the following:
-
Find the code that uses the most resources. For more information, see Retrieve a flame graph.
-
Compare profiles before and after a deployment. For more information, see Compare two profiles.
-
Review how Java profiling works with the add-on and the Helm chart, and its configuration options. For more information, see Use the EKS add-on or Helm chart.
Related topics
For more information, see the following topics: