Enable Xtrace for ALB Ingress with an AlbConfig

更新时间:
复制 MD 格式

Enable Xtrace in AlbConfig to attach trace IDs at the ALB ingress and correlate access logs with distributed traces—without per-service instrumentation.

Prerequisites

Ensure you have:

How it works

When a request hits the ALB listener, ALB generates a trace ID with the Zipkin sampling algorithm and injects it into request headers. ALB forwards the request to the backend with the trace ID attached. Access logs—including the trace ID—are written to a Simple Log Service (SLS) Logstore. Managed Service for OpenTelemetry reads these logs, reconstructs the trace, and displays it in the OpenTelemetry console.

Configure Xtrace at the listener level in AlbConfig. The sampling rate applies to all traffic on that listener.

Enable Xtrace for ALB Ingress

Step 1: Enable Simple Log Service for the cluster

Enable Simple Log Service for your cluster to auto-create an SLS project. See Collect logs from ACK cluster containers.

Step 2: Get the SLS project ID

  1. Log on to the ACK console and click Clusters.

  2. Click your cluster name, then click Cluster Information.

  3. On the Basic Information tab, copy the Simple Log Service Project value for Step 3.

Step 3: Create an AlbConfig

The AlbConfig creates an ALB instance with Simple Log Service access logs and Xtrace enabled on the listener.

  1. Create alb-test.yaml:

    Modify Xtrace settings only after Simple Log Service access logs are enabled on the ALB instance. To reuse an existing ALB instance, set forceOverride: true under spec.config to overwrite existing properties. See Reuse an existing ALB instance.
    See Create and manage vSwitches.

    Xtrace parameters

    Parameter

    Description

    Default

    Range

    tracingEnabled

    Enables Xtrace for the listener

    false

    true / false

    tracingSample

    Sampling rate. 10000 = 100%, 5000 = 50%, 1 = 0.01%

    1–10000

    tracingType

    Sampling algorithm

    Zipkin

    apiVersion: alibabacloud.com/v1
    kind: AlbConfig
    metadata:
      name: alb-demo
    spec:
      config:
        name: alb-test
        addressType: Intranet
        zoneMappings: # The vSwitch must be in an ALB-supported zone and the same VPC as the cluster. Select at least two vSwitches in different zones for high availability.
        - vSwitchId: vsw-2vc82nndnoo**********  # Replace with your vSwitch ID.
        - vSwitchId: vsw-2vc30f5mlhs**********
        accessLogConfig:
          logProject: "k8s-log-xz92lvykqj1siwvif****"  # Replace with your SLS project ID from Step 2.
          logStore: alb_xz92lvykqj1siwvif****           # Must start with "alb_". Created automatically if it does not exist.
      listeners:
      - port: 80
        protocol: HTTP
        logConfig:
          accessLogRecordCustomizedHeadersEnabled: false
          accessLogTracingConfig:
            tracingEnabled: true    # Set to true to enable Xtrace. Default: false.
            tracingSample: 9999     # Sampling rate. Range: 1-10000, where 10000 = 100%. Example: 9999 = 99.99%.
            tracingType: Zipkin     # Sampling algorithm. Only takes effect when tracingEnabled is true.
  2. Apply the AlbConfig:

    kubectl apply -f alb-test.yaml

    Expected output:

    albconfig.alibabacloud.com/alb-demo created

Step 4: Create an IngressClass

  1. Create alb.yaml:

    apiVersion: networking.k8s.io/v1
    kind: IngressClass
    metadata:
      name: alb
    spec:
      controller: ingress.k8s.alibabacloud/alb
      parameters:
        apiGroup: alibabacloud.com
        kind: AlbConfig
        name: alb-demo
  2. Apply the IngressClass:

    kubectl apply -f alb.yaml

    Expected output:

    ingressclass.networking.k8s.io/alb created

Step 5: Deploy a service and configure Ingress routing

  1. Create cafe-service.yaml to deploy a coffee Deployment and coffee-svc Service:

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: coffee
    spec:
      replicas: 2
      selector:
        matchLabels:
          app: coffee
      template:
        metadata:
          labels:
            app: coffee
        spec:
          containers:
          - name: coffee
            image: registry.cn-hangzhou.aliyuncs.com/acs-sample/nginxdemos:latest
            ports:
            - containerPort: 80
    ---
    apiVersion: v1
    kind: Service
    metadata:
      name: coffee-svc
    spec:
      ports:
      - port: 80
        targetPort: 80
        protocol: TCP
      selector:
        app: coffee
      type: NodePort
  2. Deploy the service:

    kubectl apply -f cafe-service.yaml

    Expected output:

    deployment.apps/coffee created
    service/coffee-svc created
  3. Create cafe-ingress.yaml with Ingress routing rules:

    apiVersion: networking.k8s.io/v1
    kind: Ingress
    metadata:
      name: cafe-ingress
    spec:
      ingressClassName: alb
      rules:
       - host: demo.domain.ingress.top
         http:
          paths:
          - path: /coffee
            pathType: ImplementationSpecific
            backend:
              service:
                name: coffee-svc
                port:
                  number: 80
  4. Apply the Ingress:

    kubectl apply -f cafe-ingress.yaml

    Expected output:

    ingress.networking.k8s.io/cafe-ingress created

Step 6: Configure domain name resolution

If you set a custom domain in spec.rules.host, add a CNAME record pointing to the ALB DNS name. See Create and use an ALB Ingress to expose Services to external traffic.

Step 7: Send requests to generate trace data

  1. Get the ALB address:

    kubectl get ing

    Expected output:

    NAME           CLASS   HOSTS                     ADDRESS                                              PORTS   AGE
    cafe-ingress   alb     demo.domain.ingress.top   alb-u53i28ewt580*****.cn-<Region>.alb.aliyuncs.com   80      16m
  2. Send a request:

    Traces appear in the console after enough requests are sampled. With tracingSample: 9999 (99.99%), one request is enough. With a lower rate—for example, tracingSample: 100 (1%)—send at least 100 requests.
    curl -H Host:demo.domain.ingress.top http://alb-u53i28ewt580*****.cn-<Region>.alb.aliyuncs.com/coffee
    for i in $(seq 1 100); do curl -s -o /dev/null -H Host:demo.domain.ingress.top http://alb-u53i28ewt580*****.cn-<Region>.alb.aliyuncs.com/coffee; done

    The response is an HTML page titled Hello World.

Verify tracing

Check SLS access logs

  1. Log on to the ACK console and click Clusters.

  2. Click your cluster name, then click Cluster Information.

  3. On the Basic Information tab, click the Simple Log Service Project link to open the SLS console. Select the Logstore whose name starts with alb_ (for example, alb_xz92lvykqj1siwvif****). The xtrace search should match slb_xtrace (trace ID) and xtrace_type (value zipkin), confirming access logs record tracing data.

    image

View traces in the OpenTelemetry console

  1. Log on to the Managed Service for OpenTelemetry console.

  2. On the Applications page, select your region and click your application name.

  3. Click API Calls > Trace to view trace data. The tab shows up to 100 longest-duration traces. See Interface Calls.

Next steps