安装使用SkyWalking先看这篇文章,地址:https://www.cnblogs.com/sanduzxcvbnm/p/15829781.html
使用SkyWalking监控nginx借助的是nginx的lua模块,若没有则需要重新编译安装nginx,在这里直接使用openresty,这个自带就已经有lua模块了
wget https://openresty.org/package/centos/openresty.repo && mv openresty.repo /etc/yum.repos.d/ && yum makecache
yum -y install openresty
systemctl start openresty.service && systemctl enable openresty.service
下载SkyWalking Nginx LUA
地址:https://skywalking.apache.org/downloads/
下载后的文件是:skywalking-nginx-lua-0.6.0.tar.gz
解压缩skywalking-nginx-lua-0.6.0.tar.gz文件到nginx的conf目录下
nginx的conf目录以实际情况为准,解压后的文件夹是:skywalking-nginx-lua-0.6.0
tar -zxv -f skywalking-nginx-lua-0.6.0.tar.gz -C /usr/local/openresty/nginx/conf/
使用官方提供的nignx配置文件进行配置演示
官方参考文档地址:https://github.com/apache/skywalking-nginx-lua
官方参考配置:
http {
lua_package_path "/Path/to/.../skywalking-nginx-lua/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
metadata_buffer:set('serviceName', 'User Service Name')
-- Instance means the number of Nginx deployment, does not mean the worker instances
metadata_buffer:set('serviceInstanceName', 'User Service Instance Name')
-- 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()
require("skywalking.client"):startBackendTimer("http://127.0.0.1:12800") # 这儿应该是12800,不是8080,官方文档写的有问题
-- 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 8090;
location /ingress {
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_tracer:start("upstream service")
-- If you want correlation custom data to the downstream service
-- skywalking_tracer:start("upstream service", {custom = "custom_value"})
}
-- Target upstream service
proxy_pass http://127.0.0.1:8080/backend;
body_filter_by_lua_block {
if ngx.arg[2] then
skywalking_tracer:finish()
end
}
log_by_lua_block {
skywalking_tracer:prepareForReport()
}
}
}
}
在解压的skywalking-nginx-lua-0.6.0/examples路径下有一个skywalking官方提供的nginx.conf配置文件,用这个文件替换安装openresty后的那个默认nginx.conf文件,然后再修改这个文件。
主要是修改lua_package_path参数,也就是引用的lua文件路径和访问skywalking后端服务的端口号
然后启动nginx服务,使用命令:curl http://127.0.0.1:8090/ingress
,结果如下:
# curl 127.0.0.1:8090/ingress
<p>Backend service for testing only.</p>
<p>Backend sw8 received headers: 1-ZjhlNjNjYjMtMDU3Yy00ZDQ4LWEwNmItMDVhYWU4ZDQ5MzUz-MzZjNjY3ZmMtMjZjMi00MjQ0LWEzOTctODZjNWZkOTZlZmQz-1-VXNlciBTZXJ2aWNlIE5hbWU=-VXNlciBTZXJ2aWNlIEluc3RhbmNlIE5hbWU=-L3RpZXIyL2xi-YmFja2VuZCBzZXJ2aWNl</p>
对照nginx.conf文件分析刚才使用的命令,可以得到如下结果:
1.访问127.0.0.1:8090/ingress对应的是nginx.conf配置文件中的server {listen 8090;}
,location /ingress {}
配置
2.这个location /ingress {}
配置中实际访问使用的是:proxy_pass http://127.0.0.1:8090/tier2/lb;
,把接受过来的请求转发给这个服务了。
3.分析proxy_pass http://127.0.0.1:8090/tier2/lb;
,对应的是 server {listen 8090;}
,location /tier2/lb
配置
4.这个配置再把请求转发给proxy_pass http://127.0.0.1:8090/backend;
,这个服务对应的是 server {listen 8090;}
,location /backend
配置
5.这个配置的内容如下,正好是请求curl http://127.0.0.1:8090/ingress
后得到的相应结果。
content_by_lua_block {
ngx.say("<p>Backend service for testing only.</p>")
ngx.say("<p>Backend sw8 received headers: " .. ngx.req.get_headers()["sw8"] .. "</p>")
}
根据实际情况修改nginx.conf文件
skywalking官方提供的nginx.conf文件可以作为测试分析使用,实际使用的话可以直接在原有nginx.conf文件的基础上新增加上需要的配置即可。
nginx.conf实际配置
http {
lua_package_path "/usr/local/openresty/nginx/conf/skywalking-nginx-lua-0.6.0/lib/?.lua;;"; # 根据实际情况修改这个路径,跟上面解压缩后的路径保持一致,(lib目录下还有子目录,子目录下才是lua文件)
lua_shared_dict tracing_buffer 100m;
init_worker_by_lua_block {
local metadata_buffer = ngx.shared.tracing_buffer
metadata_buffer:set('serviceName', 'httpnginx') # 修改一下名称
metadata_buffer:set('serviceInstanceName', 'httpInstancenginx') # 修改一下名称
metadata_buffer:set('includeHostInEntrySpan', false)
require("skywalking.util").set_randomseed()
require("skywalking.client"):startBackendTimer("http://127.0.0.1:12800") # 修改成连接skywalking后端服务的地址和端口(我这都是在一台主机上)
skywalking_tracer = require("skywalking.tracer")
}
.......
# 静态文件示例,没有后端服务
server {
listen 80;
server_name localhost;
access_log logs/host.access.log main;
location / {
default_type text/html;
root html;
index index.html index.htm;
# 如下内容直接新增
rewrite_by_lua_block {
skywalking_tracer:start("listennginx") # 修改一下名称
}
body_filter_by_lua_block {
if ngx.arg[2] then
skywalking_tracer:finish()
end
}
log_by_lua_block {
skywalking_tracer:prepareForReport()
}
}
}
# 有后端服务的示例
server {
listen 80;
server_name localhost;
location /ingress { # ingress根据实际情况修改
default_type text/html;
rewrite_by_lua_block {
skywalking_tracer:start("upstream service")
}
proxy_pass http://127.0.0.1:8000/xxx; # 反向代理的后端服务和端口等根据实际情况修改
body_filter_by_lua_block {
if ngx.arg[2] then
skywalking_tracer:finish()
end
}
log_by_lua_block {
skywalking_tracer:prepareForReport()
}
}
}
}
效果
以静态文件示例,没有后端服务为例
用浏览器访问这个主机的80端口,出现的是openresty默认页面,此时查看skywalking页面
故意访问一个不存在的页面或路径