Grafana Loki LogMonitor采集日志
1.采集架构图

image-20250830095509362
2.部署loki服务器
ruby
# 准备文件夹
root@ubuntu2204-98:~# cd /usr/local/prometheus_monitor/
root@ubuntu2204-98:/usr/local/prometheus_monitor# mkdir loki/{config,data_loki}
root@ubuntu2204-98:/usr/local/prometheus_monitor# mkdir -p loki/{config,data_loki}
root@ubuntu2204-98:/usr/local/prometheus_monitor# ls loki/
config data_loki
root@ubuntu2204-98:/usr/local/prometheus_monitor# cd loki/config/
root@ubuntu2204-98:/usr/local/prometheus_monitor/loki# chmod -R 777 *
root@ubuntu2204-98:/usr/local/prometheus_monitor/loki/config#
# 下载配置文件到config配置文件(一般不用修改,可以根据自己需要进行修正)
wget https://raw.githubusercontent.com/grafana/loki/v3.4.1/cmd/loki/loki-local-config.yaml -O loki-config.yaml
# 这里我修改了loki-config.yaml中alertmanager_url地址
ruler:
alertmanager_url: http://192.168.1.98:9093
# 准备docker-compose文件
root@ubuntu2204-98:/usr/local/prometheus_monitor/loki# vi docker-compose.yml
services:
loki:
container_name: loki
image: grafana/loki:main-04f8929
command: "-config.file=/etc/loki/config.yaml"
ports:
- "9096:9096"
- "3100:3100"
volumes:
- ./config/loki-config.yaml:/etc/loki/config.yaml:ro
- ./data_loki:/loki:rw
restart: unless-stopped
# 启动并查看状态
root@ubuntu2204-98:/usr/local/prometheus_monitor/loki# docker-compose up -d
root@ubuntu2204-98:/usr/local/prometheus_monitor/loki# docker-compose ps
NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS
loki grafana/loki:main-04f8929 "/usr/bin/loki -conf..." loki 9 seconds ago Up 8 seconds 0.0.0.0:3100->3100/tcp, :::3100->3100/tcp, 0.0.0.0:9096->9096/tcp, :::9096->9096/tcp
3.Grafana添加loki数据源

image-20250830102428603

image-20250830102458798

image-20250830102542094
4.监控端部署并接入系统日志
ruby
# 创建目录
root@ubuntu2204-98:/usr/local/prometheus_monitor# mkdir -p promtail/config
root@ubuntu2204-98:/usr/local/prometheus_monitor# chmod -R 777 promtail/*
# 创建配置文件,因为我只是复现之前工作中的内容所以都在一台机器上,如果你是实际部署记得修改相关配置
root@ubuntu2204-98:/usr/local/prometheus_monitor# cd promtail/
root@ubuntu2204-98:/usr/local/prometheus_monitor/promtail# vi config/promtail-config.yaml
server:
http_listen_port: 9080
grpc_listen_port: 0
positions:
filename: /tmp/positions.yaml
clients:
- url: http://192.168.1.98:3100/loki/api/v1/push
scrape_configs:
- job_name: system
static_configs:
- targets:
- 192.168.1.98
labels:
job: servlogs
__path__: /var/log/*log
# 创建docker-compose文件
root@ubuntu2204-98:/usr/local/prometheus_monitor/promtail# vi docker-compose.yml
services:
promtail:
image: grafana/promtail:main-04f8929
container_name: promtail
volumes:
- /usr/local/prometheus_monitor/promtail/config:/mnt/config
- /var/log:/var/log
command: -config.file=/mnt/config/promtail-config.yaml
4.1 ssh日志模板 17514

image-20250830102822991
4.2 系统日志模板 13639

image-20250830103127506
5.接入Docker容器日志监控
css
# 这里是对单独容器的监控,也就是你在启动其他容器的时候也要单独设置
# 被监控端的Docker安装插件
root@ubuntu2204-98:~/n8n# docker plugin install grafana/loki-docker-driver:latest --alias loki
Plugin "grafana/loki-docker-driver:latest" is requesting the following privileges:
- network: [host]
Do you grant the above permissions? [y/N] y
latest: Pulling from grafana/loki-docker-driver
Digest: sha256:82270b494d8c157f035fde65bc0159076f4c4361b5bb061b4fc817e8db04234a
32dbe5a742d5: Complete
Installed plugin grafana/loki-docker-driver:latest
# 安装后启动一个docker应用来测试下,启动的时候要指定日志服务器参数
# docker-compose文件
services:
n8n:
image: n8nio/n8n
container_name: n8n
# 这里指定了日志服务器位置
logging:
driver: loki
options:
loki-url: "http://192.168.1.98:3100/loki/api/v1/push"
loki-retries: 2
loki-max-backoff: 800ms
loki-timeout: 1s
keep-file: "true"
mode: "non-blocking"
5.1 容器日志查看

image-20250830105210635
6.结束语
到这里大概日志收集的方法都已经展示了,实验环境是无所谓规划一类的东西,在实际生产中大家如果要部署,一定要根据自己公司实际情况,对于主机硬件配置、收集信息内容等进行合理规划,不然你上了监控可能会让你自己更加混乱,切记切记~~~~!