Configure ALB Ingress tracing with an AlbConfig

更新时间:
复制 MD 格式

Alibaba Cloud provides the Managed Service for OpenTelemetry service that offers developers of distributed applications various features, such as trace mapping, request statistics, and trace topology. This service helps you quickly analyze and diagnose performance bottlenecks in a distributed application architecture and improves the efficiency of development and diagnostics for microservices applications. You can install the ALB Ingress controller in Container Compute Service (ACS) clusters and enable tracing analysis to view service traces and the relevant data.

Prerequisites

Step 1: Obtain the ID of the Simple Log Service project

  1. Log on to the ACS console. In the left navigation pane, click Clusters.

  2. On the Clusters page, click the name of the target cluster. In the left navigation pane, choose Cluster Information.

  3. On the Basic Information tab, find the Cluster Resources section. Copy and save the ID next to Log Service Project.

    Note

    If Simple Log Service is not enabled when you create the cluster, you can manually install alibaba-log-controller to enable the service. The default project ID is k8s-log-ACS cluster ID. For more information, see Enable the Simple Log Service component.

Step 2: Configure access logs and tracing analysis

  1. Create a file named alb-test.yaml and copy the following content to the file. The file is used to create an AlbConfig Object.

    apiVersion: alibabacloud.com/v1
    kind: AlbConfig
    metadata:
      name: alb-demo
    spec:
      config:
        name: alb-test
        addressType: Internet
        # The specified vSwitches must reside in the zones supported by the current ALB instance and in the VPC of the ACS cluster. To ensure the high availability of services, select at least two vSwitches in different zones. 
        zoneMappings:
        - vSwitchId: vsw-2vc82nndnoo**********          #  Specify the IDs of the vSwitches that are used by the ALB instance. 
        - vSwitchId: vsw-2vc30f5mlhs**********          # Specify the IDs of the vSwitches that are used by the ALB instance. 
        accessLogConfig:
          logProject: k8s-log-xz92lvykqj1siwvif****     # Specify the ID of the Simple Log Service project that you obtained in Step 1. 
          logStore: alb_test_xz92lvykqj1siwvif****      # The Logstore name must start with alb_. If the specified Logstore does not exist, the system automatically creates one. 
      listeners:
      - port: 80
        protocol: HTTP
        logConfig:
          accessLogRecordCustomizedHeadersEnabled: false
          accessLogTracingConfig:                       # Configure Xtrace parameters. To do this, you must first configure accessLogConfig. 
            tracingEnabled: true                        # Specify whether to enable or disable the Xtrace feature. The default value is false. To enable Xtrace, set this parameter to true. 
            tracingSample: 9999                         # The sampling rate of the Xtrace feature. Valid values: 1 to 10000. This parameter takes effect only if you set tracingEnabled to true. 
            tracingType: Zipkin                         # The sampling algorithm of the Xtrace feature. Set the value to Zipkin. This parameter takes effect only if you set tracingEnabled to true.
  2. Run the following command to create an AlbConfig:

    kubectl apply -f alb-test.yaml

    Expected output:

    albconfig.alibabacloud.com/alb-demo created
  3. Create a file named alb.yaml and copy the following content to the file. The file is used to create an IngressClass.

    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
  4. Run the following command to create an IngressClass:

    kubectl apply -f alb.yaml

    Expected output:

    ingressclass.networking.k8s.io/alb created
Note

After you complete the configuration, a Logstore named alb_test_xz92lvykqj1siwvif**** is created in the Simple Log Service project.

Step 3: Deploy sample services

  1. Create a file named cafe-service.yaml and copy the following content to the file. The file is used to create a Deployment named coffee and a Service named coffee.

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: coffee
    spec:
      replicas: 1
      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: ClusterIP
  2. Run the following command to deploy the Deployment and Service:

    kubectl apply -f cafe-service.yaml

    Expected output:

    deployment.apps/coffee created
    service/coffee-svc created
  3. Create a file named cafe-ingress.yaml and copy the following content to the file. The file is used to configure forwarding rules for the ALB Ingress to expose the Service.

    apiVersion: networking.k8s.io/v1
    kind: Ingress
    metadata:
      name: cafe-ingress 
    spec:
      ingressClassName: alb
      rules:
       - host: demo.domain.ingress.top
         http:
          paths:
          # Configure a context path.
          - path: /coffee
            pathType: ImplementationSpecific
            backend:
              service:
                name: coffee-svc
                port: 
                  number: 80
  4. Run the following command to configure the publicly-accessible domain name and path of the coffee Service:

    kubectl apply -f cafe-ingress.yaml

    Expected output:

    ingress.networking.k8s.io/cafe-ingress created
  5. (Optional) If you set the spec.rules.host parameter to a custom domain name when you create the Ingress, you must add a CNAME record to map the domain name to the domain name of the ALB instance. Then, you can access the Services by using the custom domain name. For more information, see Configure a custom domain name.

Step 4: View tracing data

  1. Run the following command to obtain the address of the ALB instance (ADDRESS):

    kubectl get ingress

    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. Run the following command to access the Service:

    curl -H Host:demo.domain.ingress.top http://alb-u53i28ewt580*****.cn-<Region>.alb.aliyuncs.com/coffee
    Note

    The expected output is an HTML page with the title Hello World. Access the domain name from your web browser. The following page is displayed.

    image

  3. Check the access log of the ALB instance in Simple Log Service. Make sure that the preceding service access operation is logged and the Xtrace fields can be found.

    1. Log on to the Simple Log Service console.

    2. In the Projects area, select the Simple Log Service project that corresponds to the ACK cluster (the default project is k8s-log-<your-cluster-ID>) and then click the Logstores tab.

    3. Click the Logstore whose name starts with alb_ (alb_test_xz92lvykqj1siwvif**** in this example) and search for coffee.

      image

      Check whether the following Xtrace fields can be found:

      • slb_xtrace: 19241ed1c4790d31 in slb_xtrace: 19241ed1c4790d31:19241ed1c4790d31:-:1 is the trace ID of the request. The trace ID can be used in tracing analysis as a query condition.

      • xtrace_type: xtrace_type: zipkin indicates the sampling algorithm. For more information, see Step 2: Configure access logs and tracing analysis.

  4. Use the TraceId to query trace information in Managed Service for OpenTelemetry.

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

    2. On the Applications page, select the target region at the top, and then click the target application name. By default, the application name is the ID of the corresponding ALB instance (for example, alb-u53i28ewt580*****).

    3. In the left-side navigation pane, click Trace Analysis. In the search box on the right, enter the query condition trace_id IN (19241ed1c4790d31). The TraceId 19241ed1c4790d31 is from the log in the previous step.

      image

    4. Click Search to find the trace and view the trace details generated by the request.

      image

References