How to integrate docker based project with skywalking agent

01skywalking introduction

skywalking is an open source application performance monitoring system, including index monitoring, distributed tracking and distributed system performance diagnosis

02skywalking official Chinese translation document

https://skyapm.github.io/document-cn-translation-of-skywalking/

03 how to quickly build skywalking

https://github.com/apache/skywalking-docker

04 how to integrate skywalking in the project

1. Download skywalking agent

https://archive.apache.org/dist/skywalking/

The extracted directory is as follows

2. Configure skywalking probe for our project

The shape is as follows

java -javaagent:D:apache-skywalking-apm-es7-8.4.0/apache-skywalking-apm-bin-es7/agentskywalking-agent.jar -Dskywalking.agent.service_name=Current project in skywalking Name of display -Dskywalking.collector.backend_service=xxxx:11800 -jar spring-demo-0.0.1-SNAPSHOT.jar

In fact, the official also provides documents to tell us how to configure, as shown in the figure below

For more detailed configuration information, you can view the following links

https://github.com/apache/skywalking/blob/master/docs/en/setup/service-agent/java-agent/README.md

Through the above steps, the project can be integrated with skywalking. However, some small partners report that in the docker environment, they don't know how to use skywalking agent to bury points. Next, let's introduce how to integrate docker based deployment projects with skywalking agent

Thinking point: how can skywalking agent be used in projects in docker?

Everyone may understand the truth, that is, put skywalking agent and project into the same docker container. Based on this theory, the following two schemes are derived

Scheme 1: integrate the entire agent folder of skywalking agent into the project to be buried

The shape is shown in the figure below:

Then modify the dockerfile file of the project. The modified content is as follows

FROM adoptopenjdk/openjdk8
VOLUME /tmp
COPY localtime /etc/localtime
RUN echo "Asia/Shanghai" > /etc/timezone
COPY target/spring-demo-*.jar app.jar
COPY agent /usr/local/agent
ENTRYPOINT [ "sh", "-c", "java -javaagent:/usr/local/agent/skywalking-agent.jar -Dskywalking.agent.service_name=spring-demo -Dskywalking.collector.backend_service=192.168.1.2:11800 -Djava.security.egd=file:/dev/./urandom -jar /app.jar" ]

The main of the core is the following two sentences

COPY agent /usr/local/agent
ENTRYPOINT [ "sh", "-c", "java -javaagent:/usr/local/agent/skywalking-agent.jar -Dskywalking.agent.service_name=spring-demo -Dskywalking.collector.backend_service=192.168.1.2:11800 -Djava.security.egd=file:/dev/./urandom -jar /app.jar" ]

Copy the agent folder in the project to the / usr/local/agent folder in the container, and then the following operations are the same as using skwalking agent in a normal environment

After integration, see the figure below

Scheme 2: when we build the basic image, skywalking agent is also added

For example, when we build the jdk basic image running in java, we add skywalking agent

The dockerfile contents are as follows

FROM adoptopenjdk/openjdk8
VOLUME /tmp
#ENV JAVA_OPTS="-Dcom.sun.management.jmxremote.port=39083 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false"
ENV JAVA_OPTS=""
ENV SKYWALKING_AGENT_SERVICE_NAME=""
ENV SKYWALKING_COLLECTOR_BACKEND_SERVICE=""
COPY localtime /etc/localtime
COPY agent /usr/local/agent
RUN echo "Asia/Shanghai" > /etc/timezone

ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -javaagent:/usr/local/agent/skywalking-agent.jar -Dskywalking.agent.service_name=$SKYWALKING_AGENT_SERVICE_NAME -Dskywalking.collector.backend_service=$SKYWALKING_COLLECTOR_BACKEND_SERVICE -Djava.security.egd=file:/dev/./urandom -jar /app.jar" ]

Then build - t image name through docker Or build the basic image through docker compose build

The basic image built in this example is openjdk8 trace agent.

Here are several parameter descriptions: SKYWALKING_AGENT_SERVICE_NAME and skywarming_ COLLECTOR_ BACKEND_ Service is used as an environment variable and can be found in docker compose Specify specific environment variable values in the YML file or k8s file. In docker compose YML as an example

The configuration is as follows

version: '3.1'
services:
  spring-demo:
    restart: always
    image: 192.168.1.3:5002/demo/spring-demo:dev
    container_name: spring-demo
    network_mode: bridge
    ports:
     - "8085:8080"
    environment:
     - SKYWALKING_AGENT_SERVICE_NAME=spring-demo-test
     - SKYWALKING_COLLECTOR_BACKEND_SERVICE=192.168.1.2:11800

secondly

ONBUILD COPY app.jar app.jar

When we build maven, we uniformly name the business jar as app Jar, so the first app Jar is the jar of our business project, and the second jar is the jar running in the docker container. In this way, we only need to write this in the dockerfile of the business

FROM 192.168.1.3:5002/dev/openjdk8-trace-agent

The example after integration is shown in the figure below

05

summary

Distributed link tracking is basically a required option in microservices. At present, in addition to skywalking, there are also pinpoint, jaeger, zipkin, cat, etc. for open source link tracking on the market. If you are interested, you can learn about it. In addition, this article provides that using skywalking agent in docker container may not be the best solution, so let's take it as a reference

Added by hardius on Fri, 07 Jan 2022 04:04:37 +0200