日志监控Grafana+Loki+Promtail

75 阅读2分钟

日志监控是任何系统管理工作中至关重要的一部分,可以帮助追踪系统行为、诊断问题和保障系统健康。

Grafana、Loki 和 Promtail 是一套强大的轻量级日志监控解决方案,结合了实时可视化、高效的日志收集和存储。

本文将介绍如何在 CentOS 上离线搭建 Grafana、Loki 和 Promtail 的日志监控系统。

各组件介绍

  • Grafana:一个流行的开源可视化工具,用于创建仪表盘和图表,方便展示各种数据源的信息。
  • Loki:由 Grafana 实验室开发的日志聚合系统,专注于处理大规模日志流,将日志存储为可查询的可索引数据。
  • Promtail:Grafana 实验室开发的轻量级日志收集代理,用于实时收集、索引和发送日志到 Loki 中。

安装

在 CentOS 7.5 服务器离线安装,需要先在有网络的电脑下载好安装包,再传到服务器进行安装。

下载安装包

  1. Grafana

下载地址:dl.grafana.com/enterprise/…

获取最新版本的方式

方式一: 访问官方下载地址:grafana.com/grafana/dow…

方式二:通过 github(github.com/grafana/gra… 找到最新 tag,再通过官网下载地址(dl.grafana.com/enterprise/… )组装最新下载地址

如:image.png 版本号为 10.4.2,则最新下载地址为:dl.grafana.com/enterprise/…

  1. Loki

下载地址:github.com/grafana/lok…

获取最新版本github.com/grafana/lok…

  1. Promtail

下载地址:github.com/grafana/lok…

获取最新版本github.com/grafana/lok…

安装

Grafana

# 安装
yum install grafana-enterprise-10.4.2-1.x86_64.rpm

# 启动
systemctl start grafana-server

# 查看启动状态
systemctl status grafana-server

# 设置为开机自启动
systemctl enable grafana-server

访问 http://[你的Grafana服务器ip]:3000 验证是否可以正常访问,默认用户名密码:admin/admin

Loki

  1. 解压安装包
unzip loki-linux-amd64.zip /usr/local/bin
  1. 创建 Loki 配置
echo 'auth_enabled: false

server:
  http_listen_port: 3100

common:
  path_prefix: /tmp/loki
  storage:
    filesystem:
      chunks_directory: /tmp/loki/chunks
      rules_directory: /tmp/loki/rules
  replication_factor: 1
  ring:
    kvstore:
      store: inmemory

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

ruler:
  alertmanager_url: http://localhost:9093' > /usr/local/bin/config-loki.yml
  1. 配置 Loki 作为服务运行
# 添加脚本
echo '[Unit]
Description=Loki service
After=network.target

[Service]
Type=simple
User=root
ExecStart=/usr/local/bin/loki-linux-amd64 -config.file /usr/local/bin/config-loki.yml

[Install]
WantedBy=multi-user.target ' > /etc/systemd/system/loki.service

# 启动
systemctl start loki

# 查看启动状态
systemctl status loki

# 设置为开机自启动
systemctl enable loki

Promtail

  1. 解压安装包
promtail-linux-amd64.zip
  1. 创建 Promtail 配置
echo 'server:
  http_listen_port: 9080
  grpc_listen_port: 0

positions:
  filename: /tmp/positions.yaml

clients:
  - url: http://[你的Loki服务器ip]:3100/loki/api/v1/push

scrape_configs:
- job_name: system
  static_configs:
  - targets:
      - localhost
    labels:
      job: varlogs
      # 监听的本地路径
      __path__: /var/log/*log' > /usr/local/bin/config-promtail.yml
  1. 配置 Promtail 作为服务运行
# 添加脚本
echo '[Unit]
Description=Promtail service
After=network.target

[Service]
Type=simple
User=root
ExecStart=/usr/local/bin/promtail-linux-amd64 -config.file /usr/local/bin/config-promtail.yml

[Install]
WantedBy=multi-user.target ' > /etc/systemd/system/promtail.service

# 启动
systemctl start promtail

# 查看启动状态
systemctl status promtail

# 设置为开机自启动
systemctl enable promtail

参考:

  1. 安装步骤参考:sbcode.net/grafana/ins…
  2. 安装包下载地址:github.com/grafana/lok…