小白前段时间做Loki分布式追踪时,遇到需要在Nginx这一层生成TraceID和打印traceid相关日志的需求,在网上找了一大圈恁是没找到合适的Docker镜像。
原本应该用opentracing-contrib编译的nginx-opentracing
的docker镜像,但是当pull镜像时居然发现它有1.5G的容量,看了Dockerfile才知道这个镜像把Nginx源码和所有编译所需的C库都放在里面,真是佩服。
最后还是得我们自己根据OpenTracing的方法给Nginx安装插件。按照阿里云的文档,小白做了一个Nginx1.14版本镜像提供给大家使用。
quay.io/cloudxiaobai/nginx-jaeger:1.14.0
安装过程参考:https://help.aliyun.com/document_detail/126251.html
zipkin也可以用如上方法炮制
如何使用镜像
1. 首先要启用trace的话,我们需要定义个jaeger的描述配置:
jaeger.config
{
"service_name": "nginx",
"sampler": {
"type": "const",
"param": 1
},
"reporter": {
"localAgentHostPort": "jaeger:6831"
},
"headers": {
#一些默认的jaeger Baggage头设置
"jaegerDebugHeader": "jaeger-debug-id",
"jaegerBaggageHeader": "jaeger-baggage",
"traceBaggageHeaderPrefix": "uberctx-"
},
"baggage_restrictions": {
"denyBaggageOnInitializationFailure": false,
"hostPort": ""
}
}
2. nginx还需引入jaeger相关的动态库配置,如下:
nginx.conf
# 加载OpenTracing的动态库
load_module modules/ngx_http_opentracing_module.so;
http {
# 加载Jaeger库
opentracing_load_tracer /usr/local/lib/libjaegertracing_plugin.so /etc/jaeger-nginx-config.json;
# 启用Jaeger
opentracing on;
# 选择性的注入Tag
opentracing_tag http_user_agent $http_user_agent;
upstream backend {
server app-service:9001;
}
location ~ {
opentracing_operation_name $uri;
opentracing_propagate_context;
proxy_pass http://backend;
}
}
3. 如果我们需要将Trace的日志打印出来,可以设置自定义的日志格式:
log_format opentracing '{"timestamp":"$time_iso8601",'
'"source":"$server_addr",'
'"hostname":"$hostname",'
'"ip":"$http_x_forwarded_for",'
'"traceID":"$opentracing_context_uber_trace_id",'
'"client":"$remote_addr",'
'"request_method":"$request_method",'
'"scheme":"$scheme",'
'"domain":"$server_name",'
'"referer":"$http_referer",'
'"request":"$request_uri",'
'"args":"$args",'
'"size":$body_bytes_sent,'
'"status": $status,'
'"responsetime":$request_time,'
'"upstreamtime":"$upstream_response_time",'
'"upstreamaddr":"$upstream_addr",'
'"http_user_agent":"$http_user_agent",'
'"https":"$https"'
'}';
access_log /dev/stdout opentracing;
这样我们的日志打印出来的就是json格式
{
"source": "172.16.0.1",
"timestamp": "2020-11-02T15:58:15+00:00",
"args": "-",
"client": "172.16.0.20",
"domain": "",
"hostname": "e7b3c1369b96",
"http_user_agent": "Ruby",
"https": "",
"ip": "-",
"referer": "-",
"request": "/loki/api/v1/push",
"request_method": "POST",
"responsetime": 0.005,
"scheme": "http",
"size": 0,
"status": 204,
"traceID": "c39f7c73e43ddd29:c39f7c73e43ddd29:0:1",
"upstreamaddr": "172.20.0.3:3100",
"upstreamtime": "0.008"
}
扩展:Ingress-nginx怎么启用OpenTracing
如果你在kubernetes中用了ingress-nginx作为服务的网关,那么可以直接在ingress中启用opentracing而不用重建镜像。
在启用前,我们需要在ingress-nginx-controller的configmap里面添加如下配置:
apiVersion: v1
kind: ConfigMap
data:
enable-opentracing: "true"
#jaeger的常用篇日志
jaeger-collector-host: jaeger-agent.default.svc.cluster.local
jaeger-sampler-param: 1
jaeger-sampler-type: const
#zipkin的常用配置
zipkin-collector-host: zipkin.default.svc.cluster.local
zipkin-sample-rate: 1.0
#datadog的常用配置
datadog-collector-host: datadog-agent.default.svc.cluster.local
datadog-sample-rate: 1.0
metadata:
name: ingress-nginx-controller
namespace: kube-system
启用成功后,我们就可以在jaeger里面看到详情了。