天天看點

快速部署Grafana日志監控+Nginx封禁IP

事情的起因還要從某天突然發現服務有明顯異常的通路記錄說起,雖然我這1核2G的小機器沒什麼公雞的價值,但也有了部署一套監控系統的想法。

↓ 這僅是免費CDN靜态加速的記錄。
快速部署Grafana日志監控+Nginx封禁IP

一番簡單的調研之後,我發現主流的日志監控系統為ELK組合(Elasticsearch, Logstash, Kibana),還有Prometheus體系等,但這些相對而言有着較高學習成本與資源占用,适合複雜場景與企業級别使用,不太适合個人使用,于是最終我選擇了曾經的老朋友Grafana作為基礎快速搭建起輕量可擴充的日志監控系統。

Grafana本身支援多種資料對接,擴充性極高,本文不深入展開,所有監控名額圍繞着Nginx,因為我的服務都是通過Nginx來代理端口的,是以日志的資料源由Nginx提供,但Nginx提供的log隻是一個文本檔案,我們要解析文本内的資料來進行分析,也可以解析轉換為其他日志堆棧來獲得更好的資料處理能力,這裡我使用了Grafana團隊自研的Loki來作為資料堆棧,Loki同時提供了一個代理服務,負責将日志進行解析和推送。

部署之後的預設界面展示,什麼都沒做的情況下這個界面也算有點味了

Docker環境

Docker官方自動腳本,運作後自動将Docker的穩定(stable)版本安裝在系統中:

curl -fsSL get.docker.com -o get-docker.sh
sudo sh get-docker.sh --mirror Aliyun      

配置鏡像加速:

mkdir      
tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://kfwkfulq.mirror.aliyuncs.com"]
}
EOF      
sudo systemctl daemon-reload
sudo systemctl start      

Docker中國區官方 ​​registry.docker-cn.com​​​

網易 ​​​hub-mirror.c.163.com​​​

阿裡雲 ​​​kfwkfulq.mirror.aliyuncs.com​​

Loki安裝

找個不會常修改的目錄下載下傳所需配置檔案,因為後面會映射到容器中

wget https://raw.githubusercontent.com/grafana/loki/v2.4.2/cmd/loki/loki-local-config.yaml -O loki-config.yaml      

Docker安裝Loki

docker run -d --name loki -u root -v $(pwd):/mnt/config -v /data/nginx-home/loki:/tmp/loki -v /etc/localtime:/etc/localtime -p 3002:3100 grafana/loki:2.4.2 -config.file=/mnt/config/loki-config.yaml      

等待一會通路 ​

​http://xxxxxx:3002/ready​

​ 準備就緒

​​官網連結 - 安裝 Grafana Loki​​

Grafana安裝

docker run -d -p 3001:3000 -v /etc/localtime:/etc/localtime --name=grafana grafana/grafana      

通路​

​http://xxxxxx:3001​

​​,預設使用者名密碼admin,第一次初始化需要較長時間,之後直接點選配置一個資料源,點選Loki,配置好url點選底部的​

​Save&test​

​按鈕。

快速部署Grafana日志監控+Nginx封禁IP

接着導入模闆:12559(Nginx監控模闆,更多模闆​​通路官網​​)

快速部署Grafana日志監控+Nginx封禁IP

但是目前是沒有資料的,需要将日志代理到Loki中。

Promtail安裝

運作安裝promtail

同樣是找個目錄先下載下傳預設配置

wget https://raw.githubusercontent.com/grafana/loki/v2.4.2/clients/cmd/promtail/promtail-docker-config.yaml -O promtail-config.yaml      

下載下傳完後打開編輯修改配置:

server:
  http_listen_port: 0
  grpc_listen_port: 0

positions:
  filename: /tmp/positions.yaml

clients:
  - url: http://xxxxxxx:3002/loki/api/v1/push  # 修改為你的Loki服務推送位址

scrape_configs:
  - job_name: system
    static_configs:
      - targets:
          - localhost
        labels:
          job: nginx_access_log
          agent: promtail
          __path__: /usr/local/nginx/logs/host.access.log      

注意​

​/data/nginx-home/extra/logs​

​修改你自己的nginx日志映射路徑:

docker run -d --name promtail -v $(pwd):/mnt/config -v /etc/localtime:/etc/localtime -v /data/nginx-home/extra/logs:/usr/local/nginx/logs grafana/promtail:2.4.2 -config.file=/mnt/config/promtail-config.yaml      

修改Nginx配置

log_format json_analytics '{'
                            '"msec": "$msec", ' # request unixtime in seconds with a milliseconds resolution
                            '"connection": "$connection", ' # connection serial number
                            '"connection_requests": "$connection_requests", ' # number of requests made in connection
                    '"pid": "$pid", ' # process pid
                    '"request_id": "$request_id", ' # the unique request id
                    '"request_length": "$request_length", ' # request length (including headers and body)
                    '"remote_addr": "$remote_addr", ' # client IP
                    '"remote_user": "$remote_user", ' # client HTTP username
                    '"remote_port": "$remote_port", ' # client port
                    '"time_local": "$time_local", '
                    '"time_iso8601": "$time_iso8601", ' # local time in the ISO 8601 standard format
                    '"request": "$request", ' # full path no arguments if the request
                    '"request_uri": "$request_uri", ' # full path and arguments if the request
                    '"args": "$args", ' # args
                    '"status": "$status", ' # response status code
                    '"body_bytes_sent": "$body_bytes_sent", ' # the number of body bytes exclude headers sent to a client
                    '"bytes_sent": "$bytes_sent", ' # the number of bytes sent to a client
                    '"http_referer": "$http_referer", ' # HTTP referer
                    '"http_user_agent": "$http_user_agent", ' # user agent
                    '"http_x_forwarded_for": "$http_x_forwarded_for", ' # http_x_forwarded_for
                    '"http_host": "$http_host", ' # the request Host: header
                    '"server_name": "$server_name", ' # the name of the vhost serving the request
                    '"request_time": "$request_time", ' # request processing time in seconds with msec resolution
                    '"upstream": "$upstream_addr", ' # upstream backend server for proxied requests
                    '"upstream_connect_time": "$upstream_connect_time", ' # upstream handshake time incl. TLS
                    '"upstream_header_time": "$upstream_header_time", ' # time spent receiving upstream headers
                    '"upstream_response_time": "$upstream_response_time", ' # time spend receiving upstream body
                    '"upstream_response_length": "$upstream_response_length", ' # upstream response length
                    '"upstream_cache_status": "$upstream_cache_status", ' # cache HIT/MISS where applicable
                    '"ssl_protocol": "$ssl_protocol", ' # TLS protocol
                    '"ssl_cipher": "$ssl_cipher", ' # TLS cipher
                    '"scheme": "$scheme", ' # http or https
                    '"request_method": "$request_method", ' # request method
                    '"server_protocol": "$server_protocol", ' # request protocol, like HTTP/1.1 or HTTP/2.0
                    '"pipe": "$pipe", ' # "p" if request was pipelined, "." otherwise
                    '"gzip_ratio": "$gzip_ratio", '
                    '"http_cf_ray": "$http_cf_ray"'
                    '}';

access_log  /etc/nginx/extra/logs/host.access.log  json_analytics;      

重新開機各個服務以應用配置

docker restart nginx
docker restart loki
docker restart promtail      

之後便可以回到grafana中檢視儀表盤裡的資料了

日志清理

由于過去太久的日志資訊并沒有太大的價值,是以需要定時清理,否則很容易成為磁盤容量殺手,Loki 的設計目标是降低存儲日志的成本,是以我認為保持其預設的删除政策即可(絕對不是因為懶得去看文檔嗯),而nginx的日志檔案則可以随意删除,避免累積太多。

首先建立一個​

​auto_clear.sh​

​檔案:

#!/bin/bash
rm -rf /data/nginx-home/extra/logs/host.access.log # 換成你的日志路徑
docker restart nginx # 重新開機nginx,簡單粗暴      

打開計劃任務

crontab -e      

寫入腳本每天淩晨4點自動運作

00 04 * * * /bin/bash /data/nginx-home/extra/logs/auto_clear.sh      

​wq​

​退出并重新整理應用

service crond restart      

檢視計劃任務:

crontab -l      

Nginx封禁IP

nginx封禁IP非常簡單,先建立一個​

​blackips.conf​

​檔案:

# 每一行代表一個黑名單IP
deny 1.2.3.4;
deny 110.191.215.8;
deny 110.191.214.214;      
include       /etc/nginx/extra/blockips.conf;      

繼續閱讀