image-20230413231355163官网git :

https://github.com/grafana/loki

官网git:https://github.com/grafana/loki

参数参考:

Grafana Loki configuration parameters | Grafana Loki documentation

日志告警:

Loki日志系统告警_mahj125的博客-CSDN博客

接口:

Grafana Loki HTTP API | Grafana Loki documentation

准备:一台服务器+安装包(grafana不做讲解)

#资源包
curl -O -L "<https://github.com/grafana/loki/releases/download/v2.8.0/loki-linux-amd64.zip>"
curl -O -L "<https://github.com/grafana/loki/releases/download/v2.8.0/promtail-linux-amd64.zip>"

一、loki部署

#
mkdir -p loki/{chunks,rules}
unzip loki-linux-amd64.zip -C loki
chmod +x loki-linux-amd64
​
#增加配置文件:与可执行的loki文件同级目录
cd loki
#vi loki.yaml:
auth_enabled: false
​
server:
  http_listen_port: 3100
​
common:
  path_prefix: /loki
  storage:
    filesystem:
      chunks_directory: /home/dpan/soft/loki/chunks
      rules_directory: /home/dpan/soft/root/loki/rules
  replication_factor: 1
  ring:
    instance_addr: 127.0.0.1
    kvstore:
      store: inmemory
​
schema_config:
  configs:
    - from: 2020-10-24
      store: boltdb-shipper
      object_store: filesystem
      schema: v11
      index:
        prefix: index_
        period: 24h
​
#配置规则使用 不用的情况注释掉避免端口暴露
#ruler:
#  alertmanager_url: <http://10.252.32.99:9093>
​
limits_config:
  enforce_metric_name: false
  reject_old_samples: true
  reject_old_samples_max_age: 168h
  ingestion_rate_mb: 30  #修改每用户摄入速率限制,即每秒样本量,默认值为4M
  ingestion_burst_size_mb: 15  #修改每用户摄入速率限制,即每秒样本量,默认值为6M
​
#增加服务管理
cat <<EOF >/etc/systemd/system/loki.service
[Unit]
Description=loki server
Wants=network-online.target
After=network-online.target
​
[Service]
ExecStart=/home/dpan/soft/loki/loki-linux-amd64 -config.file=/home/dpan/soft/loki/loki.yaml
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=loki
[Install]
WantedBy=default.target
EOF
​
#启动和自启
systemctl start loki
systemctl enable loki

二、promtail部署

mkdir promtail
unzip promtail-linux-amd64.zip -C promtail
chmod +x promtail-linux-amd64
​
##增加配置文件:与可执行的loki文件同级目录
cd promtail
#vi promtail.yaml:
server:
  http_listen_port: 9080
  grpc_listen_port: 0
​
positions:
  filename: /home/dpan/soft/promtail/positions.yaml
​
clients:
  - url: <http://10.252.32.99:3100/loki/api/v1/push>  #loki的ip地址 默认3100端口
​
scrape_configs:
  - job_name: 10.252.32.71
    static_configs:
      - targets:
          - 10.252.32.71-xxx
        labels:
          job:xxx-8094                 #job的名称
          host: 10.252.32.71-xxx    #host名称和targets一致
          __path__: /home/dpan/xxx/run.log #日志路径
​
      - targets:
          - 10.252.32.71-xxx
        labels:
          job: xxx-8098
          host: 10.252.32.71-xxx
          __path__: /home/dpan/xxx/run.log
​
#增加服务管理
cat <<EOF >/etc/systemd/system/promtail.service
[Unit]
Description=promtail server
Wants=network-online.target
After=network-online.target
[Service]
ExecStart=/home/dpan/soft/promtail/promtail-linux-amd64 -config.file=/home/dpan/soft/promtail/promtail.yaml
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=promtail
[Install]
WantedBy=default.target
EOF
​
systemctl daemon-reload
systemctl enable promtail

三、grafana采集

image-20230413231723537

image-20230413231704815

image-20230413231645131

四、转发

#使用nginx转发 此处使用了basic认证
​
location  /loki/ {
        auth_basic           "Please input password";
        auth_basic_user_file /etc/nginx/.htpasswd;
    proxy_set_header        Host $host;
    proxy_set_header        X-Real-IP $remote_addr;
    proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header        Upgrade $http_upgrade;
    proxy_set_header        Connection "upgrade";
​
        if ($request_uri ~* "/loki/(.*)"){
                        proxy_pass http://10.252.32.99:3100/$1;
                        break;
        }
    proxy_pass http://10.252.32.99:3100/loki;
}