使用 loki grafana 分析nginx 请求日志

时间:2023-04-10 17:22:12

loki 是类似prometheus 的log 可视化展示、收集系统,已经集成在grafana 6.0 版本中了
说明: 测试环境使用了docker-compose 运行

环境准备

  • docker-compose 文件
version: "3"
services:
  nginx:
    build: ./
    ports:
    - "8090:80"
    volumes:
    - $PWD:/etc/promtail
  loki:
    image: grafana/loki:master
    ports:
      - "3100:3100"
    volumes:
      - $PWD:/etc/loki
    command: -config.file=/etc/loki/loki-local-config.yaml
  promtail:
    image: grafana/promtail:make-images-static-26a87c9
    volumes:
      - $PWD:/etc/promtail
      - ./log:/var/log
    command: 
      -config.file=/etc/promtail/promtail-docker-config.yaml
  grafana:
    image: grafana/grafana:master
    ports:
      - "3000:3000"
    environment:
    - "GF_EXPLORE_ENABLED=true"
  • 配置说明
    nginx loki 配置,主要是使用容器运行多个应用
    dockerfile
FROM grafana/promtail:make-images-static-26a87c9 as builder
FROM openresty/openresty:alpine
ENV TINI_VERSION v0.18.0
RUN apk add --update \
    && apk add --no-cache tini
ADD entrypoint.sh /entrypoint.sh
ADD promtail.sh /promtail.sh
COPY nginx.conf usr/local/openresty/nginx/conf/
COPY --from=builder /usr/bin/promtail /usr/bin/
EXPOSE 80
ENTRYPOINT ["/sbin/tini","-s", "--", "/entrypoint.sh"]

entrypoint.sh:

#!/bin/sh
sh promtail.sh
exec /usr/local/openresty/bin/openresty -g "daemon off;"

promtail.sh:
后台方式运行,配置通过数据卷挂载

#!/bin/sh
nohup /usr/bin/promtail -config.file=/etc/promtail/promtail-docker-config2.yaml & 

nginx log 配置:
很简单,就是基本的配置

worker_processes 1;
+ error_log /var/log/error.log error;
user root;
events {
    worker_connections 1024;
}
http {
    include mime.types;
    default_type application/octet-stream;
    sendfile on;
    keepalive_timeout 65;
    gzip on;
    resolver 114.114.114.114;
    real_ip_header X-Forwarded-For;
    real_ip_recursive on;
+ log_format main '$remote_addr - $remote_user [$time_local] "$request" '
                               '$status $body_bytes_sent "$http_referer" '
                               '"$http_user_agent" "$http_x_forwarded_for"';
+ access_log /var/log/access.log main;

promtail ,loki 配置可以参考项目:https://github.com/rongfengliang/grafana-loki-demo
https://www.cnblogs.com/rongfengliang/p/10112500.html

启动&&测试

  • 构建包含agent 的 nginx
docker-compose build
  • 启动
docker-compose up -d

参考资料

https://www.cnblogs.com/rongfengliang/p/10112500.html
https://github.com/grafana/loki#getting-started
https://github.com/rongfengliang/grafana-loki-demo