Apache Exporter 监控手札
前言
Apache Exporter 是一个 Prometheus 的 exporter,用于收集 Apache HTTP Server 的指标数据,并通过 Prometheus 进行监控。
步骤
以下是使用 Apache Exporter 进行监控的步骤:
前提条件
- 已安装并运行 Apache HTTP Server。
- 已安装 Prometheus。
安装 Apache Exporter
下载并安装
- 前往 Apache Exporter 的 GitHub 发布页面下载最新版本的二进制文件。
解压并运行
tar xvfz apache_exporter-*.linux-amd64.tar.gz
cd apache_exporter-*.linux-amd64
./apache_exporter [flags]
为了安全这边使用了basic_auth_users
进行验证获取数据认证,web-config.yml
如下:
# Usernames and passwords required to connect.
# Passwords are hashed with bcrypt: https://github.com/prometheus/exporter-toolkit/blob/master/docs/web-configuration.md#about-bcrypt.
basic_auth_users:
alice: $2y$10$mDwo.lAisC94iLAyP81MCesa29IzH37oigHC/42V2pdJlUprsJPze
bob: $2y$10$hLqFl9jSjoAAy95Z/zw8Ye8wkdMBM8c5Bn1ptYqP/AXyV0.oy0S8m
run.sh脚本如下:
cd /AppHome/apache_exporter
nohup ./apache_exporter --web.config.file="./web-config.yml" --scrape_uri="http://localhost:51780/server-status/?auto" > ./apache_exporter.log 2>&1 &
配置 Apache ServerStatus
启用 mod_status 模块
确保在 Apache 配置中启用了 mod_status
模块,并允许从 Apache Exporter 的 IP 访问。
####################-Apache Server Status--------------
ExtendedStatus On
<Location /server-status>
SetHandler server-status
Order deny,allow
Deny from all
Allow from all
</Location>
###################--Apache Server Status--end---------
重启 Apache
sudo systemctl restart apache2
验证Apache Exporter
查看是否有数据采集成功,访问:http://ip:9117/metrics
配置 Prometheus
apiVersion: v1
kind: Secret
metadata:
name: apache-exporter-basic-auth
namespace: monitoring
data:
password: aaaaa=
user: bbbbb==
type: Opaque
---
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: external-apache-exporter
labels:
app: apache-exporter
release: prometheus-community
namespace: monitoring
spec:
endpoints:
- port: http-metrics
interval: 15s
path: /metrics
basicAuth:
username:
name: apache-exporter-basic-auth
key: user
password:
name: apache-exporter-basic-auth
key: password
relabelings:
- sourceLabels: [ __meta_kubernetes_service_label_app, __meta_kubernetes_service_label_system ]
targetLabel: job
separator: "-" # 用连字符连接两个标签值
action: replace
selector:
matchLabels:
app: apache
type: apache-exporter
namespaceSelector:
matchNames:
- monitoring
---
apiVersion: v1
kind: Service
metadata:
name: external-apache-dyck-exporter
labels:
app: apache
type: apache-exporter
system: dyck
namespace: monitoring
spec:
type: ClusterIP
clusterIP: None
ports:
- name: http-metrics
port: 9121
protocol: TCP
---
apiVersion: v1
kind: Endpoints
metadata:
name: external-apache-dyck-exporter
labels:
app: apache
type: apache-exporter
system: dyck
namespace: monitoring
subsets:
- addresses:
- ip: 10.196.68.159
ports:
- name: http-metrics
port: 9117
protocol: TCP
---
apiVersion: monitoring.coreos.com/v1
kind: PrometheusRule
metadata:
labels:
prometheus: k8s
role: alert-rules
release: prometheus-community
name: apache-rules
namespace: monitoring
spec:
groups:
- name: apache.rules
rules:
# 规则:Apache 服务宕机
- alert: ApacheDown
expr: apache_up == 0
for: 1m
labels:
severity: critical
annotations:
summary: "Apache 服务宕机 (instance {{ $labels.instance }})"
description: "Apache 服务已停止,请立即检查!"
# 规则:请求速率突增(5分钟内增长10倍)
- alert: ApacheRequestRateSpike
expr: rate(apache_accesses_total[1m]) > 10 * avg_over_time(apache_accesses_total[5m])
for: 2m
labels:
severity: warning
annotations:
summary: "Apache 请求速率突增 (instance {{ $labels.instance }})"
description: "当前请求速率:{{ $value }} 请求/秒。"
# 规则:CPU 使用率过高
- alert: ApacheHighCPU
expr: rate(apache_cpu_time_ms_total[5m]) > 1000
for: 10m
labels:
severity: warning
annotations:
summary: "Apache CPU 使用率过高 (instance {{ $labels.instance }})"
description: "CPU 使用时间:{{ $value }}ms/秒。"
grafana可视化(可选)
安装对应的 Grafana Dashboard,id为:3894