This topic describes how to use Alibaba Cloud Log Service to collect the standard output and log files of containers in Serverless Kubernetes clusters.

Prerequisites

A Serverless Kubernetes cluster is created. For more information, see Create a serverless Kubernetes cluster.

Use a YAML template to configure log collection

  1. Log on to the Container Service console.
  2. In the left-side navigation pane under Container Service-Kubernetes, choose Applications > Deployments. Then, click Create by Image in the upper-right corner.
  3. Configure log collection by specifying environment variables in the env field. To collect log files, you also need to specify the volumeMounts and volumes fields. The following example shows how to configure log collection for a deployment:
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      labels:
        app: alpine
      name: alpine
    spec:
      replicas: 2
      selector:
        matchLabels:
          app: alpine
      template:
        metadata:
          labels:
            app: alpine
        spec:
          containers:
          - image: alpine
            imagePullPolicy: Always
            args:
            - ping
            - 127.0.0.1
            name: alpine
            env:
            ######### Specify environment variables ###########
            - name: aliyun_logs_test-stdout
              value: stdout
            - name: aliyun_logs_test-file
              value: /log/*.log
            #################################
            ######### Configure volume mounting #######
            volumeMounts:
            - name: volume-sls
              mountPath: /log
          volumes:
          - name: volumn-sls
            emptyDir: {}
          ###############################

    Specify the following configurations in order as required:

    • Configure log collection by specifying environment variables. Ensure that all environment variables related to log collection are prefixed with aliyun_logs_. Specify an environment variable in the following format:
      - name: aliyun_logs_{Logstore name}
        value: {Log path}

      In the preceding example, two environment variables are specified. The aliyun_logs_test-stdout environment variable instructs the system to create a Logstore named test-stdout for collecting the standard output of the container.

      Note A Logstore name cannot contain underscores (_). You can use hyphens (-) instead.
    • If you specify a path for collecting log files apart from the standard output, you need to add the volumeMounts field.

      In the preceding example, the volumeMounts field is added and its value is /log, indicating that the /log/*.log files are collected.

  4. After entering the YAML code, click Create to deliver the configurations to the Serverless Kubernetes cluster.
    After the deployment is complete, you can run the corresponding command to view the pod status.
    NAME                      READY     STATUS    RESTARTS   AGE       IP             NODE
    alpine-76d978dbdd-gznk6   1/1       Running   0          21m       10.1.XX.XX   viking-c619c41329e624975a7bb50527180****
    alpine-76d978dbdd-vb9fv   1/1       Running   0          21m       10.1.XX.XX   viking-c619c41329e624975a7bb50527180****

View logs

  1. After you complete the installation, log on the Log Service console.
  2. In the console, select the project (k8s-log-{Kubernetes cluster ID} by default) corresponding to the Kubernetes cluster.
  3. In the Logstore list, find the Logstore specified in the log collection configuration, click 花, and select Search & Analysis from the drop-down list.
    In this example, find the test-stdout Logstore, click 花, and select Search & Analysis from the drop-down list. You can check the collected standard output of Elastic Container Instance (ECI).