All Products
Search
Document Center

Container Service for Kubernetes:Use AlbConfigs to enable Tracing Analysis based on Xtrace

最終更新日:Nov 19, 2024

Alibaba Cloud provides the Tracing Analysis service that offers developers of distributed applications various features, such as trace mapping, request statistics, and trace topology. The Tracing Analysis 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 Application Load Balancer (ALB) Ingress controller and enable the Xtrace feature in a cluster. After the Xtrace feature is enabled, you can view the tracing data.

Prerequisites

Enable Tracing Analysis for an AlbConfig

Step 1: Enable Logtail for the cluster

After you enable Simple Log Service, the cluster automatically creates a Simple Log Service project. For more information, see Step 1: Install Logtail.

Step 2: Obtain the ID of the Simple Log Service project for subsequent operations

  1. Log on to the ACK console. In the left-side navigation pane, click Clusters.

  2. On the Clusters page, find the cluster that you want to manage and click its name. In the left-side pane, click Cluster Information.

  3. On the Cluster Information page, click the Basic Information tab and copy the ID next to the Log Service Project parameter.

Step 3: Create an AlbConfig

Use an AlbConfig to create an ALB instance and listeners. Enable Simple Log Service for the ALB instance to collect access logs. Configure the parameters related to the Xtrace feature in the AlbConfig. Enable the Xtrace feature to set the sampling algorithm and sampling rate.

  1. Copy the following content to the alb-test.yaml file. The file is used to create an AlbConfig.

    apiVersion: alibabacloud.com/v1
    kind: AlbConfig
    metadata:
      name: alb-demo
    spec:
      config:
        name: alb-test
        addressType: Intranet
        zoneMappings: # The specified vSwitch must be in a zone supported by the ALB instance and in the same virtual private cloud (VPC) as the cluster. To ensure the high availability of services, select at least two vSwitches in different zones. 
        -vSwitchId: vsw-2vc82nndnoo********** # Specify the IDs of the vSwitches that are used by the ALB instance. 
        - vSwitchId: vsw-2vc30f5mlhs**********
        accessLogConfig:
          logProject: "k8s-log-xz92lvykqj1siwvif****" # Specify the ID of the Simple Log Service project that you obtained. 
          logStore: The name of the alb_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: # The parameters related to the Xtrace feature. 
            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.

    Note
    • For more information about how to create a vSwitch, see Create and manage a vSwitch.

    • You can modify the Xtrace configurations only if you enable the access log feature of Simple Log Service for the ALB instance.

  2. Run the following command to create an AlbConfig:

    kubectl apply -f alb-test.yaml

    Expected output:

    albconfig.alibabacloud.com/alb-demo created

Step 4: Create an IngressClass

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

    kubectl apply -f alb.yaml

    Expected output:

    ingressclass.networking.k8s.io/alb created

Step 5: Create a Service and configure forwarding rules for the Ingress

  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: 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. 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:
          # Specify 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

Step 6: Configure domain name resolution

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 Service through the custom domain name. For more information, see (Optional) Step 5: Configure domain name resolution.

Step 7: Access the Service

When you access the Service, the generated request contains an Xtrace ID.

  1. Run the following command to obtain the address of the ALB instance (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. Run the following command to access the Service:

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

    The expected output is an HTML page with the title Hello World.

Verify Tracing Analysis

  1. Check requests sent to the ALB instance in Simple Log Service to confirm that the Xtrace data is marked.

    1. Log on to the ACK console. In the left-side navigation pane, click Clusters.

    2. On the Clusters page, find the cluster that you want to manage and click its name. In the left-side pane, click Cluster Information.

    3. On the Cluster Information page, click the Basic Information tab, and then click the link next to Log Service Project to go to the Simple Log Service console. In the left-side navigation pane, click the Logstore whose name starts with alb_, such as alb_xz92lvykqj1siwvif**** in this example. Then, search for xtrace on the current page.

      image

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

  3. On the Applications page, select a region in the top navigation bar and click the name of the application.

  4. In the left-side navigation pane, click Interface Calls, and then click the Traces tab to view the tracing data.

    On the Traces tab, you can view up to 100 traces with the longest duration for the application. For more information about the tracing data, see API calls.

References