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

語言: CN / TW / HK

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

↓ 這僅是免費CDN靜態加速的記錄。 2022/02/1645946288791.png

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

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

部署之後的預設介面展示,什麼都沒做的情況下這個介面也算有點味了: 2022/03/1647786549140.png

Docker環境

Docker官方自動指令碼,執行後自動將Docker的穩定(stable)版本安裝在系統中: shell curl -fsSL get.docker.com -o get-docker.sh sudo sh get-docker.sh --mirror Aliyun 配置映象加速: mkdir -p /etc/docker tee /etc/docker/daemon.json <<-'EOF' { "registry-mirrors": ["https://kfwkfulq.mirror.aliyuncs.com"] } EOF sudo systemctl daemon-reload sudo systemctl start docker

Docker中國區官方 https://registry.docker-cn.com 網易 http://hub-mirror.c.163.com 阿里雲 https://kfwkfulq.mirror.aliyuncs.com

Loki安裝

找個不會常修改的目錄下載所需配置檔案,因為後面會對映到容器中 shell wget https://raw.githubusercontent.com/grafana/loki/v2.4.2/cmd/loki/loki-local-config.yaml -O loki-config.yaml Docker安裝Loki shell 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安裝

shell docker run -d -p 3001:3000 -v /etc/localtime:/etc/localtime --name=grafana grafana/grafana 訪問http://xxxxxx:3001,預設使用者名稱密碼admin,第一次初始化需要較長時間,之後直接點選配置一個數據源,點選Loki,配置好url點選底部的Save&test按鈕。

2022/03/1647767452247.png

接著匯入模板:12559(Nginx監控模板,更多模板訪問官網2022/03/1647769105473.png

但是目前是沒有資料的,需要將日誌代理到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; 配置檔案`nginx.conf`中的http物件中寫入檔案路徑: include /etc/nginx/extra/blockips.conf; ```