Install Java Agent in Kubernetes & Docker Environment
Prerequisites
- The installer is familiar with the manual installation of the Java Agent.
- The installer understands the principles of Kubernetes and Docker operation and image creation.
- Before installing the Agent, ensure that the local browser time, server time zone, and time are all consistent. If there are multiple servers, ensure that the local browser, all server time zones, and times are consistent. Otherwise, it may affect data accuracy, such as incorrect topology, etc.
Background
-
The following installation process is based on a project developed with Tomcat service, not a pure Tomcat service.
-
Image dependency used: jdk8 (base image) -build-> jdk8-tomcat8-demo (service image) -build-> tomcat-agent-demo (application image)
-
In the following instructions, the base image environment is CentOS. Other operating systems such as RedHat, Ubuntu, Debian can also refer to these steps.
Configuration Script
In Kubernetes & Docker environments, the service is started by writing a script like run.sh, which is executed after the container starts. After the script is configured, the Agent will be automatically deployed as the container is created and run.
The original startup script run.sh built into the image is as follows:
#!/bin/bash
TOMCAT_SERVER_CONF="/opt/apache-tomcat/conf/server.xml"
sed -i "s#CONNECTOR#$CONNECTOR#" $TOMCAT_SERVER_CONF
sed -i "s#CONTEXT#$CONTEXT#" $TOMCAT_SERVER_CONF
sed -i "s#ACCESS_LOG#$ACCESS_LOG#" $TOMCAT_SERVER_CONF
catalina.sh run
Configuration Example:
Note: Do not copy the following script and YAML configuration content in bulk to the SSH client, otherwise formatting issues may occur. It is recommended to configure each item according to the example.
- Add the following code to the Dockerfile of the service image jdk8-tomcat8-demo.
RUN mkdir -p /script
ADD run.sh /script/run.sh
- Modify the run.sh file.
Replace the Agent version, download URL, installation directory, application name, license_key, collector addresses, etc., according to your actual situation. Note that the Agent installation directory should not be shared with other applications.
#!/bin/bash
# Agent version
AGENT_VERSION=${AGENT_VERSION}
# Agent installation path inside the container, can be mounted outside the container, do not share this directory with others.
TINGYUN_AGENT_PATH=/opt/tingyun_agent
# Application name
APP_NAME=tomcat-agent-demo
# license_key
LICENSE_KEY=oiHCIGbcaZ3Il4zd
# collector.addresses
COLLECTOR_ADDRESSES=tingyun.server.com:8080
# Modify Tomcat server.xml file by setting ENV in YAML
TOMCAT_SERVER_CONF="/opt/apache-tomcat/conf/server.xml"
sed -i "s#CONNECTOR#$CONNECTOR#" $TOMCAT_SERVER_CONF
sed -i "s#CONTEXT#$CONTEXT#" $TOMCAT_SERVER_CONF
sed -i "s#ACCESS_LOG#$ACCESS_LOG#" $TOMCAT_SERVER_CONF
# Download Agent
function DownLoadAgent() {
if [ ! -d $TINGYUN_AGENT_PATH ];then
mkdir -p $TINGYUN_AGENT_PATH
/usr/bin/curl --connect-timeout 10 -m 60 -o $TINGYUN_AGENT_PATH/tingyun-agent-java-${AGENT_VERSION}.zip $AGENT_DOWNLOAD_URL
unzip -d $TINGYUN_AGENT_PATH $TINGYUN_AGENT_PATH/tingyun-agent-java-${AGENT_VERSION}.zip && rm -rf $TINGYUN_AGENT_PATH/tingyun-agent-java-${AGENT_VERSION}.zip
else
echo "Agent directory already exists in the container, trying to start the Agent. If startup fails, check if the Agent directory is complete. You can try deleting the entire Agent directory and reinstalling."
fi
}
# Determine whether to enable the Agent
if [ $TINGYUN_AGENT_ENABLE == "true" ];then
DownLoadAgent
if [ $? -eq 0 ];then
export JAVA_OPTS="$JAVA_OPTS \
-Dtingyun.app_name=${APP_NAME} \
-Dtingyun.license_key=${LICENSE_KEY} \
-Dtingyun.collector.addresses=${COLLECTOR_ADDRESSES} \
-javaagent:${TINGYUN_AGENT_PATH}/tingyun/tingyun-agent-java.jar"
echo "Injected required environment variables for Agent startup into JAVA_OPTS"
echo "JAVA_OPTS updated to: $JAVA_OPTS"
echo "Starting service and Agent"
catalina.sh run
else
echo "Java Agent download and installation failed, service not started. You can: 1. Check errors and restart Agent and service after recovery. 2. Set AGENT_ENABLE in YAML to false to disable the Agent and start the service."
fi
else
echo "Service started (Agent not embedded)"
catalina.sh run
fi
- Modify the following parts in the YAML configuration file of the application image tomcat-agent-demo:
volumes:
# Create configmap directory, put the modified run.sh script in the directory (this script is based on the original script and has been modified for Agent download, installation, and integrated startup)
- name: run-config-volume
configMap:
name: tomcat-agent-demo-run-config
volumeMounts:
# Mount the new script to the directory of the original startup script in the container, replacing the original script
- name: run-config-volume
mountPath: /script
containers:
- name: tomcat-agent-demo
image: registry.tingyun.com/common/tomcat-agent-demo:15
# Service startup
command: ['sh', '/script/run.sh']
env:
# Agent installation and startup switch. true means auto-install and start the Agent, false or other means the service does not embed the Agent and starts normally
- name: TINGYUN_AGENT_ENABLE
value: "true"
- name: AGENT_DOWNLOAD_URL
# Guanyun SaaS Java Agent download URL
value: "http://wukong1.tingyun.com/server-config/config/deploy/download/file?version=3.4.6&type=1&token=30a6c263-656a-437b-8f3e-9e00cad88de4"
# Guanyun Private Java Agent download URL
value: "http://down.tingyun.com/3.0/agent/apm/java/tingyun-agent-java-${AGENT_VERSION}.zip"
- Based on the application image, create and run a container, check the Agent logs and reports, and observe whether performance data is uploaded normally.