使用SkyWalking監控nginx (以openresty為例)


安裝使用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頁面

故意訪問一個不存在的頁面或路徑


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM