Kubernetes中Logtail日志采集CRD配置详解

本文涉及的产品
对象存储 OSS,20GB 3个月
对象存储 OSS,恶意文件检测 1000次 1年
对象存储 OSS,内容安全 1000次 1年
简介: 日志采集配置默认支持控制台配置方式,同时针对Kubernetes微服务开发模式,我们还提供CRD的配置方式,您可以直接使用kubectl对配置进行管理。

日志采集配置默认支持控制台配置方式,同时针对Kubernetes微服务开发模式,我们还提供CRD的配置方式,您可以直接使用kubectl对配置进行管理。

我们推荐使用CRD方式进行采集配置管理,该方式与Kubernetes部署、发布流程的集成更加完善。

实现原理

dcbf28a3-6a60-4b22-9de6-670cca52db91.png



执行安装命令时会自动安装alibaba-log-controller的Helm包。Helm包中主要执行了以下操作:

1创建aliyunlogconfigs CRD(CustomResourceDefinition)。
2部署alibaba-log-controller的Deployment。
3部署Logtail的DaemonSet。

具体配置的内部工作流程如下:

1用户使用kubectl或其他工具应用aliyunlogconfigs CRD配置。
2alibaba-log-controller监听到配置更新。
3alibaba-log-controller根据CRD内容以及服务端状态,自动向日志服务提交logstore创建、配置创建以及应用机器组的请求。
4以DaemonSet模式运行的Logtail会定期请求配置服务器,获取新的或已更新的配置并进行热加载。
5Logtail根据配置信息采集各个容器(POD)上的标准输出或文件。
6最终Logtail将处理、聚合好的数据发送到日志服务。

使用方式

配置方式

注意: 如果您之前使用的DaemonSet方式部署的日志服务Logtail,将无法使用CRD的方式进行配置管理。升级方式参见Kubernets日志采集部署中DaemonSet部署方式迁移步骤。

您只需要定义AliyunLogConfig的CRD即可实现配置的创建,删除对应的CRD资源即可删除对应配置。CRD配置格式如下:

apiVersion: log.alibabacloud.com/v1alpha1      ## 默认值,无需改变
kind: AliyunLogConfig                          ## 默认值,无需改变
metadata:
  name: simple-stdout-example                  ## 资源名,必须在集群内唯一
spec:
  logstore: k8s-stdout                         ## logstore名,不存在时自动创建
  shardCount: 2                                ## [可选] logstore分区数,默认为2,支持1-10
  lifeCycle: 90                                ## [可选] 该logstore存储时间,默认为90,支持1-36500,3650天为永久存储
  logtailConfig:                               ## 详细配置
    inputType: plugin                          ## 采集的输入类型,一般为file或plugin
    configName: simple-stdout-example          ## 采集配置名,需要和资源名(metadata.name)一致
    inputDetail:                               ## 详细配置信息,具体请参考示例
    ...


配置完成并apply后,alibaba-log-controller会自动创建

查看配置

您可以通过Kubernetes CRD或控制台查看配置。

控制台查看配置参见查看Logtail配置列表

注意: 使用CRD管理方式,若您在控制台更改配置,下一次执行CRD更新配置时,会覆盖控制台的更改内容。
使用kubectl get aliyunlogconfigs查看当前所有配置

[root@iZbp1dsbiaZ ~]# kubectl get aliyunlogconfigs
NAME                   AGE
regex-file-example     10s
regex-stdout-example   4h
simple-file-example    5s


使用kubectl get aliyunlogconfigs ${config_name} -o yaml查看详细配置和状态。

配置中status字段显示配置执行的结果,若配置应用成功,status字段中的statusCode为200。若statusCode非200,则说明配置应用失败。

[root@iZbp1dsbiaZ ~]# kubectl get aliyunlogconfigs simple-file-example -o yaml
apiVersion: log.alibabacloud.com/v1alpha1
kind: AliyunLogConfig
metadata:
  annotations:
    kubectl.kubernetes.io/last-applied-configuration: |
      {"apiVersion":"log.alibabacloud.com/v1alpha1","kind":"AliyunLogConfig","metadata":{"annotations":{},"name":"simple-file-example","namespace":"default"},"spec":{"logstore":"k8s-file","logtailConfig":{"configName":"simple-file-example","inputDetail":{"dockerFile":true,"dockerIncludeEnv":{"ALIYUN_LOGTAIL_USER_DEFINED_ID":""},"filePattern":"simple.LOG","logPath":"/usr/local/ilogtail","logType":"common_reg_log"},"inputType":"file"}}}
  clusterName: ""
  creationTimestamp: 2018-05-17T08:44:46Z
  generation: 0
  name: simple-file-example
  namespace: default
  resourceVersion: "21790443"
  selfLink: /apis/log.alibabacloud.com/v1alpha1/namespaces/default/aliyunlogconfigs/simple-file-example
  uid: 8d3a09c4-59ae-11e8-851d-00163f008685
spec:
  lifeCycle: null
  logstore: k8s-file
  logtailConfig:
    configName: simple-file-example
    inputDetail:
      dockerFile: true
      dockerIncludeEnv:
        ALIYUN_LOGTAIL_USER_DEFINED_ID: ""
      filePattern: simple.LOG
      logPath: /usr/local/ilogtail
      logType: common_reg_log
    inputType: file
  machineGroups: null
  project: ""
  shardCount: null
status:
  status: OK
  statusCode: 200


配置示例

容器标准输出

容器标准输出中,需要将inputType设置为plugin,并将具体信息填写到inputDetail下的plugin字段,详细配置字段极其含义请参考: 容器标准输出

极简采集方式

采集除了环境变量中配置COLLECT_STDOUT_FLAG=false之外所有容器的标准输出(stdout和stderr)。

apiVersion: log.alibabacloud.com/v1alpha1
kind: AliyunLogConfig
metadata:
  # your config name, must be unique in you k8s cluster
  name: simple-stdout-example
spec:
  # logstore name to upload log
  logstore: k8s-stdout
  # logtail config detail
  logtailConfig:
    # docker stdout's input type is 'plugin'
    inputType: plugin
    # logtail config name, should be same with [metadata.name]
    configName: simple-stdout-example
    inputDetail:
      plugin:
        inputs:
          -
            # input type
            type: service_docker_stdout
            detail:
              # collect stdout and stderr
              Stdout: true
              Stderr: true
              # collect all container's stdout except containers with "COLLECT_STDOUT_FLAG:false" in docker env config
              ExcludeEnv:
                COLLECT_STDOUT_FLAG: "false"


自定义处理采集方式

采集grafana的access log,并将access log解析成结构化数据。

grafana的容器配置中包含环境变量为:GF_INSTALL_PLUGINS=grafana-piechart-....,通过配置IncludeEnv为GF_INSTALL_PLUGINS: ''指定Logtail只采集该容器的标准输出。

1e95c4c0-4f7d-4c92-943a-e3badb6c81fa.png



grafana的access log格式如下:

t=2018-03-09T07:14:03+0000 lvl=info msg="Request Completed" logger=context userId=0 orgId=0 uname= method=GET path=/ status=302 remote_addr=172.16.64.154 time_ms=0 size=29 referer=


在这里我们使用正则表达式解析access log,具体配置如下:

apiVersion: log.alibabacloud.com/v1alpha1
kind: AliyunLogConfig
metadata:
  # your config name, must be unique in you k8s cluster
  name: regex-stdout-example
spec:
  # logstore name to upload log
  logstore: k8s-stdout-regex
  # logtail config detail
  logtailConfig:
    # docker stdout's input type is 'plugin'
    inputType: plugin
    # logtail config name, should be same with [metadata.name]
    configName: regex-stdout-example
    inputDetail:
      plugin:
        inputs:
          -
            # input type
            type: service_docker_stdout
            detail:
              # 只采集stdout,不采集stderr
              Stdout: true
              Stderr: false
              # 只采集容器环境变量中配置key为"GF_INSTALL_PLUGINS"的stdout 
              InluceEnv:
                GF_INSTALL_PLUGINS: ''
        processors:
          -
            # 使用正则表达式处理
            type: processor_regex
            detail:
        # docker 采集的数据默认key为"content"
              SourceKey: content
        # 正则表达式提取
              Regex: 't=(\d+-\d+-\w+:\d+:\d+\+\d+) lvl=(\w+) msg="([^"]+)" logger=(\w+) userId=(\w+) orgId=(\w+) uname=(\S*) method=(\w+) path=(\S+) status=(\d+) remote_addr=(\S+) time_ms=(\d+) size=(\d+) referer=(\S*).*'
              # 提取出的key
        Keys: ['time', 'level', 'message', 'logger', 'userId', 'orgId', 'uname', 'method', 'path', 'status', 'remote_addr', 'time_ms', 'size', 'referer']
              # 保留原始字段
              KeepSource: true
              NoKeyError: true
              NoMatchError: true


配置应用后,采集到日志服务的数据如下:

a0f79bbb-f87d-4735-92fd-4ab8e5d31fc6.png



容器文件

极简文件

采集环境变量配置中含有key为ALIYUN_LOGTAIL_USER_DEFINED_ID的容器内日志文件,文件所处的路径为/data/logs/app_1,文件名为simple.LOG。

apiVersion: log.alibabacloud.com/v1alpha1
kind: AliyunLogConfig
metadata:
  # your config name, must be unique in you k8s cluster
  name: simple-file-example
spec:
  # logstore name to upload log
  logstore: k8s-file
  # logtail config detail
  logtailConfig:
    # log file's input type is 'file'
    inputType: file
    # logtail config name, should be same with [metadata.name]
    configName: simple-file-example
    inputDetail:
      # 极简模式日志,logType设置为"common_reg_log"
      logType: common_reg_log
      # 日志文件夹
      logPath: /data/logs/app_1
      # 文件名, 支持通配符,例如log_*.log
      filePattern: simple.LOG
      # 采集容器内的文件,dockerFile flag设置为true
      dockerFile: true
      # only collect container with "ALIYUN_LOGTAIL_USER_DEFINED_ID" in docker env config
      dockerIncludeEnv:
        ALIYUN_LOGTAIL_USER_DEFINED_ID: ""


完全正则模式文件

对于某Java程序日志样例为:

[2018-05-11T20:10:16,000] [INFO] [SessionTracker] [SessionTrackerImpl.java:148] Expiring sessions
java.sql.SQLException: Incorrect string value: '\xF0\x9F\x8E\x8F",...' for column 'data' at row 1
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:84)
at org.springframework.jdbc.support.AbstractFallbackSQLException


日志中由于包含错误堆栈信息,可能一条日志会被分解成多行,因此需要设置行首正则表达式;为了提取出各个字段,这里我们使用正则表达式进行提取,具体配置如下:

apiVersion: log.alibabacloud.com/v1alpha1
kind: AliyunLogConfig
metadata:
  # your config name, must be unique in you k8s cluster
  name: regex-file-example
spec:
  # logstore name to upload log
  logstore: k8s-file
  logtailConfig:
    # log file's input type is 'file'
    inputType: file
    # logtail config name, should be same with [metadata.name]
    configName: regex-file-example
    inputDetail:
      # 对于正则类型的日志,将logType设置为common_reg_log
      logType: common_reg_log
      # 日志文件夹
      logPath: /app/logs
      # 文件名, 支持通配符,例如log_*.log
      filePattern: error.LOG
      # 行首正则表达式
      logBeginRegex: '\[\d+-\d+-\w+:\d+:\d+,\d+]\s\[\w+]\s.*'
      # 解析正则
      regex: '\[([^]]+)]\s\[(\w+)]\s\[(\w+)]\s\[([^:]+):(\d+)]\s(.*)'
      # 提取出的key列表
      key : ["time", "level", "method", "file", "line", "message"]
      # 正则模式日志,时间解析默认从日志中的`time`提取,如果无需提取时间,可不设置该字段
      timeFormat: '%Y-%m-%dT%H:%M:%S'
      # 采集容器内的文件,dockerFile flag设置为true
      dockerFile: true
      # only collect container with "ALIYUN_LOGTAIL_USER_DEFINED_ID" in docker env config
      dockerIncludeEnv:
        ALIYUN_LOGTAIL_USER_DEFINED_ID: ""


配置应用后,采集到日志服务的数据如下:

5669739e-7737-418a-a7df-8e8692bca05f.png



分隔符模式文件

Logtail同时支持分隔符模式的日志解析,示例如下:

apiVersion: log.alibabacloud.com/v1alpha1
kind: AliyunLogConfig
metadata:
  # your config name, must be unique in you k8s cluster
  name: delimiter-file-example
spec:
  # logstore name to upload log
  logstore: k8s-file
  logtailConfig:
    # log file's input type is 'file'
    inputType: file
    configName: delimiter-file-example
    # logtail config name, should be same with [metadata.name]
    inputDetail:
      # 对于分隔符类型的日志,logType设置为delimiter_log
      logType: delimiter_log
      # 日志文件夹
      logPath: /usr/local/ilogtail
      # 文件名, 支持通配符,例如log_*.log
      filePattern: delimiter_log.LOG
      # 使用多字符分隔符
      separator: '|&|'
      # 提取的key列表
      key : ['time', 'level', 'method', 'file', 'line', 'message']
      # 用作解析时间的key,如无需求可不填
      timeKey: 'time'
      # 时间解析方式,如无需求可不填
      timeFormat: '%Y-%m-%dT%H:%M:%S'
      # 采集容器内的文件,dockerFile flag设置为true
      dockerFile: true
      # only collect container with "ALIYUN_LOGTAIL_USER_DEFINED_ID" in docker env config
      dockerIncludeEnv:
        ALIYUN_LOGTAIL_USER_DEFINED_ID: ''


JSON模式文件

若文件中每行数据为一个JSON object,可以使用JSON方式进行解析,示例如下:

apiVersion: log.alibabacloud.com/v1alpha1
kind: AliyunLogConfig
metadata:
  # your config name, must be unique in you k8s cluster
  name: json-file-example
spec:
  # logstore name to upload log
  logstore: k8s-file
  logtailConfig:
    # log file's input type is 'file'
    inputType: file
    # logtail config name, should be same with [metadata.name]
    configName: json-file-example
    inputDetail:
      # 对于分隔符类型的日志,logType设置为json_log
      logType: json_log
      # 日志文件夹
      logPath: /usr/local/ilogtail
      # 文件名, 支持通配符,例如log_*.log
      filePattern: json_log.LOG
      # 用作解析时间的key,如无需求可不填
      timeKey: 'time'
      # 时间解析方式,如无需求可不填
      timeFormat: '%Y-%m-%dT%H:%M:%S'
      # 采集容器内的文件,dockerFile flag设置为true
      dockerFile: true
      # only collect container with "ALIYUN_LOGTAIL_USER_DEFINED_ID" in docker env config
      dockerIncludeEnv:
        ALIYUN_LOGTAIL_USER_DEFINED_ID: ""
相关实践学习
容器服务Serverless版ACK Serverless 快速入门:在线魔方应用部署和监控
通过本实验,您将了解到容器服务Serverless版ACK Serverless 的基本产品能力,即可以实现快速部署一个在线魔方应用,并借助阿里云容器服务成熟的产品生态,实现在线应用的企业级监控,提升应用稳定性。
云原生实践公开课
课程大纲 开篇:如何学习并实践云原生技术 基础篇: 5 步上手 Kubernetes 进阶篇:生产环境下的 K8s 实践 相关的阿里云产品:容器服务 ACK 容器服务 Kubernetes 版(简称 ACK)提供高性能可伸缩的容器应用管理能力,支持企业级容器化应用的全生命周期管理。整合阿里云虚拟化、存储、网络和安全能力,打造云端最佳容器化应用运行环境。 了解产品详情: https://www.aliyun.com/product/kubernetes
目录
相关文章
|
1月前
|
Kubernetes Perl 容器
K8s查看集群 状态事件描述以及Pod日志信息
K8s查看集群 状态事件描述以及Pod日志信息
53 3
|
2月前
|
监控 Serverless 数据库
Serverless 应用引擎常见问题之biphon-education-配置了SLS后一直重启如何解决
Serverless 应用引擎(Serverless Application Engine, SAE)是一种完全托管的应用平台,它允许开发者无需管理服务器即可构建和部署应用。以下是Serverless 应用引擎使用过程中的一些常见问题及其答案的汇总:
28 5
|
2月前
|
缓存 Kubernetes Docker
容器服务ACK常见问题之容器服务ACK ingress websocket配置失败如何解决
容器服务ACK(阿里云容器服务 Kubernetes 版)是阿里云提供的一种托管式Kubernetes服务,帮助用户轻松使用Kubernetes进行应用部署、管理和扩展。本汇总收集了容器服务ACK使用中的常见问题及答案,包括集群管理、应用部署、服务访问、网络配置、存储使用、安全保障等方面,旨在帮助用户快速解决使用过程中遇到的难题,提升容器管理和运维效率。
|
2月前
|
存储 JSON 监控
可以通过配置Filebeat来将Higress日志持久化到磁盘
【2月更文挑战第10天】可以通过配置Filebeat来将Higress日志持久化到磁盘
31 4
|
3月前
|
Java
flume的log4j.properties配置说明
flume的log4j.properties配置说明
|
3月前
|
存储 运维 Kubernetes
批处理及有状态等应用类型在 K8S 上应该如何配置?
批处理及有状态等应用类型在 K8S 上应该如何配置?
|
2月前
|
Prometheus 监控 Kubernetes
Kubernetes 集群监控与日志管理实践
【2月更文挑战第29天】 在微服务架构日益普及的当下,Kubernetes 已成为容器编排的事实标准。然而,随着集群规模的扩大和业务复杂度的提升,有效的监控和日志管理变得至关重要。本文将探讨构建高效 Kubernetes 集群监控系统的策略,以及实施日志聚合和分析的最佳实践。通过引入如 Prometheus 和 Fluentd 等开源工具,我们旨在为运维专家提供一套完整的解决方案,以保障系统的稳定性和可靠性。
|
5天前
|
存储 数据采集 Kubernetes
一文详解K8s环境下Job类日志采集方案
本文介绍了K8s中Job和Cronjob控制器用于非常驻容器编排的场景,以及Job容器的特点:增删频率高、生命周期短和突发并发大。文章重点讨论了Job日志采集的关键考虑点,包括容器发现速度、开始采集延时和弹性支持,并对比了5种采集方案:DaemonSet采集、Sidecar采集、ECI采集、同容器采集和独立存储采集。对于短生命周期Job,建议使用Sidecar或ECI采集,通过调整参数确保数据完整性。对于突发大量Job,需要关注服务端资源限制和采集容器的资源调整。文章总结了不同场景下的推荐采集方案,并指出iLogtail和SLS未来可能的优化方向。
|
4天前
|
存储 Kubernetes 数据安全/隐私保护
|
12天前
|
Prometheus 监控 Kubernetes
Kubernetes 集群的监控与日志管理策略
【4月更文挑战第30天】 在微服务架构日益普及的当下,容器化技术与编排工具如Kubernetes成为了运维领域的重要话题。有效的监控和日志管理对于保障系统的高可用性和故障快速定位至关重要。本文将探讨在Kubernetes环境中实施监控和日志管理的最佳实践,包括选用合适的工具、部署策略以及如何整合这些工具来提供端到端的可见性。我们将重点讨论Prometheus监控解决方案和EFK(Elasticsearch, Fluentd, Kibana)日志管理堆栈,分析其在Kubernetes集群中的应用,并给出优化建议。

相关产品

  • 日志服务