Logstash将数据推送到Loki

logstash 推送数据到 loki

运行 logstash

Copy
# logstash
version: '3'
services:
logstash:
image: docker.elastic.co/logstash/logstash:8.12.0
container_name: logstash_server
restart: always
ports:
- 8065:8065
environment:
- LS_JAVA_OPTS=-Xmx1024m -Xms1024m
volumes:
- ./config/logstash.conf:/etc/logstash/conf.d/logstash.conf # 读取宿主机配置
entrypoint:
- logstash
- -f
- /etc/logstash/conf.d/logstash.conf
logging:
driver: "json-file"
options:
max-size: "200m"
max-file: "3"

logstash 运行后进入容器

Copy
# 安装logstash-output-loki插件
logstash-plugin install logstash-output-loki

修改 logstash 配置

Copy
// logstash.conf
input {
tcp {
port=> 8065
codec => "json"
}
}

output {
loki{
url => "http://loki:3100/loki/api/v1/push"
}
}

重启 logstash

运行 loki

Copy
# loki
version: '3'
services:
loki:
image: grafana/loki
container_name: loki
restart: always
ports:
- "3100:3100"
command: -config.file=/etc/loki/local-config.yaml

运行 grafana

Copy
version: '3'
services:
grafana:
image: grafana/grafana
container_name: grafana
hostname: grafana
restart: always
ports:
- "3000:3000"

进入 grafana, 打开左侧菜单，点击 Administration

点击 Plugin，搜索 loki

进入选项后点击 Add new data source

输入 loki 地址，http://localhost:3100

配置成功

查询日志

左侧菜单，explore，选择过滤条件后，Run query 进行查询

参考