前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >skywalking监控nginx-ingress-controller

skywalking监控nginx-ingress-controller

作者头像
没有故事的陈师傅
发布2021-02-01 11:03:42
1.8K0
发布2021-02-01 11:03:42
举报
文章被收录于专栏:运维开发故事
前提:有可用的kubernetes集群和skywalking监控。

软件版本信息:

软件

版本

kubernetes

1.17.2

nginx-ingress-controller

0.34.1

skywalking

8.1.0

skywalking-nginx-lua

0.2.0

下面直接进入正题......

(1)下载skywalking-nginx-lua

代码语言:javascript
复制
git clone https://github.com/apache/skywalking-nginx-lua.git

(2)修改util.lua文件名,因为其和nginx-ingress默认的一个lua脚本名字冲突,这里改一下。

代码语言:javascript
复制
$ skywalking-nginx-lua/lib/skywalking
# 修改文件名
$ mv util.lua swutil.lua
# 修改文件调用
## 可以使用grep查看一下哪些文件有调用这个lua
$ grep util `find ./ -type f`
./correlation_context.lua:local Util = require('util')
./segment_ref.lua:local Util = require('util')
./span.lua:local Util = require('util')
./tracing_context.lua:local Util = require('util')
./swutil_test.lua:local Util = require('util')
# 将里面调用的util都改为swuitl

(3)修改Nginx-ingress的nginx.tmpl模板文件,增加Skywalking的配置。

  • 添加Skywalking Lua脚本扫描路径
  • 增加环境变量读取,如:SW_SERVICE_NAME、SW_SERVICE_INSTANCE_NAME、SW_BACKEND_SERVERS
  • 添加Tracing使用的缓存tracing_buffer
  • 设置Skywalking Lua Agent的初始化方法,并将相关配置从环境变量中提取。
  • 设置http节点的追踪配置。
代码语言:javascript
复制
# Skywalking ENV
env SW_SERVICE_NAME;
env SW_SERVICE_INSTANCE_NAME;
env SW_BACKEND_SERVERS;

events {
    multi_accept        {{ if $cfg.EnableMultiAccept }}on{{ else }}off{{ end }};
    worker_connections  {{ $cfg.MaxWorkerConnections }};
    use                 epoll;
}

http {
        # 引入lua脚本
    lua_package_path "/etc/nginx/lua/?.lua;/etc/nginx/lua/skywalking/?.lua;;";
        # 使用缓存
    lua_shared_dict tracing_buffer 100m;

    {{ buildLuaSharedDictionaries $cfg $servers }}

    init_by_lua_block {
        collectgarbage("collect")

        -- init modules
        local ok, res

        ok, res = pcall(require, "lua_ingress")
        if not ok then
          error("require failed: " .. tostring(res))
        else
          lua_ingress = res
          lua_ingress.set_config({{ configForLua $all }})
        end

        ok, res = pcall(require, "configuration")
        if not ok then
          error("require failed: " .. tostring(res))
        else
          configuration = res
        end

        ok, res = pcall(require, "balancer")
        if not ok then
          error("require failed: " .. tostring(res))
        else
          balancer = res
        end

        {{ if $all.EnableMetrics }}
        ok, res = pcall(require, "monitor")
        if not ok then
          error("require failed: " .. tostring(res))
        else
          monitor = res
        end
        {{ end }}

        ok, res = pcall(require, "certificate")
        if not ok then
          error("require failed: " .. tostring(res))
        else
          certificate = res
          certificate.is_ocsp_stapling_enabled = {{ $cfg.EnableOCSP }}
        end

        ok, res = pcall(require, "plugins")
        if not ok then
          error("require failed: " .. tostring(res))
        else
          plugins = res
        end
        -- load all plugins that'll be used here
        plugins.init({ {{ range  $idx, $plugin := $cfg.Plugins }}{{ if $idx }},{{ end }}{{ $plugin | quote }}{{ end }} })
    }

    init_worker_by_lua_block {
......

        -- Skywalking
        local metadata_buffer = ngx.shared.tracing_buffer
        metadata_buffer:set('serviceName', os.getenv("SW_SERVICE_NAME"))
        metadata_buffer:set('serviceInstanceName', os.getenv("SW_SERVICE_INSTANCE_NAME"))
        
        -- set random seed
        require("swutil").set_randomseed()
        require("client"):startBackendTimer(os.getenv("SW_BACKEND_SERVERS"))
    }
......    
    
    rewrite_by_lua_block {
                lua_ingress.rewrite({{ locationConfigForLua $location $all }})
                balancer.rewrite()
                plugins.run()

                -- Skywalking
                require("tracer"):start({{ buildUpstreamName $location | quote }})
     }
......     
     
     body_filter_by_lua_block {
                -- Skywalking
                if ngx.arg[2] then
                    require("tracer"):finish()
                end
     }
......     
     
     log_by_lua_block {
                balancer.log()
                {{ if $all.EnableMetrics }}
                monitor.call()
                {{ end }}

                plugins.run()

                -- Skywalking
                require("tracer"):prepareForReport()
     }
.......

(4)将这些脚本添加到nginx-ingress镜像中,Dockerfile如下:

代码语言:javascript
复制
FROM quay.io/kubernetes-ingress-controller/nginx-ingress-controller:0.32.0

ADD --chown=www-data nginx.tmpl /etc/nginx/template
ADD --chown=www-data skywalking /etc/nginx/lua/skywalking

(5)构建镜像并上传仓库

代码语言:javascript
复制
$ docker build -t registry.cn-hangzhou.aliyuncs.com/rookieops/ingress-nginx-controller-skywalking:0.34.1 .
$ docker push registry.cn-hangzhou.aliyuncs.com/rookieops/ingress-nginx-controller-skywalking:0.34.1

(6)修改nginx-ingress的deployment文件,主要增加以下环境变量

代码语言:javascript
复制
......
      containers:
        - name: controller
          image: registry.cn-hangzhou.aliyuncs.com/rookieops/nginx-ingress-controller:0.32.0
          imagePullPolicy: IfNotPresent
......
            - name: SW_SERVICE_NAME
              value: Kubernetes Ingress
            - name: SW_BACKEND_SERVERS
              value: http://skywalking-oap.skywalking.svc.cluster.local:12800
            - name: SW_SERVICE_INSTANCE_NAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.uid
......

(7)然后重新部署ingress-controller应用。

然后可以在skywalking的面板上看到了。

image.png

已将所需的代码都放在github了,仓库地址:https://github.com/sunsharing-note/skywalking-ingress.git

参考:

  • https://github.com/lipangeng/Skywalking-Ingress-Overlay
  • https://github.com/apache/skywalking-nginx-lua

公众号:运维开发故事

github:https://github.com/orgs/sunsharing-note/dashboard

爱生活,爱运维

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2021-01-04,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 运维开发故事 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档