kubernetes 日志采集方案 loongcollector(旧名iLogtail)


在这里插入图片描述

日志采集使用阿里云开源工具iLogtail,支持部署于物理机,虚拟机,Kubernetes等多种环境中来采集遥测数据,例如logs、traces和metrics。

优势

  • 支持多种Logs、Traces、Metrics数据采集,尤其对容器、Kubernetes环境支持非常友好

  • 数据采集资源消耗极低,相比同类遥测数据采集的Agent性能好5-20倍

  • 高稳定性,在阿里巴巴以及数万阿里云客户生产中使用验证,部署量近千万,每天采集数十PB可观测数据

  • 支持插件化扩展,可任意扩充数据采集、处理、聚合、发送模块

  • 支持配置远程管理,支持以图形化、SDK、K8s Operator等方式进行配置管理,可轻松管理百万台机器的数据采集

  • 支持自监控、流量控制、资源控制、主动告警、采集统计等多种高级特性

日志采集配置

logtail 服务部署

github 项目地址:https://github.com/alibaba/loongcollector/tree/main/example_config/start_with_k8s

创建namespace
kubectl apply -f https://raw.githubusercontent.com/alibaba/loongcollector/refs/heads/main/example_config/start_with_k8s/loongcollector-ns.yaml

创建采集配置, 保存到configmap
kubectl apply -f  https://raw.githubusercontent.com/alibaba/loongcollector/refs/heads/main/example_config/start_with_k8s/loongcollector-user-configmap.yaml

daemonset 方式部署ilogtail
kubectl apply -f  https://raw.githubusercontent.com/alibaba/loongcollector/refs/heads/main/example_config/start_with_k8s/loongcollector-daemonset.yaml

ilogtail ingress 日志采集

apiVersion: v1
kind: ConfigMap
metadata:
  name: loongcollector-user-cm
  namespace: loongcollector
data:
  nginx_stdout.yaml: |
    enable: true
    inputs:
      - Type: service_docker_stdout
        Stderr: true
        Stdout: true
        IncludeK8sLabel:
          app.kubernetes.io/name: ingress-nginx
    processors:
      - Type: processor_regex
        SourceKey: content
        KeepSourceIfParseError: true
        KeepSource: true
        Regex: '([\d\.:]+) - (\S+) \[(\S+) \S+\] \"(\S+) (\S+) ([^\\"]+)\" (\d+) (\d+) \"([^\\"]*)\" \"([^\\"]*)\" (\d+) (\S+) \[(\S+)\] \[(\S*)\] (\S+) (\S+) (\S+) (\S+) (\S+)'
        Keys:
          - remote_addr
          - remote_user
          - time_local
          - method
          - url
          - protocol
          - status
          - body_bytes_sent
          - http_referer
          - http_user_agent
          - request_length
          - request_time
          - proxy_upstream_name
          - proxy_alternative_upstream_name
          - upstream_addr
          - upstream_response_length
          - upstream_response_time
          - upstream_status
          - req_id
    flushers:
      - Type: flusher_elasticsearch
        Addresses:
          - http://192.168.1.1:9200
        Index: ingress-nginx_%{+yyyyMMdd}
        Authentication:
          PlainText:
            Username: elastic
            Password: xxxx
          TLS:
            Enabled: false

Ilogtail 业务日志采集

日志需要统一格式,格式如下:

[2025-02-14 11:17:45.545545] [DEBUG] [client.go:49] xxxxxxxxx

  • time: 日期格式,精确到微妙,但由于处理问题,在日志查询中只能精确到微妙。

  • log_level: 日志等级,方便日志过滤。

  • file_and_line:日志文件及行数,方便日志过滤。

  • data: 日志剩余信息。

  qa-log.yaml: |
    enable: true
    inputs:
      - Type: input_file
        FilePaths:
          - /app/logs/*
        EnableContainerDiscovery: true
        ContainerFilters:
          K8sNamespaceRegex: vision
    processors:
      - Type: processor_regex
        SourceKey: content
        KeepSource: true
        #FullMatch: false
        Regex: '\[(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{3})\d{3}\]\s+\[(\S+)\]\s+\[(\S+)\]\s+(.*)'
        Keys:
          - time
          - log_level
          - file_and_line
          - data
    flushers:
      - Type: flusher_elasticsearch
        Addresses:
          - http://192.168.1.1:9200
        Index: qa-log-%{tag.k8s.namespace.name}-%{tag.container.name}_%{+yyyyMMdd}
        Authentication:
          PlainText:
            Username: elastic
            Password: xxxx
          TLS:
            Enabled: false

ElasticSearch 环境配置

创建index templates

模版指定了如下参数:

  • index的副本为0,当前后端es集群为单点,所以没有必要配置副本。

  • numeric_detection,自动检测数字类型字段。

  • dynamic_date_formats,自定义格式解析日志字符串。

  • dynamic 新字段自动添加映射。

  • aliases 定义统一别名,方便汇聚查询

  • allow_auto_create 索引的自动创建

{
  "index_templates": [
    {
      "name": "qa-log",
      "index_template": {
        "index_patterns": [
          "qa-log*"
        ],
        "template": {
          "settings": {
            "index": {
              "number_of_replicas": "0"
            }
          },
          "mappings": {
            "numeric_detection": true,
            "dynamic_date_formats": [
              "yyyy-MM-dd HH:mm:ss.SSS"
            ],
            "dynamic": true,
            "dynamic_templates": []
          },
          "aliases": {
            "qa-log": {}
          }
        },
        "composed_of": [],
        "allow_auto_create": true,
        "ignore_missing_component_templates": []
      }
    }
  ]
}

索引生命周期管理

创建如下配置的ilm策略,对于qa-log模版创建的索引保留时间为15天。

{
  "qa-log": {
    "version": 4,
    "modified_date": "2025-02-17T12:41:20.148Z",
    "policy": {
      "phases": {
        "hot": {
          "min_age": "0ms",
          "actions": {
            "set_priority": {
              "priority": 100
            }
          }
        },
        "delete": {
          "min_age": "15d",
          "actions": {
            "delete": {
              "delete_searchable_snapshot": true
            }
          }
        }
      }
    },
      "data_streams": [],
      "composable_templates": [
        "qa-log"
      ]
    }
  }
}

Kibana查询索引创建

这步不用我教了吧。 🤣

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值