Profiling a application in OpenShift container

Sometimes writing code that just runs is not enough. We might want to know what goes on internally such as how memory is allocated, consequences of using one coding approach over another, implications of concurrent executions, areas to improve performance, etc. We can use profilers for this.

In this post I’ll discuss how to use YourKit-JavaProfiler inside a container.

Since my sample application is built using OpenShift S2I process and pushed into OpenShift internal registry, I’ll have to pull the image locally.

docker login -p $(oc whoami --show-token) -u admin
docker pull

Create a new Dockerfile, add few lines to install YourKit Java Profiler agents and expose the profiler agent port.

RUN wget -P /tmp/ && \
    unzip /tmp/ -d /usr/local && \
    rm /tmp/
EXPOSE 10001

Build and push the image into registry

docker build . -t
docker push

Update image in the deployment configuration.

Load the agent into the JVM by adding a JAVA_TOOL_OPTIONS environment variable in the deployment configuration.

$ oc set env dc/MY_APP_NAME JAVA_TOOL_OPTIONS=-agentpath:/usr/local/YourKit-JavaProfiler-2019.01/bin/linux-x86-64/,listen=all
oc rollout latest dc/MY_APP_NAME

Once deployment is finished, do a oc port forwarding from your local machine to the application pod.

$ oc get pod
NAME                            READY     STATUS      RESTARTS   AGE
MY_APP_NAME-3-1xrsp             1/1       Running     0          6s
$ oc port-forward MY_APP_NAME-3-1xrsp 10001:10001

Add a connection in the profiler with localhost:10001 and you’re all set.

Built with Hugo
Theme Stack designed by Jimmy