18
头图

I. Overview

Recently, I am studying skywalking and plan to use k8s to deploy skywalking and connect the application in pod to skywalking for service link tracking. This article does not introduce the deployment and use of skywalking in k8s, but first introduces how to use skywalking manually and docker. In the whole practice process, I consulted a large number of documents and encountered various problems. I will record my own practice process here. I hope to provide some help to small partners who have the same needs.

1. Introduction to skywalking

SkyWalking is a popular domestically produced APM (Application Performance Monitoring, application performance monitoring) product, mainly for microservices, Cloud Native and containerized (Docker, Kubernetes, Mesos) applications. The core of SkyWalking is a distributed tracking system, which is currently the top project of the Apache Foundation.

For a detailed introduction of skywalking, please refer to the official document: skywalking official website

2. Skywalking architecture

Logically speaking, SkyWalking is divided into four parts: probe, platform backend, storage and UI, as shown in the following figure:
image.png

  • probe collects data and reformats it to meet SkyWalking requirements (different probes support different sources).
  • platform backend supports data aggregation, analysis and stream processing, covering tracking, metrics and logs.
  • storage device stores SkyWalking data through an open/pluggable interface. You can choose an existing implementation, such as ElasticSearch, H2, MySQL, TiDB, InfluxDB, or implement your own.
  • UI is a highly customizable web-based interface that allows SkyWalking end users to visualize and manage SkyWalking data.

3. How does skywalking automatically report data

Before using the link tracking console to track the link data of the application, the client needs to report the application data to the link tracking. SkyWalking reports the Java application data to the link tracking console, and first needs to complete the burying work. SkyWalking supports both automatic probes (Dubbo, gRPC, JDBC, OkHttp, Spring, Tomcat, Struts, Jedis, etc.) and manual tracing (OpenTracing). This article introduces the automatic burying method.

The principle of skywalking reporting data is shown in the figure below:
image.png

2. Use docker-compose to install skywalking-oap-server and skywalking-ui

Use the following command to install docker-compose in Linux:

yum install -y docker-compose

Use the following command to create the skywalking-docker-compose.yaml file:

vim skywalking-docker-compose.yaml

Start a standlone container here, and use H2 store data by default. If you need other storage, you can check the official document to set it up.
skywalking-docker-compose.yaml content of the 060796732b1686 file is as follows:

version: '3'
services:
  oap:
    image: apache/skywalking-oap-server:8.4.0-es6
    container_name: oap
    restart: always
    ports:
      - 11800:11800 # agent 上报数据的端口,这是 gRPC 端口
      - 12800:12800 # ui 读取数据的端口, 这是 http 端口
  skywaling-ui:
    image: apache/skywalking-ui:8.4.0
    container_name: ui
    depends_on:
      - oap
    links:
      - oap
    ports:
      - 8088:8080
    environment:
      - SW_OAP_ADDRESS=oap:12800

Use the following command to start skywalking:

docker-compose -f skywalking-docker-compose.yaml up -d

Use the following command to view the startup log:

docker-compose -f skywalking-docker-compose.yaml logs -f 

After the startup is successful, visit: http://localhost :8088, you can see the following interface:
image.png

3. Manually connect spring boot to skywalking

1. Download skywalking agent

Download address of skywalking official website: http://skywalking.apache.org/downloads/
image.png
image.png

The one I downloaded here is apache-skywalking-apm-8.4.0.tar.gz , which can also be downloaded and decompressed by the following command in the Linux system:

wget https://archive.apache.org/dist/skywalking/8.4.0/apache-skywalking-apm-8.4.0.tar.gz

tar -zxvf apache-skywalking-apm-8.4.0.tar.gz

The directory structure after decompression is as follows:
image.png
The description of the agent directory is as follows:
image.png

2. The spring boot project automatically reports data

To report the data of the spring boot project, you need to configure the access point and service name of skywalking, which can be configured in the following ways:

Modify the configuration file

Open the previously downloaded agent/config/agent.config file and find the following two configurations:

# The service name in UI
agent.service_name=${SW_AGENT_NAME:Your_ApplicationName}

# Backend service addresses.
collector.backend_service=${SW_AGENT_COLLECTOR_BACKEND_SERVICES:127.0.0.1:11800}

Configure according to the actual situation. After the configuration modification is completed, you can start the spring boot project with the following command:

java -javaagent:<skywalking-agent-path> -jar spring-boot-demo
Please replace <skywalking-agent-path> with the absolute path of skywalking-agent.jar in the Agent folder.
Note that the -javaagent parameter must be before the -jar parameter.

Configure via command parameters

-Dskywalking.agent.service_name parameter in the startup command line of the application.
The skywalking access point can add the -Dskywalking.collector.backend_service parameter to the startup command line of the application.
The complete command is as follows:

java -javaagent:<skywalking-agent-path> -Dskywalking.agent.service_name=<ServiceName> -Dskywalking.collector.backend_service=<backend-service-addresses>  -jar yourApp.jar

Configure in the startup parameters of idea

We can also perform the following configuration VM options of the idea startup configuration option:
image.png

After the configuration is complete, the startup project can see the following information printed to the console:
image.png

Check the skywalking console, and there is no data, because we need to access the interface before reporting the data to the console. By continuously calling the test interface, we can see our calling situation by checking the console again, as shown in the following figure:
image.png

Fourth, use docker to deploy spring boot and access skywalking

Here, the basic image of spring boot is skywalking-base . This image contains openjdk11 and skywalking-agent . For detailed information, please refer to the reference document below.
image.png

The content of the spring boot Dockerfile file is as follows:

FROM apache/skywalking-base:8.4.0-es6
WORKDIR /app
COPY target/spring-demo-0.0.1-SNAPSHOT.jar spring-demo-0.0.1-SNAPSHOT.jar
ENV SW_AGENT_COLLECTOR_BACKEND_SERVICES="127.0.0.1:11800" \
    SW_AGENT_NAME="my-spring-demo-test-adfasdf"


CMD java -javaagent:/skywalking/agent/skywalking-agent.jar \
         -jar spring-demo-0.0.1-SNAPSHOT.jar

Here, environment variables are used to set custom parameters for skywalking, and other configurations of environment variables can be viewed in the agent/config/agent.config file.

Use the following command to build the image:

docker build -t spring-boot-demo .

Use the following command to start the mirror:

docker run --rm -p 8080:8080 spring-boot-demo

After the startup is successful, you can visit the test address and check whether there is data reported in the skywalking console. If there is no data reported, we need to check the skywalking agent log. You can check the error message agent/logs/skywalking-api.log

Reference documents

skywalking official document
Apache SkyWalking Docker Files
Report Java application data through SkyWalking


惜鸟
328 声望2.3k 粉丝