如何在 Linux 上安装 Apache Skywalking APM 工具 |
监控是任何系统管理员的重要任务之一。随着技术的不断发展,许多组织依赖应用程序来运营其业务。因此,必须经常检查服务器和应用程序的运行状况。通过这样做,您可以确保应用程序和系统按预期工作。有多种工具可用于监控环境。最流行的解决方案是 Prometheus、Dynatrace、Elastic Stack、Zenoss、New Relic、N-able RMM、Zabbix、Datadog 基础设施监控、Nagios、PRTG Network Monitor 等
SkyWalking是一个开源的APM(应用程序性能监控)系统,为微服务、云原生和基于容器的环境提供监控、跟踪和诊断功能。
Apache SkyWalking 提供了更多其他特性和功能,包括:
- 根本原因分析:使用运行时上的配置文件代码,由进程内代理和 ebpf 分析器提供支持。它还具有用于 TCP、TCP/TLS 和 HTTP(s) 流量的网络分析器。
服务、服务实例、端点 (URI) 指标分析:
- 指标收集、分析和定制。
- 分布式跟踪和上下文传播
- 跨指标、跟踪和日志的协作
SkyWalking 可用于跨多种来源和格式收集指标、跟踪、日志等。支持的包括:
- Go、C++ 和 Rust SDK。
- Java、NodeJS、.NET Core、PHP 和 Python 自动检测代理。
- Java 和 Python 的代理分析。
- 浏览器代理。
- LUA 代理特别适用于 Nginx、OpenResty 和 Apache APISIX。
- ebpf 网络分析和 ON/OFF CPU 分析。
- 指标系统包括Prometheus、OpenTelemetry、Micrometer(Spring Sleuth)和Zabbix。
- 服务网格可观察性。控制平面和数据平面。
- 日志,包括 OpenTelemetry。
下面是 Apache Skywalking 架构的图示。
在本指南中,我们将系统地介绍如何在 Linux 上安装和使用 Apache Skywalking 应用程序性能监控
第 1 步 – 设置先决条件
本指南最适合以下情况:
- 安装了 JDK 1.8.0 或更高版本的 Linux 服务器
- 不过,创建了 Elasticsearch 集群,可以使用任何其他存储,例如 H2、OpenSearch、MySQL、TiDB、InfluxDB、PostgreSQL
要设置 Elasticsearch,您可以使用以下任何指南:
- 在 RHEL 8|CentOS 8 上安装 Elastic Stack 8 (ELK 8)
- 如何在 CentOS/Fedora 上安装 ELK Stack
- 使用 Docker Compose 在 Docker 容器上运行 Elastic stack (ELK)
Elasticsearch 弹性搜索启动后,验证它是否正常工作:
$ curl -X GET localhost:9200
{
"name" : "localhost.localdomain",
"cluster_name" : "my-application",
"cluster_uuid" : "cIYbRiEwTMyVOpYuyn4dnw",
"version" : {
"number" : "8.4.3",
"build_flavor" : "default",
"build_type" : "rpm",
"build_hash" : "42f05b9372a9a4a470db3b52817899b99a76ee73",
"build_date" : "2022-10-04T07:17:24.662462378Z",
"build_snapshot" : false,
"lucene_version" : "9.3.0",
"minimum_wire_compatibility_version" : "7.17.0",
"minimum_index_compatibility_version" : "7.0.0"
},
"tagline" : "You Know, for Search"
}
还要确保 JDK 已安装:
##On Debian/Ubuntu
sudo apt install default-jdk -y
##On RHEL/Rocky_Linux/Alma/CentOS
sudo dnf install java-11-openjdk-devel -y
验证安装:
$ java --version
openjdk 11.0.16 2022-07-19
OpenJDK Runtime Environment (build 11.0.16+8-post-Ubuntu-0ubuntu120.04)
OpenJDK 64-Bit Server VM (build 11.0.16+8-post-Ubuntu-0ubuntu120.04, mixed mode, sharing)
步骤 2 – 安装和配置 Apache Skywalking APM
您可以从官方 Apache Skywalking 下载页面下载 Apache Skywalking。
您还可以下载最新版本,如图所示。首先导出最新版本:
VER=$(curl -s https://api.github.com/repos/apache/skywalking/releases/latest|grep tag_name|cut -d '"' -f 4|sed 's/v//')
然后使用以下命令下载版本:
wget https://archive.apache.org/dist/skywalking/$VER/apache-skywalking-apm-$VER.tar.gz
下载后,解压文件:
tar -xvf apache-skywalking-apm-*.tar.gz
将文件移动到/opt目录下;
sudo mv apache-skywalking-apm-bin /opt/apache-skywalking-apm-bin
设置正确的权限:
sudo chmod -R 775 /opt/apache-skywalking-apm-bin
导航到目录;
cd /opt/apache-skywalking-apm-bin
进入目录后,您需要进行一些配置。
sudo vim config/application.yml
在文件中,找到存储并将其从 H2 设置为 elasticsearch,如下所示:
storage:
selector: ${SW_STORAGE:elasticsearch}
elasticsearch:
namespace: ${SW_NAMESPACE:"my-application"}
clusterNodes: ${SW_STORAGE_ES_CLUSTER_NODES:localhost:9200}
protocol: ${SW_STORAGE_ES_HTTP_PROTOCOL:"http"}
connectTimeout: ${SW_STORAGE_ES_CONNECT_TIMEOUT:3000}
socketTimeout: ${SW_STORAGE_ES_SOCKET_TIMEOUT:30000}
responseTimeout: ${SW_STORAGE_ES_RESPONSE_TIMEOUT:15000}
numHttpClientThread: ${SW_STORAGE_ES_NUM_HTTP_CLIENT_THREAD:0}
user: ${SW_ES_USER:"elastic"}
password: ${SW_ES_PASSWORD:"changeme"}
# trustStorePath: ${SW_STORAGE_ES_SSL_JKS_PATH:""}
# trustStorePass: ${SW_STORAGE_ES_SSL_JKS_PASS:""}
# secretsManagementFile: ${SW_ES_SECRETS_MANAGEMENT_FILE:""} # Secrets management file in the properties format includes the username, password, which are managed by 3rd party tool.
....
在上面的文件中,由于H2不支持持久数据存储,我们将存储形式H2更改为elasticsearch。上述参数说明如下:
- 选择器:存储选择器。对于本示例,将值设置为elasticsearch
- nameSpace:表示Elasticsearch集群中所有索引名称的前缀。
- clusterNodes:Elasticsearch 集群的端点
- user:用于访问Elasticsearch集群的用户名。默认用户名是elastic。
- 密码:用于访问Elasticsearch集群的密码。通常在创建Elasticsearch集群时指定。默认设置是changeme
在配置中,您可以选择更改 SkyWalking 用于与 Elasticsearch 通信的默认端口(用于 RESTful API 操作的 12800 和用于 gRPC API 操作的端口 11800)
core:
selector: ${SW_CORE:default}
default:
# Mixed: Receive agent data, Level 1 aggregate, Level 2 aggregate
# Receiver: Receive agent data, Level 1 aggregate
# Aggregator: Level 2 aggregate
role: ${SW_CORE_ROLE:Mixed} # Mixed/Receiver/Aggregator
restHost: ${SW_CORE_REST_HOST:0.0.0.0}
restPort: ${SW_CORE_REST_PORT:12800}
restContextPath: ${SW_CORE_REST_CONTEXT_PATH:/}
gRPCHost: ${SW_CORE_GRPC_HOST:0.0.0.0}
gRPCPort: ${SW_CORE_GRPC_PORT:11800}
您还可以在 webapp 文件夹中进行配置。
sudo vim webapp/webapp.yml
对于本指南,我们将使用默认值,但您仍然可以根据您的业务要求修改这些值。
server:
port: 8080
spring:
cloud:
gateway:
routes:
- id: oap-route
uri: lb://oap-service
predicates:
- Path=/graphql/**
discovery:
client:
simple:
instances:
oap-service:
- uri: http://127.0.0.1:12800
# - uri: http://<oap-host-1>:<oap-port1>
# - uri: http://<oap-host-2>:<oap-port2>
mvc:
throw-exception-if-no-handler-found: true
web:
resources:
add-mappings: true
management:
server:
base-path: /manage
步骤 3 – 为 Apache Skywalking 创建 systemd 单元
我们需要创建一个 systemd 服务,用于停止和启动应用程序。创建文件如图所示:
sudo vim /etc/systemd/system/skywalking.service
在该文件中,添加以下行:
[Unit]
Description=Apache Skywalking
After=network.target
[Service]
Type=oneshot
User=root
WorkingDirectory=/opt/apache-skywalking-apm-bin/
ExecStart=/bin/bash /opt/apache-skywalking-apm-bin/bin/startup.sh
RemainAfterExit=yes
RestartSec=5
[Install]
WantedBy=multi-user.target
保存文件并重新加载系统守护程序。
sudo systemctl daemon-reload
对于基于 Rhel 的系统,如图所示配置 SELinux
sudo /sbin/restorecon -v /opt/apache-skywalking-apm-bin/bin/startup.sh
启动并启用服务:
sudo systemctl enable --now skywalking
验证服务是否正在运行:
$ systemctl status skywalking
● skywalking.service - Apache Skywalking
Loaded: loaded (/etc/systemd/system/skywalking.service; enabled; vendor preset: disabled)
Active: active (running) since Sat 2022-10-15 05:54:11 EDT; 870ms ago
Main PID: 351646 (bash)
Tasks: 3 (limit: 23198)
Memory: 1.1M
CGroup: /system.slice/skywalking.service
├─351646 /bin/bash /opt/apache-skywalking-apm-bin/bin/startup.sh
├─351648 sh /opt/apache-skywalking-apm-bin/bin/oapService.sh
└─351652 sleep 1
允许服务通过防火墙;
##For UFW
sudo ufw allow 8080
##For Firewalld
sudo firewall-cmd --add-port=8080/tcp --permanent
sudo firewall-cmd --reload
第 4 步 – 访问 Apache Skywalking WebUI
此时,您可以使用 URL http://IP_Address:8080 访问 Apache Skywalking WebUI
此时,我们无法查看任何指标,因为我们尚未执行任何配置。在本指南中,我将演示如何从 Linux 主机/代理收集和导出指标。
第 5 步 – 安装 Prometheus Node Exporter
现在我们需要设置一个代理才能查看指标。这里我们将使用 Prometheus 节点导出器从代理获取日志。
首先,下载最新的存档:
##For AMD64
VERSION=$(curl -s https://api.github.com/repos/prometheus/node_exporter/releases/latest|grep tag_name|cut -d '"' -f 4|sed 's/v//')
wget https://github.com/prometheus/node_exporter/releases/download/v$VERSION/node_exporter-$VERSION.linux-amd64.tar.gz
##For ARM64
VERSION=$(curl -s https://api.github.com/repos/prometheus/node_exporter/releases/latest|grep tag_name|cut -d '"' -f 4|sed 's/v//')
wget https://github.com/prometheus/node_exporter/releases/download/v$VERSION/node_exporter-$VERSION.linux-arm64.tar.gz
##For i386
VERSION=$(curl -s https://api.github.com/repos/prometheus/node_exporter/releases/latest|grep tag_name|cut -d '"' -f 4|sed 's/v//')
wget https://github.com/prometheus/node_exporter/releases/download/v$VERSION/node_exporter-$VERSION.linux-386.tar.gz
现在提取存档:
tar -xf node_exporter-$VERSION.linux-*.tar.gz
将文件移至/opt目录:
sudo mv node_exporter-*/node_exporter /usr/local/bin
为该服务创建专用用户:
sudo adduser -M -r -s /sbin/nologin prometheus
创建systemd服务文件:
$ sudo vim /etc/systemd/system/node_exporter.service
[Unit]
Description=Node Exporter
After=network.target
[Service]
User=prometheus
Group=prometheus
Type=simple
ExecStart=/usr/local/bin/node_exporter
ExecReload=/bin/kill -HUP $MAINPID
Restart=on-failure
RestartSec=5s
[Install]
WantedBy=multi-user.target
保存文件并重新加载守护程序。
sudo systemctl daemon-reload
在基于 Rhel 的系统上,配置 SELinux:
sudo /sbin/restorecon -v /usr/local/bin/node_exporter
启动并启用服务:
sudo systemctl enable --now node_exporter
验证服务是否正在运行:
$ systemctl status node_exporter
node_exporter.service - Node Exporter
Loaded: loaded (/etc/systemd/system/node_exporter.service; enabled; vendor preset: disabled)
Active: active (running) since Sat 2022-10-15 06:52:23 EDT; 8s ago
Main PID: 353801 (node_exporter)
Tasks: 3 (limit: 23198)
Memory: 4.8M
CGroup: /system.slice/node_exporter.service
└─353801 /usr/local/bin/node_exporter
检查节点导出器端口:
$ sudo ss -aplnt | grep node
LISTEN 0 128 *:9100 *:* users:(("node_exporter",pid=353801,fd=3))
允许该端口通过防火墙;
##For UFW
sudo ufw allow 9100
##For Firewalld
sudo firewall-cmd --add-port=9100/tcp --permanent
sudo firewall-cmd --reload
步骤 6 – 安装并配置 OpenTelemetry Collector
OpenTelemetry Collector 于2019 年 5 月融合 OpenCensus 和 OpenTracing 后形成。其主要目标是提供一组标准化的与供应商无关的 SDK、API 和工具,用于摄取、转换数据并将数据发送到 Observability 后端。这不是像 Prometheus 或 Jager 那样的可观察性后端,它支持将数据导出到各种开源和商业后端。
该工具可以安装在您的 Linux 系统上,如图所示。
##On Debian/Ubuntu
sudo apt update && sudo apt install wget curl -y
VERSION=$(curl -s https://api.github.com/repos/open-telemetry/opentelemetry-collector-releases/releases/latest|grep tag_name|cut -d '"' -f 4|sed 's/v//')
wget https://github.com/open-telemetry/opentelemetry-collector-releases/releases/download/v${VERSION}/otelcol_${VERSION}_linux_amd64.deb
sudo dpkg -i otelcol_${VERSION}_linux_amd64.deb
##On Rhel/Rocky/Alma/CentOS
sudo yum -y install wget curl
VERSION=$(curl -s https://api.github.com/repos/open-telemetry/opentelemetry-collector-releases/releases/latest|grep tag_name|cut -d '"' -f 4|sed 's/v//')
wget https://github.com/open-telemetry/opentelemetry-collector-releases/releases/download/v${VERSION}/otelcol_${VERSION}_linux_amd64.rpm
sudo rpm -Uvh otelcol_${VERSION}_linux_amd64.rpm
安装后,您需要将其配置为将指标从 Prometheus Node 导出器导出到 Apache Skywalking 后端。
将可用配置移至备份文件
sudo mv /etc/otelcol/config.yaml /etc/otelcol/config.yaml.bak
现在创建一个新配置:
sudo vim /etc/otelcol/config.yaml
添加以下行并根据需要替换它们。
receivers:
prometheus:
config:
scrape_configs:
- job_name: "vm-monitoring" # make sure to use this in the vm.yaml to filter only VM metrics
scrape_interval: 10s
static_configs:
- targets: ["192.168.205.11:9100"]
processors:
batch:
exporters:
####################################################################################
# If you want to use otlp exporter please ensure that your OAP version is >= 9.2.0 #
# or you can use oc exporter, The config format should be: #
# opencensus: #
# endpoint: "127.0.0.1:11800" #
# tls: #
# insecure: true #
####################################################################################
otlp:
endpoint: "127.0.0.1:11800" # The OAP Server address
###############################################################################
# OTEL previous version before 0.34.0 the config format, eg. 0.29.0 should be:#
# insecure: true #
# The new version should be: #
# tls: #
# insecure: true #
###############################################################################
tls:
insecure: true
#insecure: true
# Exports data to the console
logging:
logLevel: debug
service:
pipelines:
metrics:
receivers: [prometheus]
processors: [batch]
#########################################
# oc exporter config format: #
# exporters: [opencensus,logging] #
#########################################
exporters: [otlp, logging]
请记住将 192.168.205.11 替换为运行节点导出器的 IP 地址,并且
保存文件并重新启动服务:
sudo systemctl restart otelcol
验证服务是否正在运行:
$ systemctl status otelcol
● otelcol.service - OpenTelemetry Collector
Loaded: loaded (/usr/lib/systemd/system/otelcol.service; enabled; vendor preset: disabled)
Active: active (running) since Sat 2022-10-15 08:09:13 EDT; 7s ago
Main PID: 355530 (otelcol)
Tasks: 6 (limit: 23198)
Memory: 12.7M
CGroup: /system.slice/otelcol.service
└─355530 /usr/bin/otelcol --config=/etc/otelcol/config.yaml
配置 OpenTelemetry 接收器
要配置 OpenTelemetry 接收器,请编辑 Apache Skywalking 配置文件:
sudo vim /opt/apache-skywalking-apm-bin/config/application.yml
在文件中,确保在 enabledOtelRules 下捕获 VM,以便能够从 Linux 主机收集指标。
eceiver-otel:
selector: ${SW_OTEL_RECEIVER:default}
default:
enabledHandlers: ${SW_OTEL_RECEIVER_ENABLED_HANDLERS:"oc,otlp"}
enabledOtelRules: ${SW_OTEL_RECEIVER_ENABLED_OTEL_RULES:"istio-controlplane,k8s-node,oap,vm,mysql,postgresql"}
通常,各种enabledOtelRules在config/otel-rules/路径中定义
$ ls config/otel-rules/
istio-controlplane.yaml k8s-cluster.yaml k8s-instance.yaml k8s-node.yaml k8s-service.yaml mysql.yaml oap.yaml postgresql.yaml vm.yaml
对于本例,我们将使用 vm.yaml,它定义了用于从 Linux 主机收集指标的多个规则。
如果进行了任何更改,请重新启动服务
sudo systemctl restart skywalking
步骤 7 – 使用 Apache Skywalking 查看指标
现在,您可以导航到基础设施 -> Linux,查看使用 Prometheus Node Exporter 收集的指标
您可以在仪表板 -> 仪表板列表下加载图表
从列表中,识别 OS_LINUX 的仪表板并加载它,如图所示。
瞧!
判决
这标志着关于如何在 Linux 上安装和使用 Apache Skywalking 应用程序性能监控的详细指南的结束。使用此处获得的知识,您可以轻松配置对微服务、云原生和基于容器的环境的监控。我希望这是有用的。
查看更多:
- 在CentOS 8上安装Centreon监控工具 |洛基 Linux 8
- 在 Rocky Linux 9 | AlmaLinux 9 上安装 Grafana 和 Prometheus
- 在 Docker 容器中部署 Pinpoint APM(应用程序性能管理)