An overview,

In the last article, I introduced the use of Docker to deploy Spring Boot and access SkyWalking. In the article, I described the use of Docker-compose to build SkyWalking. This article will not cover the construction of SkyWalking. Here we mainly record the deployment of a Tomcat using Docker and access skyWalking for service link tracking.

Use Docker to deploy Tomcat and access SkyWalking

The following is a description of how to connect a Tomcat application to the SkyWalking website:

For more information, see the official website: SkyWalking Agent official website

  • Linux Tomcat 7 / Tomcat 8

Add the following to the first line of tomcat/bin/catalina.sh:

CATALINA_OPTS="$CATALINA_OPTS -javaagent:<skywalking-agent-path>"; export CATALINA_OPTS
Copy the code
  • Windows Tomcat 7 / Tomcat 8

In the first line of tomcat/bin/catalina.bat, add the following:

set "CATALINA_OPTS=-javaagent:<skywalking-agent-path>"
Copy the code
  • JAR File or Spring Boot

Add the -javaAgent parameter to the startup command line of the application:

java -javaagent:<skywalking-agent-path> -jar yourApp.jar
Copy the code

Note: The -javaAgent parameter must precede the -jar parameter.

Since I am using Docker to deploy Tomcat, I am not going to change the tomcat/bin/catalina.sh file, and check the tomcat/bin/catalina.sh file, which is described as follows:

The script to read setenv.sh is defined in the following figure:

It means: Do not set variables in the catalina.sh script. To keep your custom variables separate, you should put custom environment variables in the CATALINA_BASE/bin/setenv.sh file. In addition, in my tests, I found that you can use environment variables directly. I will describe the configuration in both ways below.

1. Write the user-defined variables in the setenv.sh file

First we need to download the SkyWalking Agent. You can see the article at the beginning of this article about downloading the Agent, and then put the agent in an accessible directory. This is my test, so copy the agent directory to the same directory as my Dockerfile. The contents of Dockerfile are as follows:

FROM tomcat

LABEL maintaner="xiniao"

COPY agent /usr/skywalking/agent

WORKDIR /usr/local/tomcat/bin

RUN echo 'CATALINA_OPTS="$CATALINA_OPTS -javaagent:/usr/skywalking/agent/skywalking-agent.jar";' > setenv.sh

ENV SW_AGENT_NAME="my-spring-demo" \
    SW_AGENT_COLLECTOR_BACKEND_SERVICES="127.0.0.1:11800"

WORKDIR /usr/local/tomcat/webapps/ROOT

COPY target/my-spring-demo.war my-spring-demo.war

RUN jar -xf my-spring-demo.war


Copy the code

The rest of the SkyWalking Agent configuration can be defined in the form of environment variables, such as the name of the service shown in SkyWalkingENV SW_AGENT_NAME="my-spring-demo"And skyWalking’s back-end service addressSW_AGENT_COLLECTOR_BACKEND_SERVICES = "127.0.0.1:11800"When we visit the SkyWalking UI, we can see something like this:

2. Use the environment variable to specify the path of the SkyWalking Agent

The details of the Dockerfile are as follows:

FROM tomcat LABEL maintaner="xiniao" COPY agent /usr/skywalking/agent ENV CATALINA_OPTS="$CATALINA_OPTS -javaagent:/usr/skywalking/agent/skywalking-agent.jar" \ SW_AGENT_NAME="my-spring-demo-env" \ SW_AGENT_COLLECTOR_BACKEND_SERVICES = "127.0.0.1:11800" WORKDIR/usr/local/tomcat/webapps/ROOT COPY target/my-spring-demo.war my-spring-demo.war RUN jar -xf my-spring-demo.warCopy the code

Run the following command to build an image:

docker build -t my-spring-demo .
Copy the code

Run the container with the following command:

docker run --rm -p 8080:8080 my-spring-demo
Copy the code

Access the SkyWalking UI and you can see the following monitored information:

conclusion

This paper mainly introduces the use of docker to deploy Tomact and access SkyWalking. Because there is not much relevant information found on the Internet, it is recorded here that some help is needed for partners in need. There are still some problems, such as skywalking Agent directly into the image file is not friendly, I think we can customize a basic Tomcat image, and the Skywalking Agent into the basic image, All Tomcat builds images with reference to this base image and can access SkyWalking. If you are deploying with K8S, you can define an initial container in POD and copy the SkyWalking agent from the initial container into our application when POD starts. And non-intrusively connect pod applications to SkyWalking, which I’ll cover in the next article.