Docker 部署 Grafana + Loki + Promtail 日志可视化搭建

1.Promtail配置文件

bash 复制代码
vim /data/promtail/promtail-config.yaml

server:
  http_listen_port: 9080
  grpc_listen_port: 0
positions:
  filename: /tmp/positions.yaml
clients:
  - url: http://host.docker.internal:3100/loki/api/v1/push
scrape_configs:
  - job_name: crm-logs
    static_configs:
      - targets:
          - localhost
        labels:
          job: "crm"
          __path__: /logs/*.log

2.启动 Loki

bash 复制代码
docker run -d --name loki -p 3100:3100 grafana/loki:latest

3、启动Grafana

bash 复制代码
docker run -d --name grafana -p 3000:3000 grafana/grafana:latest
bash 复制代码
http://localhost:3000
默认账号密码:
admin / admin

4.启动 Promtail

bash 复制代码
docker run -d --name promtail 
 -v /data/promtail/promtail-config.yaml:/etc/promtail/config.yml 
 -v /data/xxxxx/logs:/logs
 grafana/promtail:latest --config.file=/etc/promtail/config.yml

重要

验证 Promtail 状态:

bash 复制代码
docker logs promtail

如果看到:

tail routine: started

说明成功采集日志了

5.在 Grafana 接入 Loki

进入 Grafana:

导航:

Connections → Data sources → Add data source

选择:

Loki

URL 填:

bash 复制代码
http://host.docker.internal:3100

点击:

Save & test

6.日志查询语法(重点)

  1. Explore →Datasource 选择 Loki

    2.基础查询:
bash 复制代码
{job="crm"}
1)筛选错误日志
{job="crm"} |= "ERROR"
2)统计 ERROR 数量(支持图表)
sum(count_over_time({job="crm"} |= "ERROR" [1m]))
3)搜索关键字(如 cus)
{job="crm"} |= "cus"
相关推荐
Patrick_Wilson5 天前
从「改个端口」到 502:Next.js on k8s 的容器端口、Service 映射与 env 覆盖
docker·kubernetes·next.js
SRETalk5 天前
Zabbix、Prometheus、Grafana、Nightingale,四个监控如何选型?
zabbix·grafana·prometheus·nightingale
Suroy5 天前
DockerView-Go:用 Go 写一个终端 Docker 监控工具,顺便做了个 Web 仪表盘
docker
云恒要逆袭5 天前
运行你的第一个Docker容器
后端·docker·容器
宋均浩6 天前
# Docker 镜像瘦身实战:从 1.2G 到 80MB 的五个优化步骤
ci/cd·docker
程序员老赵7 天前
10 分钟部署 OpenCode:Docker 一键安装,浏览器打开就能用 AI 写代码(附完整命令与排错)
docker·容器·ai编程
WangMingHua1117 天前
LM Studio Docker 部署——本地大模型一键启动
docker
曲幽8 天前
别再用网页翻译看源码了!你的私人翻译神器LibreTranslate,部署避坑指南来了
python·docker·web·pot·translate·libretranslate·arogstranslate
武子康10 天前
调查研究-183 Apple container:Mac 上用轻量 VM 跑 Linux 容器,Swift 会改写本地容器体验吗?
docker·容器·apple
Alsn8613 天前
等待学习-学习目录:Docker 容器安全攻防
学习·安全·docker