使用Loki收集监控linux日志

发布时间 2023-06-05 16:36:58作者: me小怪兽

简介

Loki 是受Prometheus启发的水平可扩展、高可用、多租户日志聚合系统。它的设计非常具有成本效益且易于操作。它不索引日志的内容,而是索引每个日志流的一组标签。

系统架构

 

1、Promtail收集并将日志发送给Loki的 Distributor 组件

2、Distributor会对接收到的日志流进行正确性校验,并将验证后的日志分批并行发送到Ingester

3、Ingester 接受日志流并构建数据块,压缩后存放到所连接的存储后端

4、Querier 收到HTTP查询请求,并将请求发送至Ingester 用以获取内存数据 ,Ingester 收到请求后返回符合条件的数据

5、如果 Ingester 没有返回数据,Querier 会从后端存储加载数据并遍历去重执行查询 ,通过HTTP返回查询结果。

特点

  • 用于索引日志的高效内存使用

    通过对一组标签进行索引,索引可以明显减小 比其他日志聚合产品。 更少的内存使其运行成本更低。

  • 多租户

    Loki 允许多个租户使用单个 Loki 实例。 不同租户的数据与其他租户完全隔离。 通过在代理中分配租户 ID 来配置多租户。

  • LogQL,洛基的查询语言

    Prometheus 查询语言 PromQL 的用户会发现 LogQL 很熟悉。 并灵活地针对日志生成查询。 该语言还有助于从日志数据生成指标, 远远超出日志聚合的强大功能。

  • 可扩展性

    Loki可以作为单个二进制文件运行; 所有组件都在一个进程中运行。

    Loki专为可扩展性而设计, 因为 Loki 的每个组件都可以作为微服务运行。 配置允许单独缩放微服务, 允许灵活的大规模安装。

  • 灵活性

    许多代理(客户端)都有插件支持。 这允许当前的可观测性结构 将 Loki 添加为他们的日志聚合工具,而无需 以切换可观测性堆栈的现有部分。

  • 格拉法纳集成

    Loki与Grafana无缝集成, 提供完整的可观测性堆栈。

对比其他日志系统

与其他日志聚合系统相比,Loki:

1、不对日志进行全文索引。通过存储压缩的非结构化日志和仅索引元数据,Loki 更易于操作且运行成本更低。 2、使用您已经在 Prometheus 中使用的相同标签对日志流进行索引和分组,使您能够使用您已经在 Prometheus 中使用的相同标签在指标和日志之间无缝切换。 3、尤其适合存储Kubernetes Pod 日志。Pod 标签等元数据会被自动抓取和索引。 4、在 Grafana 中有本机支持(需要 Grafana v6.0)。

组件

基于 Loki 的日志系统由 3 个组件组成:

1、promtail 是代理,负责收集日志并发送给 Loki。 2、loki 是主服务器,负责存储日志和处理查询。 3、Grafana用于查询和显示日志。

Loki 就像 Prometheus,但对于日志:我们更喜欢基于多维标签的索引方法,并且想要一个单二进制、易于操作且没有依赖关系的系统。Loki 与 Prometheus 的不同之处在于专注于日志而不是指标,并且通过推送而不是拉来交付日志。

系统环境

1、loki-server 安装在172.16.1.12

2、grafana-server 安装在172.16.1.12

3、promtail 分别安装在172.16.1.2-10

安装Loki

使用Loki流程

为了运行 Loki,您必须:

  • 下载并安装 Loki 和 Promtail。

  • 下载两个程序的配置文件。

  • 启动Loki。

  • 更新 Promtail 配置文件以将您的日志输入 Loki。

  • 启动Promtail。

安装方法

安装 Loki 和 Promtail 的不同方法的说明。

  • 使用 Tanka 安装(推荐)

  • 通过 Helm 安装

  • 通过 Docker 或 Docker Compose 安装

  • 在本地安装并运行

  • 从源安装

在本地安装并运行

1、创建目录

# mkdir /usr/local/loki
# mkdir /usr/local/promtail

2、下载安装包及配置文件

# cd /usr/local/loki
# wget https://github.com/grafana/loki/releases/download/v2.8.2/loki-linux-amd64.zip
# wget https://raw.githubusercontent.com/grafana/loki/main/cmd/loki/loki-local-config.yaml
# unzip loki-linux-amd64.zip
# cd /usr/local/promtail
# wget https://github.com/grafana/loki/releases/download/v2.8.2/promtail-linux-amd64.zip
# wget https://raw.githubusercontent.com/grafana/loki/main/clients/cmd/promtail/promtail-local-config.yaml
# unzip promtail-linux-amd64.zip

3、修改配置文件

# vim /usr/local/loki/loki-local-config.yaml 
auth_enabled: false

server:
http_listen_port: 3100 #端口
grpc_listen_port: 9096

common:
instance_addr: 172.16.1.12 #本地地址
path_prefix: /usr/local/loki #安装位置
storage:
  filesystem:
    chunks_directory: /usr/local/loki/chunks #安装位置
    rules_directory: /usr/local/loki/rules   #安装位置
replication_factor: 1
ring:
  kvstore:
    store: inmemory

query_range:
results_cache:
  cache:
    embedded_cache:
      enabled: true
      max_size_mb: 100

schema_config:
configs:
  - from: 2020-10-24
    store: boltdb-shipper
    object_store: filesystem
    schema: v11
    index:
      prefix: index_
      period: 24h

ruler:
#alertmanager_url: http://172.16.1.12:9093
alertmanager_url: http://172.16.1.12:20016     #alertmanager地址,做告警时会用到

# By default, Loki will send anonymous, but uniquely-identifiable usage and configuration
# analytics to Grafana Labs. These statistics are sent to https://stats.grafana.org/
#
# Statistics help us better understand how Loki is used, and they show us performance
# levels for most users. This helps us prioritize features and documentation.
# For more information on what's sent, look at
# https://github.com/grafana/loki/blob/main/pkg/usagestats/stats.go
# Refer to the buildReport method to see what goes into a report.
#
# If you would like to disable reporting, uncomment the following lines:
#analytics:
# reporting_enabled: false

 

# vim /usr/local/promtail/promtail-local-config.yaml 
server:
http_listen_port: 9080 #端口
grpc_listen_port: 0

positions:
filename: /tmp/positions.yaml #缓存文件,默认即可

clients:
- url: http://172.16.1.12:3100/loki/api/v1/push   #loki地址

scrape_configs:
- job_name: system
static_configs:
- targets:
    - localhost
  labels:
    job: varlogs #自定义标签,搜索日志时引用标签进行过滤
    #__path__: /var/log/*log
    __path__: /var/log/{messages*,secure*} #日志的位置,支持正则表达式

现在让我们稍微扩展一下示例:一台主机添加多个日志(根据需求进行更改)


scrape_configs:
- job_name: system
  pipeline_stages:
  static_configs:
  - targets:
    - localhost
    labels:
    job: syslog
    __path__: /var/log/syslog
     
- job_name: apache
  pipeline_stages:
  static_configs:
  - targets:
    - localhost
    labels:
    job: apache
    __path__: /var/log/apache.log

4、启动服务

# vim /usr/lib/systemd/system/loki.service
[Unit]
Description=loki
After=network.target

[Service]
ExecStart=/usr/local/loki/loki-linux-amd64 -config.file=/usr/local/loki/loki-local-config.yaml
ExecStop=/bin/kill -KILL $MAINPID
ExecReload=/bin/kill -HUP $MAINPID
KillMode=control-group
Restart=on-failure
RestartSec=3s

[Install]
WantedBy=multi-user.target


# systemctl daemon-reload   #加载
# systemctl restart loki.service   #启动
# systemctl enable loki.service   #开机自启动
# systemctl status loki.service   #查看服务状态

http://172.16.1.12:3100/metrics 上运行并显示 Loki 日志。

 

 

# vim /usr/lib/systemd/system/promtail.service
[Unit]
Description=promtail
After=network.target

[Service]
ExecStart=/usr/local/promtail/promtail-linux-amd64 -config.file=/usr/local/promtail/promtail-local-config.yaml
ExecStop=/bin/kill -KILL $MAINPID
ExecReload=/bin/kill -HUP $MAINPID
KillMode=control-group
Restart=on-failure
RestartSec=3s

[Install]
WantedBy=multi-user.target


# systemctl daemon-reload   #加载
# systemctl restart promtail.service   #启动
# systemctl enable promtail.service   #开机自启动
# systemctl status promtail.service   #查看服务状态

访问 http://172.16.1.12:9080/targets

 

安装grafana

Red Hat, CentOS, RHEL安装grafana
# yum install -y https://dl.grafana.com/oss/release/grafana-8.5.25-1.x86_64.rpm
# systemctl restart grafana
# systemctl enable
# systemctl status grafana

访问grafana

http://172.16.1.12:3000 账号密码由运维组进行开通

 

 

 

 

 

配置loki数据源

 

 

 

查询日志

 

 

常用的一些查询函数和方法

Log Stream Selector 大范围区间匹配
{app="mysql",name="mysql-backup"}

=: exactly equal
!=: not equal
=~: regex matches
!~: regex does not match
实例
{name =~ "mysql.+"}
{name !~ "mysql.+"}
{name !~ `mysql-\d+`}


Log Pipeline 日志详细的一些搜索条件
|=: Log line contains string
!=: Log line does not contain string
|~: Log line contains a match to the regular expression
!~: Log line does not contain a match to the regular expression
实例
{job="mysql"} |= "error" #过滤error关键字

安装172.16.1.2-10promtail

添加工具

# vim /etc/ansible/promtail.yml
---
- hosts: server
become: yes
become_user: root
become_method: sudo
tasks:
  - name: 拷贝安装包
    copy:
      src: /usr/local/promtail
      dest: /usr/local
  - name: 授权promtail-linux-amd64
    file:
      path: /usr/local/promtail/promtail-linux-amd64
      mode: '0755'
  - name: update job
    replace:
      path: /usr/local/promtail/promtail-local-config.yaml
      regexp: varlogs
      replace: "{{ansible_hostname}}"
  - name: 添加服务配置文件
    copy:
      src: /usr/lib/systemd/system/promtail.service
      dest: /usr/lib/systemd/system
  - name: 启动服务,配置开机自启动
    service:
      name: promtail
      state: restarted
      enabled: yes

进行安装

# ansible-playbook /etc/ansible/promtail.yml

查看es-node5安全日志

 

 

优化

根据实际需求添加新的日志监控