文章目录
- SkyWalking监控java项目Halo博客
- SkyWalking监控nginx+Jenkins项目
- Jenkins部署
- nginx编译安装
- Skywalking nginx-lua-agent配置
- SkyWalking查看数据
SkyWalking监控java项目Halo博客
Halo是一个开源的博客项目,使用java编写,官网地址:https://halo.run/
安装java环境,Halo对java版本有限制,1.4.3版本以上需要使用java11以上
apt -y install openjdk-11-jdk
java --version
下载SkyWalking对应的Java Agent
wget https://dlcdn.apache.org/skywalking/java-agent/8.14.0/apache-skywalking-java-agent-8.14.0.tgz
tar xf apache-skywalking-java-agent-8.14.0.tgz -C /usr/local/src/
修改SkyWalking Java Agnet的配置文件
vim /usr/local/src/skywalking-agent/config/agent.config #修改下面3个配置参数,其余可保持默认
########################################
20 agent.service_name=${SW_AGENT_NAME:halo} #agent监控的项目或服务在SkyWalking UI上对应的服务名称
21
22 # The agent namespace
23 agent.namespace=${SW_AGENT_NAMESPACE:n70} #agent所在的名称空间,可以用来标示服务所属项目
104 collector.backend_service=${SW_AGENT_COLLECTOR_BACKEND_SERVICES:192.168.122.28:11800} #指定SkyWalking Server的地址和端口
下载Halo项目jar包
mkdir /usr/local/src/halo
wget https://github.com/halo-dev/halo/releases/download/v1.6.0/halo-1.6.0.jar -O /usr/local/src/
运行Halo项目
#通过-javaagent选项指定Java Agnet的路径
nohup java -javaagent:/usr/local/src/skywalking-agent/skywalking-agent.jar -jar /usr/local/src/halo/halo-1.6.0.jar &
运行之后对Halo进行初始华设置
之后就可以在SkyWalking的UI界面上看到关于Halo项目的数据
SkyWalking监控nginx+Jenkins项目
Jenkins部署
下载tomcat和jenkins安装包
cd /usr/local/src
wget https://dlcdn.apache.org/tomcat/tomcat-8/v8.5.86/bin/apache-tomcat-8.5.86.tar.gz
wget https://get.jenkins.io/war-stable/2.375.3/jenkins.war
tar xf apache-tomcat-8.5.86.tar.gz -C /usr/local/
ln -sv /usr/local/apache-tomcat-8.5.86 /usr/local/tomcat
cd /usr/local/tomcat/webapps
mv /usr/local/src/jenkins.war ./
修改catalina.sh,配置tomcat启动时加载skywalking java-agent
vim /usr/local/tomcat/bin/catalina.sh
############################
#在文件开头添加此行,指定skywalking java-agent位置
CATALINA_OPTS="$CATALINA_OPTS -javaagent:/usr/local/skywalking-agent/skywalking-agent.jar"; export CATALINA_OPTS
skywalking java-agent的下载和配置可以参考上面的Halo项目配置
启动tomcat
/usr/local/tomcat/bin/catalina.sh start
查看tomcat的启动日志/usr/local/tomcat/logs/catalina.out,确保服务正常启动
访问jenkins,进行初始化设置
现在SkyWalking UI界面就已经有Jenkins服务的相关数据了
nginx编译安装
由于Skywalking nginx-lua-agent需要lua环境,所以需要nginx编译的时候需要加载lua相关模块
配置luajit2
cd /usr/local
wget https://github.com/openresty/luajit2/archive/refs/tags/v2.1-20230119.tar.gz -O luajit2-2.1-20230119.tar.gz
tar xf luajit2-2.1-20230119.tar.gz
cd luajit2-2.1-20230119/
make install PREFIX=/usr/local/luajit2-2.1-20230119
#配置系统变量,编译nginx时使用
echo "export LUAJIT_LIB=/usr/local/luajit2-2.1-20230119/lib" >>/etc/profile
echo "export LUAJIT_INC=/usr/local/luajit2-2.1-20230119/include//luajit-2.1" >>/etc/profile
source /etc/profile
#配置系统加载luajit2相关的库文件
vim /etc/ld.so.conf.d/libc.conf
#############################
# libc default configuration
/usr/local/lib
/usr/local/luajit2-2.1-20230119/lib
#############################
ldconfig
配置lua-resty-core
cd /usr/local
wget https://github.com/openresty/lua-resty-core/archive/refs/tags/v0.1.25.tar.gz -O lua-resty-core-0.1.25.tar.gz
tar xf lua-resty-core-0.1.25.tar.gz
cd lua-resty-core-0.1.25/
mkae install PREFIX=/usr/local/luacore
配置lua-resty-lrucache
cd /usr/local
wget https://github.com/openresty/lua-resty-lrucache/archive/refs/tags/v0.13.tar.gz -O lua-resty-lrucache-0.13.tar.gz
tar xf lua-resty-lrucache-0.13.tar.gz
cd lua-resty-lrucache-0.13/
make install PREFIX=/usr/local/lua-lrucache
配置lua-cjson
cd /usr/local
wget https://github.com/openresty/lua-cjson/archive/refs/tags/2.1.0.9.tar.gz -O lua-cjson-2.1.0.9.tar.gz
tar xf lua-cjson-2.1.0.9.tar.gz
cd lua-cjson-2.1.0.9/
vim Makefile #编辑Makefile文件,将LUA_INCLUDE_DIR的路径修改为前面luajit2的路径
#####################
LUA_INCLUDE_DIR ?= /usr/local/luajit2-2.1-20230119/include//luajit-2.1
######################
#执行编译
make
make install
下载ngx_devel_kit和lua-nginx-module模块,编译nginx时添加
cd /usr/local/
wget https://github.com/vision5/ngx_devel_kit/archive/refs/tags/v0.3.2.tar.gz -O ngx_devel_kit-0.3.2.tar.gz
tar xf ngx_devel_kit-0.3.2.tar.gz
wget https://github.com/openresty/lua-nginx-module/archive/refs/tags/v0.10.23.tar.gz -O lua-nginx-module-0.10.23.tar.gz
tar xf lua-nginx-module-0.10.23.tar.gz
编译安装nginx
cd /usr/local/src/
wget http://nginx.org/download/nginx-1.22.1.tar.gz
tar xf nginx-1.22.1.tar.gz
cd nginx-1.22.1
./configure --prefix=/usr/local/nginx \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-pcre \
--with-file-aio \
--with-stream \
--with-stream_ssl_module \
--with-stream_realip_module \
--add-module=/usr/local/ngx_devel_kit-0.3.2/ \
--add-module=/usr/local/lua-nginx-module-0.10.23/ #注意要添加这两个模块
make && make install
测试nginx lua环境是否生效
vim /usr/local/nginx/conf/nginx.conf #修改nginx配置,导入lua包,添加lua测试页面
##################################
http {
include mime.types;
default_type application/octet-stream;
#添加此行,指定之前安装的lua-lrucache和lua-resty-core包位置
lua_package_path "/usr/local/lua-lrucache/lib/lua/?.lua;/usr/local/luacore/lib/lua/?.lua;;";
......
server {
#添加一个/hello测试页面
location /hello {
default_type text/html;
content_by_lua_block {
ngx.say("Hello Lua")
}
}
}
}
###################################
/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf #启动nginx
访问测试页面验证,能成功显示Hello Lua就表示lua环境已经生效
Skywalking nginx-lua-agent配置
nginx-lua-agent项目地址:https://github.com/apache/skywalking-nginx-lua nginx-lua-agent使用文档:https://skywalking.apache.org/docs/skywalking-java/v8.14.0/readme/
下载安装包
cd /usr/local/
wget https://github.com/apache/skywalking-nginx-lua/archive/refs/tags/v0.6.0.tar.gz -O skywalking-nginx-lua-0.6.0.tar.gz
wget https://github.com/openresty/lua-tablepool/archive/refs/tags/v0.02.tar.gz -O lua-tablepool-0.02.tar.gz
tar xf skywalking-nginx-lua-0.6.0.tar.gz
tar xf lua-tablepool-0.02.tar.gz
cp lua-tablepool-0.02/lib/tablepool.lua skywalking-nginx-lua-0.6.0/lib/
修改nginx配置文件,配置加载nginx-lua-agent
可以参考github上的文档进行配置:https://github.com/apache/skywalking-nginx-lua/tree/v0.6.0
vim /usr/local/nginx/nginx.conf
##################################
http {
......
#在http段添加下面的配置
#指定nginx-lua-agent包位置
lua_package_path "/usr/local/lua-lrucache/lib/lua/?.lua;/usr/local/luacore/lib/lua/?.lua;/usr/local/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 UI上显示的Service名字
metadata_buffer:set('serviceName', 'fronted-nginx')
-- Instance means the number of Nginx deployment, does not mean the worker instances
-- 指定实例名称,在SkyWalking UI上显示的Instance名字
metadata_buffer:set('serviceInstanceName', 'nginx-node1')
-- type 'boolean', mark the entrySpan include host/domain
metadata_buffer:set('includeHostInEntrySpan', false)
-- set random seed
require("skywalking.util").set_randomseed()
-- 指定Skywalking Backend Server的地址
require("skywalking.client"):startBackendTimer("http://192.168.122.29:11800")
-- 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 {
location / {
root html;
index index.html index.htm;
}
#在需要监控的后端(location配置)中启用nginx-lua-agent收集数据,以jenkins转发配置为例
location /jenkins {
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_tracer:start("jenkins")
-- If you want correlation custom data to the downstream service
-- skywalking_tracer:start("upstream service", {custom = "custom_value"})
}
proxy_pass http://192.168.122.29:8080;
body_filter_by_lua_block {
if ngx.arg[2] then
skywalking_tracer:finish()
end
}
log_by_lua_block {
skywalking_tracer:prepareForReport()
}
}
}
}
主要涉及两个位置:
- 在http{}段加载nginx-lua-agent模块,并添加关于nginx-lua-agent的一些初始化配置
- 在http{server{}}段针对需要监控的后端(location配置)中启用nginx-lua-agent收集数据
重新启动nginx
/usr/local/nginx/sbin/nginx -s stop
/usr/local/nginx/sbin/nginx
SkyWalking查看数据
先通过nginx访问jenkins,产生一些数据
然后就可以在SkyWalking UI查看nginx服务数据
在Topology页面可以看到当前服务的拓扑图
在Trace界面可以看到请求对应的调用链信息