日志监控平台搭建(Loki+promtail+grafana)
阅读原文时间:2023年08月09日阅读:1

搭建Loki+promtail+grafana日志监控平台,可以直接在grafana的UI界面查看系统应用日志,使日志查看起来更方便、快捷。


   Loki:主服务器,负责存储日志和处理查询。 

Promtail:代理,负责收集日志并将其发送给Loki。

  Grafana:用于查询和显示日志。


1. 离线获取安装文件

  适用于内网服务器,无法访问互联网,获取安装文件后上传至服务器指定目录。

1.1  方式一:下载

Loki的GitHub地址:https://github.com/grafana/loki
配置文件官网地址:https://grafana.com/docs/loki/latest/installation/local/
Grafana下载官网:https://grafana.com/grafana/download

1.2 方式二:网盘获取

loki、promtail应用及配置文件网盘链接:https://pan.baidu.com/s/1VKw4u329nIsfKARGVa9nvg 提取码:yplj

2. 日志平台搭建

2.1 loki安装

2.1.1 下载文件

#创建目录
mkdir -p /opt/loki

#下载压缩文件
cd /opt/loki
curl -O -L https://github.com/grafana/loki/releases/download/v2.0.0/loki-linux-amd64.zip

#下载配置文件
wget --no-check-certificate https://raw.githubusercontent.com/grafana/loki/master/cmd/loki/loki-local-config.yaml

#解压文件
unzip loki-linux-amd64.zip

#执行文件授权
chmod a+x loki-linux-amd64

2.1.2 修改配置文件(loki-local-config.yaml)

  配置如下:

auth_enabled: false

server:
http_listen_port: 3100 #如是云服务需打开3100端口
grpc_listen_port: 9096

common:
path_prefix: /opt/loki
storage:
filesystem:
chunks_directory: /opt/loki/
chunks

rules_directory: /opt/loki/rules
replication_factor: 1
ring:
instance_addr: 127.0.0.1 #loki访问路径,可不做修改
kvstore:
store: inmemory

#query_range:
#results_cache:
#cache:
#embedded_cache: false
#enabled: false
#distributed: false
#max_size_mb: 100

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

limits_config:
enforce_metric_name: false
reject_old_samples: true # 是否拒绝老样本
reject_old_samples_max_age: 336h # 336小时之前的样本将会被删除
ingestion_rate_mb: 32 # 每秒允许promtail传输32MB,默认为4
ingestion_burst_size_mb: 64
per_stream_rate_limit: 1000MB
max_entries_limit_per_query: 10000

chunk_store_config:
max_look_back_period: 336h # 为避免查询超过保留期的数据,必须小于或等于下方的时间值

table_manager:
retention_deletes_enabled: true # 保留删除开启
retention_period: 336h # 超过336h的块数据将被删除

ruler:
alertmanager_url: http://localhost:9093

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

2.1.3 启动loki服务

# 启动Loki命令 默认端口为 3100
nohup ./loki-linux-amd64 -config.file=loki-local-config.yaml > loki.log 2>&1 &

2.2 promtail安装

2.2.1 下载文件

#创建目录
mkdir -p /opt/promtail

#下载压缩文件
cd /opt/promtail
curl -O -L https://github.com/grafana/loki/releases/download/v2.0.0/promtail-linux-amd64.zip

#下载配置文件
wget --no-check-certificate https://raw.githubusercontent.com/grafana/loki/master/clients/cmd/promtail/promtail-local-config.yaml

#解压文件
unzip promtail-linux-amd64.zip

#执行文件授权
chmod a+x promtail-linux-amd64

2.2.2 修改配置文件(promtail-local-config.yaml)

修改 clients, 将host改为loki服务器地址 ; 添加 scrape_configs 对应的 labels 进行日志收集。

  配置如下,涉及到7个应用的日志查看:

server:
http_listen_port: 9080 #云服务器需开放9080端口
grpc_listen_port: 0

positions:
filename: /opt/promtail/positions.yaml #positions存放路径在promtail工具地址下

clients:

scrape_configs:

  • job_name: fap
    static_configs:

    • targets:
      • localhost
        labels:
        job: "HSJ_fap"
        __path__: /opt/cwy8.32/product/module/P/FAP/nohup.out
  • job_name: portal
    static_configs:

    • targets:
      • localhost
        labels:
        job: "HSJ_portal"
        __path__: /opt/cwy8.32/product/module/P/PORTAL/nohup.out
  • job_name: cwy
    static_configs:

    • targets:
      • localhost
        labels:
        job: "HSJ_cwy"
        __path__: /opt/cwy8.32/product/module/A/CWY/nohup.out
  • job_name: fasp_register
    static_configs:

    • targets:
      • localhost
        labels:
        job: "HSJ_fasp_register"
        __path__: /opt/cwy8.32/product/module/A/FASP/register-nohup.out
  • job_name: fasp_gateway
    static_configs:

    • targets:
      • localhost
        labels:
        job: "HSJ_fasp_gateway"
        __path__: /opt/cwy8.32/product/module/A/FASP/gateway-nohup.out
  • job_name: fasp_base
    static_configs:

    • targets:
      • localhost
        labels:
        job: "HSJ_fasp_base"
        __path__: /opt/cwy8.32/product/module/A/FASP/base-nohup.out
  • job_name: fasp_web
    static_configs:

    • targets:
      • localhost
        labels:
        job: "HSJ_fasp_web"
        __path__: /opt/cwy8.32/product/module/A/FASP/web-nohup.out

2.2.3 启动promtail服务

#启动Promtail命令,默认端口是9080
nohup ./promtail-linux-amd64 -config.file=promtail-local-config.yaml > promtail.log 2>&1 &

2.3 配置grafana

2.3.1 添加loki数据源

  登入grafana,点击【设置】按钮,进入【Configuration】页面;点击右侧的【add data source】添加loki数据源

2.3.2 配置数据源url

  点击loki数据源,配置url地址(loki服务所在服务器的IP地址,端口默认3100),随后点击【save&test】

2.3.3 选择日志查看选项

点击Explore,在Explore界面选择【job】,下一步可以选择需要查看的应用日志,如下显示:

2.3.4 查看日志示例:

选择HSJ_cwy,再点击右上角【Run query】,即可查看HSJ_cwy应用的日志信息,如下:

3.  导入loki日志查看仪表板(便于查看日志)

  点击grafana左侧菜单栏【+】-【import】,进入导入dashboard页面

  导入“下载的仪表板”和“创建的数据源”,点击【import】按钮,导入成功

loki仪表板链接:https://pan.baidu.com/s/19Jm1_k029wZj1iDP-MtDGg 提取码:de09


END

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章