【K8s教程】Nginx Ingress 控制器通过 OpenTracing 项目进行分布式跟踪说明

参考:
https://kubernetes.github.io/ingress-nginx/user-guide/third-party-addons/opentracing/

启用 NGINX 服务的请求,通过 OpenTracing 项目进行分布式跟踪。

使用第三方模块 opentracing-contrib/nginx-opentracing(https://github.com/opentracing-contrib/nginx-opentracing) ,NGINX 入口控制器可以配置 NGINX 以启用 OpenTracing(http://opentracing.io/) 检测。 默认情况下,此功能处于禁用状态。

用法

要启用检测,我们必须在配置 ConfigMap 中启用 OpenTracing:

data:
  enable-opentracing: "true"

要为单个 Ingress 启用或禁用检测,请使用 enable-opentracing注解:

kind: Ingress
metadata:
  annotations:
    nginx.ingress.kubernetes.io/enable-opentracing: "true"

我们还必须设置上传跟踪时使用的主机:

zipkin-collector-host: zipkin.default.svc.cluster.local
jaeger-collector-host: jaeger-agent.default.svc.cluster.local
datadog-collector-host: datadog-agent.default.svc.cluster.local

注意:虽然该选项被调用 jaeger-collector-host,你需要把它指向一个 jaeger-agent,而不是 jaeger-collector成分。 或者,您可以设置 jaeger-endpoint并指定用于上传跟踪的完整端点。 这将使用 TCP 并且应该用于收集器而不是代理。

接下来,您需要部署一个使用 OpenTracing 的分布式跟踪系统。 Zipkin 和 Jaeger 以及 Datadog 已经过测试。

其他可选配置选项:

# specifies the name to use for the server span
opentracing-operation-name

# specifies specifies the name to use for the location span
opentracing-location-operation-name

# specifies the port to use when uploading traces, Default: 9411
zipkin-collector-port

# specifies the service name to use for any traces created, Default: nginx
zipkin-service-name

# specifies sample rate for any traces created, Default: 1.0
zipkin-sample-rate

# specifies the port to use when uploading traces, Default: 6831
jaeger-collector-port

# specifies the endpoint to use when uploading traces to a collector instead of an agent
jaeger-endpoint

# specifies the service name to use for any traces created, Default: nginx
jaeger-service-name

# specifies the traceparent/tracestate propagation format
jaeger-propagation-format

# specifies the sampler to be used when sampling traces.
# The available samplers are: const, probabilistic, ratelimiting, remote, Default: const
jaeger-sampler-type

# specifies the argument to be passed to the sampler constructor, Default: 1
jaeger-sampler-param

# Specifies the custom remote sampler host to be passed to the sampler constructor. Must be a valid URL.
# Default: http://127.0.0.1
jaeger-sampler-host

# Specifies the custom remote sampler port to be passed to the sampler constructor. Must be a number. Default: 5778
jaeger-sampler-port

# Specifies the header name used for passing trace context. Must be a string. Default: uber-trace-id
jaeger-trace-context-header-name

# Specifies the header name used for force sampling. Must be a string. Default: jaeger-debug-id
jaeger-debug-header

# Specifies the header name used to submit baggage if there is no root span. Must be a string. Default: jaeger-baggage
jaeger-baggage-header

# Specifies the header prefix used to propagate baggage. Must be a string. Default: uberctx-
jaeger-tracer-baggage-header-prefix

# specifies the port to use when uploading traces, Default 8126
datadog-collector-port

# specifies the service name to use for any traces created, Default: nginx
datadog-service-name

# specifies the environment this trace belongs to, Default: prod
datadog-environment

# specifies the operation name to use for any traces collected, Default: nginx.handle
datadog-operation-name-override

# Specifies to use client-side sampling for distributed priority sampling and ignore sample rate, Default: true
datadog-priority-sampling

# specifies sample rate for any traces created, Default: 1.0
datadog-sample-rate

所有这些选项(包括主机)都允许环境变量,例如 $HOSTNAME或者 $HOST_IP. 在 Jaeger 的情况下,如果您在集群中的每台机器上运行一个 Jaeger 代理,您可以使用类似 $HOST_IP(它可以与 status.hostIPfieldPath会,如所描述 这里 ),以确保跟踪会被发送到本地代理。

例子

以下示例展示了如何部署和测试不同的分布式跟踪系统。 这些示例可以使用 Minikube 执行。

Zipkin

在 rnburn/zipkin-date-server(https://github.com/rnburn/zipkin-date-server) GitHub 存储库中是 dockerized 日期服务的示例。 要安装示例和 Zipkin 收集器,请运行:

kubectl create -f https://raw.githubusercontent.com/rnburn/zipkin-date-server/master/kubernetes/zipkin.yaml
kubectl create -f https://raw.githubusercontent.com/rnburn/zipkin-date-server/master/kubernetes/deployment.yaml

我们还需要使用所需的值配置 NGINX 控制器 ConfigMap:

$ echo '
apiVersion: v1
kind: ConfigMap
data:
  enable-opentracing: "true"
  zipkin-collector-host: zipkin.default.svc.cluster.local
metadata:
  name: ingress-nginx-controller
  namespace: kube-system
' | kubectl replace -f -

在Zipkin界面我们可以看到详细信息:
【K8s教程】Nginx Ingress 控制器通过 OpenTracing 项目进行分布式跟踪说明

Jaeger

在 Minikube 中启用 Ingress 插件:

$ minikube addons enable ingress

将 Minikube IP 添加到 /etc/hosts:

$ echo "$(minikube ip) example.com" | sudo tee -a /etc/hosts

应用基本服务和入口资源:

# Create Echoheaders Deployment
$ kubectl run echoheaders --image=k8s.gcr.io/echoserver:1.4 --replicas=1 --port=8080

# Expose as a Cluster-IP
$ kubectl expose deployment echoheaders --port=80 --target-port=8080 --name=echoheaders-x

# Apply the Ingress Resource
$ echo '
  apiVersion: networking.k8s.io/v1beta1
  kind: Ingress
  metadata:
    name: echo-ingress
  spec:
    rules:
    - host: example.com
      http:
        paths:
        - backend:
            serviceName: echoheaders-x
            servicePort: 80
          path: /echo
  ' | kubectl apply -f -

启用 OpenTracing 并设置 jaeger-collector-host:

$ echo '
  apiVersion: v1
  kind: ConfigMap
  data:
    enable-opentracing: "true"
    jaeger-collector-host: jaeger-agent.default.svc.cluster.local
  metadata:
    name: ingress-nginx-controller
    namespace: kube-system
  ' | kubectl replace -f -

应用 Jaeger 多合一模板:

$ kubectl apply -f https://raw.githubusercontent.com/jaegertracing/jaeger-kubernetes/master/all-in-one/jaeger-all-in-one-template.yml

向服务提出一些请求:

$ curl example.com/echo -d "meow"

CLIENT VALUES:
client_address=172.17.0.5
command=POST
real path=/echo
query=nil
request_version=1.1
request_uri=http://example.com:8080/echo

SERVER VALUES:
server_version=nginx: 1.10.0 - lua: 10001

HEADERS RECEIVED:
accept=*/*
connection=close
content-length=4
content-type=application/x-www-form-urlencoded
host=example.com
user-agent=curl/7.54.0
x-forwarded-for=192.168.99.1
x-forwarded-host=example.com
x-forwarded-port=80
x-forwarded-proto=http
x-original-uri=/echo
x-real-ip=192.168.99.1
x-scheme=http
BODY:
meow

查看 Jaeger 用户界面:

$ minikube service jaeger-query --url

http://192.168.99.100:30183

在Jaeger界面我们可以看到详细信息:
【K8s教程】Nginx Ingress 控制器通过 OpenTracing 项目进行分布式跟踪说明

上一篇:Jaeger的客户端采样配置(Java版)


下一篇:Jaeger demo 应用: HotROD 分析