This topic describes how to configure a YAML file to export the logs of an Alibaba Cloud Container Service for Kubernetes (ACK) cluster to Alibaba Cloud Log Service, without modifying the application. The following procedures demonstrate how to configure Log4jAppender by deploying an Application Programming Interface (API)-based application on an ACK cluster.

Prerequisites

  • Alibaba Cloud Container Service for Kubernetes is activated and an ACK cluster is created.

    In this example, an ACK cluster is created in the China (Hangzhou) region.

  • An AccessKey pair is created or Resource Access Management (RAM) is activated. Make sure that the required access permissions are granted. In this example, An AccessKey pair is created.

Background information

Log4j is an open-source project of Apache. Log4j consists of three components: log level, log output destination, and log output format. You can configure Log4Appender to export log data to the console, a log file, a GUI (Graphical User Interface) component, a socket server, an NT event viewer, or a UNIX syslog daemon.

Procedure

  1. Configure Log4jAppender in Alibaba Cloud Log Service.
    1. Create a project in Log Service.
      In this example, a project named k8s-log4j is created in the China (Hangzhou) region where the ACK cluster is deployed. For more information, see Create a project.
      Note We recommend that you create a project in the region where the ACK cluster is deployed. When a Log Service project and an ACK cluster are deployed in the same region, log data is transmitted through the internal network. This saves the Internet bandwidth cost, reduces the time of cross-region transmission, and ensures fast log collection and retrieval.
      Create a project
    2. Create a Logstore for the k8s-log4j project.
      In this example, a Logstore named k8s-logstore is created. For more information, see Create a Logstore.
      Configure the Logstore
    3. After the k8s-logstore Logstore is created, a dialog box appears. It provides instructions about how to use the Data Import wizard.
      Configure the Data Import wizard
    4. Select log4jAppender under Custom Data, and then configure the settings by following the steps that are provided on the page.
      In this example, log4jAppender is configured with default settings. You can configure the settings based on your actual needs.
      Custom data
  2. Configure the k8s-log4j project in the ACK cluster.
    In this example, the demo-deployment and demo-Service files are used.
    1. Connect to your ACK cluster.
    2. Obtain the demo-deployment.yaml file and configure the environment variable JAVA_OPTS.
      The following is an example of the demo-deployment.yaml:
      apiVersion: apps/v1
      kind: Deployment
      metadata:
        name: log4j-appender-demo-spring-boot
        labels:
          app: log4j-appender
      spec:
        replicas: 1
        selector:
          matchLabels:
            app: log4j-appender
        template:
          metadata:
          labels:
            app: log4j-appender
        spec:
          containers:
          -name: log4j-appender-demo-spring-boot
            image: registry.cn-hangzhou.aliyuncs.com/jaegertracing/log4j-appender-demo-spring-boot:0.0.2
            env:
            - name: JAVA_OPTS                ## Note
              value: "-Dproject={your_project} -Dlogstore={your_logstore} -Dendpoint={your_endpoint} -Daccess_key_id={your_access_key_id} -Daccess_key={your_access_key_secret}"
            ports:
            - containerPort: 8080
      Note where:
      • -Dproject: the name of your Log Service project. In this example, the project name is k8s-log4j.
      • -DlogStore: the name of your Logstore in Log Service. In this example, the Logstore name is k8s-logstore.
      • -Dendpoint: the service endpoint of Log Service. You must configure the service endpoint based on the region where the Log Service project is deployed. For more information, see Service endpoint. In this example, the service endpoint is cn-hangzhou.log.aliyuncs.com.
      • -Daccess_key_id: your AccessKey ID.
      • -Daccess_key: your AccessKey secret.
    3. Run the following command to create a deployment:
      kubectl create -f demo-deployment.yaml
    4. Obtain the demo-service.yaml file and run the following command to create a service:
      You do not need to modify the configurations in the demo-service.yaml file.
      kubectl create -f demo-service.yaml
  3. Test to generate ACK cluster logs.
    You can run the kubectl get command to view the deployment status of resource objects. After the deployment and service are provisioned, run the kubectl get svc command to check the external IP address of the service, EXTERNAL-IP.
    $ kubectl get svc
    NAME                      TYPE           CLUSTER-IP    EXTERNAL-IP      PORT(S)          AGE
    log4j-appender-demo-spring-boot-svc LoadBalancer 172.21.XX.XX 120.55.XXX.XXX 8080:30398/TCP 1h
    In this example, you can run the login command to generate Kubernetes cluster logs. wherein, K8 S_SERVICE_IP is the EXTERNAL-IP.
    Note For a complete list of API operations, visit GitHub log4j-appender-demo.
    curl http://${K8S_SERVICE_IP}:8080/login? name=bruce
  4. View logs in Alibaba Cloud Log Service
    1. On the projects page, click the target project to go to the project details page.
    2. Click the Icon icon on the right side of the k8s-logstore Logstore, and then click Search & Analysis to view the logs of the Kubernetes cluster.