文章目录
- 前言
- Jenkins案例
- 安装Jenkins
- 安装Skywalking-Java-Agent
- Nginx案例
- 安装OpenResty
- 安装Skywalking-Nginx-Lua
- 全链路追踪
前言
在上一节 10-4 Skywalking介绍,二进制与docker部署Skywalking,Skywalking收集Java博客案例,Skywalking面板介绍 我们介绍的Skywalking组件,部署和java博客案例等。
这一节继续扩展数据收集案例。将部署基于Java开发的一种持续集成工具Jenkins,和高性能的HTTP和反向代理web服务器Nginx,并在Skywalking对两个应用实现全链路追踪。
IP | 名称 | 应用 |
---|
192.168.100.201 | Skywalking服务器 | Skywalking-OAP, Skywalking-UI |
192.168.100.204 | Jenkins服务器 | Java JDK 11, Tomcat 9, Jenkins 2.361, Skywalking-Java-Agent |
192.168.100.205 | Nginx服务器 | OpenResty 1.21(带Lua模块的Nginx), Skywalking-Nginx-Lua-Agent |
Jenkins案例
安装Jenkins
安装 Java JDK 11 版本
yum install -y java-11-openjdk
安装 Tomcat 9.0.68 版本
wget --no-check-certificate https://dlcdn.apache.org/tomcat/tomcat-9/v9.0.68/bin/apache-tomcat-9.0.68.tar.gz
tar zxvf apache-tomcat-9.0.68.tar.gz
mkdir /apps
mv apache-tomcat-9.0.68 /apps
下载 Jenkins 2.361.3 版本
curl -LO https://get.jenkins.io/war-stable/2.361.3/jenkins.war
mv jenkins.war /apps/apache-tomcat-9.0.68/webapps/
安装Skywalking-Java-Agent
下载 apache-skywalking-java-agent-8.8.0 版本
wget https://archive.apache.org/dist/skywalking/java-agent/8.8.0/apache-skywalking-java-agent-8.8.0.tgz
tar zxvf apache-skywalking-java-agent-8.8.0.tgz
mv skywalking-agent/ /apps/
Tomcat的配置文件,在java启动时,加载javaagent:
vim +125 /apps/apache-tomcat-9.0.68/bin/catalina.sh
125 CATALINA_OPTS="$CATALINA_OPTS -javaagent:/apps/skywalking-agent/skywalking-agent.jar"
126 export CATALINA_OPTS
Agent的配置文件,与Skywalking关联:
vim /apps/skywalking-agent/config/agent.config
18 agent.namespace=${SW_AGENT_NAMESPACE:my_project}
21 agent.service_name=${SW_AGENT_NAME:my_project_jenkins}
93 collector.backend_service=${SW_AGENT_COLLECTOR_BACKEND_SERVICES:192.168.100.201:11800}
启动tomcat和agent,停用防火墙,监听端口为8080:
nohup /apps/apache-tomcat-9.0.68/bin/catalina.sh run &>>/root/catalina.log &
sudo systemctl disable --now firewalld.service
netstat -lntp | grep java
tcp6 0 0 127.0.0.1:8005 :::* LISTEN 1540/java
tcp6 0 0 :::8080 :::* LISTEN 1540/java
开机自启动tomcat,一定要nohup后台运行,否则开不了机。
echo "nohup /apps/apache-tomcat-9.0.68/bin/catalina.sh run &>>/root/catalina.log &" >> /etc/rc.local
chmod +x /etc/rc.d/rc.local
访问 http://192.168.100.204:8080/jenkins/ 进入初始化页面,按提示粘贴密码:
初始化完毕,进入Jenkins主页面:
在Skywalking服务器追踪到Jenkins网站的链路数据:
Nginx案例
SkyWalking Nginx Agent只为Nginx提供了由Nginx LUA模块驱动的本机跟踪功能。Nginx不自带LUA模块,可以编译安装或者使用openresty。
OpenResty是一个基于 Nginx 与 Lua 的高性能 Web 平台,其内部集成了大量精良的 Lua 库、第三方模块以及大多数的依赖项。
OpenResty官网
https://openresty.org/
安装OpenResty
配置相关yum源:
wget https://openresty.org/package/centos/openresty.repo
mv openresty.repo /etc/yum.repos.d/
sudo yum check-update
在线安装openresty,监听端口为80:
sudo yum install -y openresty
sudo systemctl enable --now openresty.service
sudo systemctl status openresty.service
netstat -ltnp | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1649/nginx: master
sudo systemctl disable --now firewalld.service
访问 http://192.168.100.205 进入欢迎页面:
安装Skywalking-Nginx-Lua
skywalking-nginx-lua是nginx的agent,基本由lua语言编写,所以需要nginx有lua模块。
wget https://github.com/apache/skywalking-nginx-lua/archive/refs/tags/v0.6.0.tar.gz
tar zxvf v0.6.0.tar.gz
mkdir /apps
mv skywalking-nginx-lua-0.6.0 /apps
Nginx主配置文件,通过systemctl status openresty.service找到路径:
vim /usr/local/openresty/nginx/conf/nginx.conf
http {
include /apps/openresty/*.conf
...
}
Nginx子配置文件,可参考github。配置内容较多,关注有中文注释的行数:
SkyWalking Nginx Agent配置文件说明
https://github.com/apache/skywalking-nginx-lua
vim /apps/openresty/mysite.conf
##### 指定skywalking的lua包路径 #####
lua_package_path "/apps/skywalking-nginx-lua-0.6.0/lib/?.lua;;";
# Buffer represents the register inform and the queue of the finished segment
lua_shared_dict tracing_buffer 100m;
# Init is the timer setter and keeper
# Setup an infinite loop timer to do register and trace report.
init_worker_by_lua_block {
local metadata_buffer = ngx.shared.tracing_buffer
-- Set service name
##### 在skywalking显示的服务名 #####
metadata_buffer:set('serviceName', 'my_project_nginx')
-- Instance means the number of Nginx deployment, does not mean the worker instances
##### 在skywalking显示的实例名 #####
metadata_buffer:set('serviceInstanceName', 'nginx_node1')
-- type 'boolean', mark the entrySpan include host/domain
metadata_buffer:set('includeHostInEntrySpan', false)
-- set ignoreSuffix, If the operation name(HTTP URI) of the entry span includes suffixes in this set, this segment would be ignored. Multiple values should be separated by a comma(',').
-- require("skywalking.util").set_ignore_suffix(".jpg,.jpeg,.js,.css,.png,.bmp,.gif,.ico,.mp3,.mp4,.svg")
-- set random seed
require("skywalking.util").set_randomseed()
##### 关联skywalking客户端IP与端口,注意是12800,不是11900 #####
require("skywalking.client"):startBackendTimer("http://192.168.100.201:12800")
-- If there is a bug of this `tablepool` implementation, we can
-- disable it in this way
-- require("skywalking.util").disable_tablepool()
skywalking_tracer = require("skywalking.tracer")
}
server {
listen 80;
##### nginx监听域名 #####
server_name www.mysite.com;
##### nginx监听路径 #####
location /jenkins {
default_type text/html;
rewrite_by_lua_block {
------------------------------------------------------
-- NOTICE, this should be changed manually
-- This variable represents the upstream logic address
-- Please set them as service logic name or DNS name
--
-- Currently, we can not have the upstream real network address
------------------------------------------------------
##### skywalking追踪的服务 #####
skywalking_tracer:start("www.mysite.com")
-- If you want correlation custom data to the downstream service
-- skywalking_tracer:start("upstream service", {custom = "custom_value"})
}
##### nginx代理路径,这里设成jenkins服务器 #####
proxy_pass http:
body_filter_by_lua_block {
if ngx.arg[2] then
skywalking_tracer:finish()
end
}
log_by_lua_block {
skywalking_tracer:prepareForReport()
}
}
}
测试nginx配置语法和使用,重启openresty服务:
sudo /usr/local/openresty/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/openresty/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/openresty/nginx/conf/nginx.conf test is successful
sudo systemctl restart openresty.service
全链路追踪
Windows电脑上修改hosts文件,192.168.100.205 为Nginx服务器:
C:\Windows\System32\drivers\etc
192.168.100.205 www.mysite.com
访问 www.mysite.com/jenkins 重新进入jenkins管理页面:
在Skywalking服务器追踪到Nginx和Jenkins两个应用的链路情况:
Nginx和Jenkins的拓步图:用户请求Nginx会转发到www.mysite.com,mysite对应jenkins,直接被Skywalking追踪:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)