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"
相关推荐
虎头金猫1 天前
4K 视频总卡在公网带宽?用 N1 + OpenList 把网盘播放链路重新理顺
运维·服务器·网络·python·容器·beautifulsoup·pandas
分布式存储与RustFS1 天前
MinIO 官方 Docker 镜像被移除:依赖它的项目该怎么办
docker·云原生·devops·对象存储·minio·分布式存储
玉&心1 天前
通过Arthas在线诊断K8S中的内存及JVM等使用情况
docker·k8s·arthas
xing-xing1 天前
Docker容器中Nginx站点根目录网页配置访问
nginx·docker
赵文宇(温玉)1 天前
CoreDNS的hosts插件的缺行配置导致k8s集群异常
容器·kubernetes·rancher
guo_wen_qiang1 天前
上传本地镜像到harbor中
docker·容器·持续部署
羑悻的小杀马特1 天前
Dockerfile 全景指南:从入门编写dockerfile到生产环境镜像优化实战,一步步带你从啃透制作指令到镜像制作落地!
docker·dockerfile·镜像制作
BianHuanShiZhe1 天前
docker常见命令
docker
正经教主1 天前
【FDE系列】阶段2:Day 43:Docker Compose — 多容器一键编排
人工智能·docker·fde
忆~遂愿1 天前
本地片库怎么在外面打开?Plex + cpolar 搭一套可远程访问的私人影音库
docker·容器