i 这里我们以 docker 安装为主,更多安装方式请参考文末连接.
官网建议生产环境使用 Helm 或者 Tanka 方式安装,docker 方式安装的适用于开发环境和测试环境.- by jonnyan404 https://mrdoc.fun
方式一
下载配置文件
mkdir -p /opt/loki/conf
cd /opt/loki/conf
wget https://raw.githubusercontent.com/grafana/loki/v2.2.0/cmd/loki/loki-local-config.yaml -O loki-config.yaml
wget https://raw.githubusercontent.com/grafana/loki/v2.2.0/cmd/promtail/promtail-docker-config.yaml -O promtail-config.yaml
安装Loki
docker run -d --name=loki -v /opt/loki/conf:/mnt/config -p 3100:3100 grafana/loki:2.2.0 -config.file=/mnt/config/loki-config.yaml
查看metrics数据
http://IP:3100/metrics
查看loki运行状态
http://IP:3100/ready
安装promtail
仅搜集docker日志,此步可省略.
docker run -d --name=promtail -v /opt/loki/conf:/mnt/config -v /var/log:/var/log grafana/promtail:2.2.0 -config.file=/mnt/config/promtail-config.yaml
安装grafana
docker run -d --name=grafana
--restart always
-p 3000:3000
-v /opt/loki/grafana-storage:/var/lib/grafana
grafana/grafana
打开 http://IP:3000
访问grafana,默认用户密码为admin/admin。
选择左侧设置--->Data Sources--->Add data source,搜索Loki配置HTTP URL为 http://IP:3100
完成后选择左侧设置--->Preferences,修改底部默认时区为 Asia/Shanghai。
选择左侧Explore查看日志,可以基于文件名或标签查看.
方式二
下载docker-compose.yml
wget https://raw.githubusercontent.com/grafana/loki/v2.2.0/production/docker-compose.yaml -O docker-compose.yaml
修改docker-compose.yml
version: "3"
networks:
loki:
volumes:
loki-config:
promtail-config:
grafana-storage:
services:
loki:
image: grafana/loki:2.0.0
container_name: loki
environment:
- TZ=Asia/Shanghai
- LANG=zh_CN.UTF-8
ports:
- "3100:3100"
command: -config.file=/etc/loki/local-config.yaml
volumes:
- loki-config:/etc/loki
networks:
- loki
promtail:
image: grafana/promtail:2.0.0
container_name: promtail
environment:
- TZ=Asia/Shanghai
- LANG=zh_CN.UTF-8
volumes:
- /var/log:/var/log
- promtail-config:/etc/promtail
command: -config.file=/etc/promtail/config.yml
networks:
- loki
grafana:
image: grafana/grafana:latest
container_name: grafana
ports:
- "3000:3000"
environment:
- TZ=Asia/Shanghai
- LANG=zh_CN.UTF-8
volumes:
- grafana-storage:/var/lib/grafana
networks:
- loki
启动docker-compose.yml
docker-compose -f docker-compose.yaml up
3.监控docker日志
被监控机器安装Loki日志插件
docker plugin install grafana/loki-docker-driver:latest --alias loki --grant-all-permissions
docker plugin ls
全局日志搜集设置
编辑daemon.json。linux下默认路径是/etc/docker/daemon.json (需要sudo), windows则默认是%userprofile%.dockerdaemon.json
{
"log-driver": "loki",
"log-opts": {
"loki-url": "http://YOUR_IP:3100/loki/api/v1/push",
"loki-batch-size": "400",
"max-size": "50m",
"max-file": "10"
}
}
然后重启docker服务生效
指定容器搜集
docker run --rm --name=grafana --log-driver=loki --log-opt loki-url="http://YOUR_IP:3100/loki/api/v1/push" --log-opt max-size=50m --log-opt max-file=10
grafana/grafana
在 docker run 命令后增加红色内容即可
docker-compose配置日志输出到loki
docker-compose 小于3.4可以对需要日志输出的配置添加配置如下
version: "3"
services:
promtail:
image: grafana/promtail
container_name: promtail
restart: always
volumes:
- $PWD:/etc/promtail
- /app/promtail:/opt
- /app/promtail/log:/var/log
command:
-config.file=/etc/promtail/promtail-docker-config.yaml
logging:
driver: loki
options:
loki-url: "http://YOUR_IP:3100/loki/api/v1/push"
max-size: "50m"
max-file: "10"
对于3.4极其以上版本可以通过定义模板来减少代码量
version: "3.4"
x-logging:
&loki-logging
driver: loki
options:
loki-url: "http://YOUR_IP:3100/loki/api/v1/push"
max-size: "50m"
max-file: "10"
services:
host:
container_name: grafana
image: grafana/grafana
environment:
- TZ=Asia/Shanghai
- LANG=zh_CN.UTF-8
logging: *loki-logging
&loki-logging
表示定义模板
*loki-logging
表示引用模板。
对于多个服务就只需要对应加上一行 logging: *loki-logging
即可。
Reference Link
- by jonnyan404 https://mrdoc.fun
- 官网:https://grafana.com/docs/loki/latest/installation/docker/
- 日志驱动:https://github.com/grafana/loki/blob/master/docs/sources/clients/docker-driver/_index.md
- Loki原理简介:https://segmentfault.com/a/1190000023379491
- https://www.kancloud.cn/willseecloud/elasticsearch/1857769
- loki 源码解析: https://aleiwu.com/post/grafana-loki/
- http://52manong.com/60197.html