prometheu+loki+grafana+promtail日志系统搭建

575 阅读1分钟

1、prometheu+loki+grafana安装

新建docker network并且创建docker-compose文件

docker network create RichNetwork
# ref: https://grafana.com/docs/grafana-cloud/quickstart/docker-compose-linux/

version: '3'

networks:
  RichNetwork:
    external: true

volumes:
  prometheus_data:
  grafana-storage:
  loki_data:

services:
  prometheus:
    image: prom/prometheus:latest
    container_name: prometheus
    restart: always
    volumes:
      - ./conf/prometheus.yml:/etc/prometheus/prometheus.yml
      - prometheus_data:/prometheus
    environment:
      TZ: "Asia/Shanghai"
    command:
      - '--config.file=/etc/prometheus/prometheus.yml'
      - '--storage.tsdb.path=/prometheus'
      - '--web.console.libraries=/etc/prometheus/console_libraries'
      - '--web.console.templates=/etc/prometheus/consoles'
      - '--web.enable-lifecycle'
    expose:
      - 9090
    networks:
      - RichNetwork
    extra_hosts:
      - "host.docker.internal:host-gateway"

  loki:
    image: grafana/loki:latest
    environment:
      TZ: "Asia/Shanghai"
    ports:
      - "3100:3100"
    container_name: loki
    command: -config.file=/etc/loki/local-config.yaml
    volumes:
      - loki_data:/loki
    networks:
      - RichNetwork

  grafana:
    image: grafana/grafana-oss:latest
    container_name: grafana
    depends_on:
      - prometheus
    ports:
      - '3000:3000'
    environment:
      TZ: "Asia/Shanghai"
      # PASSWORD: IuC5n&kNcicsWSFc
    volumes:
      - grafana-storage:/var/lib/grafana
    networks:
      - RichNetwork
    restart: always

2、在同级目录下新建conf文件夹,新建prometheus.yml文件

global:
  scrape_interval: 1s

scrape_configs:
  - job_name: "prometheus"
    scrape_interval: 1m
    static_configs:
      - targets: ["prometheus:9090"]

  - job_name: "node-exporter-115"
    static_configs:
      - targets: ["10.1.11.115:9100"]

  - job_name: "node-exporter-116"
    static_configs:
      - targets: ["10.1.11.116:9100"]

3、docker-compose安装promtail转发日志文件信息到loki

新建docker-compose.yml文件,设置路径映射

version: "2"
  
services:
  promtail:
    image: grafana/promtail:latest
    restart: always
    volumes:
      - /home/gitlab-runner:/home/gitlab-runner
      - ./config/promtail-config.yml:/etc/promtail/config.yml
    command: -config.file=/etc/promtail/config.yml

在docker-compose.yml同级目录下新建config文件夹,新建promtail-config.yml文件, 设置日志文件转发

server:
  http_listen_port: 9080

positions:
  filename: /tmp/positions.yaml

clients:
  - url: http://10.1.11.115:3100/loki/api/v1/push

scrape_configs:
- job_name: pm2-log
  static_configs:
  - targets:
      - localhost
    labels:
      job: pm2-log
      host: 10.1.11.115
      __path__: /home/gitlab-runner/.pm2/logs/*.log

- job_name: logs
  static_configs:
  - targets:
      - localhost
    labels:
      job: logs
      host: 10.1.11.115
      __path__: /home/gitlab-runner/log/*.log

- job_name: pm2
  static_configs:
  - targets:
      - localhost
    labels:
      job: pm2
      host: 10.1.11.115
      __path__: /home/gitlab-runner/.pm2/*.log