1) 目标:在新加坡区域的云服务器上搭建监控+告警+性能分析体系,包含采集(agent)、存储(prometheus)、展示(grafana)与告警(alertmanager)。
2) 前提:有一台或多台Ubuntu/Debian/CentOS服务器、能SSH访问、能开通防火墙端口(Prometheus默认9090,Grafana3000,node_exporter9100,Alertmanager9093)。
3) 建议:在新加坡选择离用户近的可用区,准备一台监控主机(至少2CPU、4GB内存)作为Prometheus/Grafana/Alertmanager节点。
1) 推荐开源方案:Prometheus(采集+存储)+node_exporter(主机指标)+cAdvisor(容器)+Grafana(可视化)+Alertmanager(告警路由)。
2) 说明:Prometheus通过抓取metrics,Grafana读取Prometheus作为数据源展示,Alertmanager负责发送邮件/Slack/Webhook告警。
1) 以Ubuntu为例:SSH登录目标服务器。
2) 下载并安装:
wget https://github.com/prometheus/node_exporter/releases/latest/download/node_exporter-<版本>.linux-amd64.tar.gz
tar xzf node_exporter-<版本>.linux-amd64.tar.gz
sudo mv node_exporter-<版本>.linux-amd64/node_exporter /usr/local/bin/
3) 创建systemd服务:在 /etc/systemd/system/node_exporter.service 写入基本unit并启动:
sudo systemctl daemon-reload && sudo systemctl enable --now node_exporter
4) 验证:在浏览器或curl访问 http://<服务器IP>:9100/metrics 可见指标输出。
1) 下载并解压Prometheus,创建配置文件 prometheus.yml:
scrape_configs: - job_name: 'node' static_configs: - targets: ['<目标IP>:9100']
2) 启动方式:安装prometheus二进制到/usr/local/bin并创建systemd unit,然后 sudo systemctl enable --now prometheus。
3) 在Prometheus UI (http://<监控主机>:9090) 验证targets是否UP,若端口被阻塞请调整安全组或防火墙(ufw/iptables)。
1) 抓取间隔在prometheus.yml中调整:scrape_interval: 15s。
2) 多实例:对大量主机建议使用Prometheus federation或使用Pushgateway/Prometheus Operator。
3) 数据持久化:配置--storage.tsdb.path并定期做备份或使用远程存储(Remote Write)到Cortex/Thanos。
1) 安装:使用官方仓库安装或下载deb/rpm包,启动grafana-server。
2) 添加数据源:在Grafana UI -> Configuration -> Data Sources -> 添加Prometheus,URL填 http://
3) 导入仪表盘:Grafana官方或社区有Node Exporter和System Overview模板,直接导入ID或JSON。
4) 面板建议:CPU使用率、Load、内存/Swap、磁盘IO、磁盘使用率、网络吞吐、进程数、TCP连接数。
1) 在Prometheus规则文件 alerts.yml 示例:
groups: - name: host_alerts rules: - alert: HighCPU usage: avg by(instance) (rate(node_cpu_seconds_total{mode!="idle"}[5m])) > 0.8 for: 5m labels: severity: page annotations: summary: "High CPU on {{ $labels.instance }}"
2) 在prometheus.yml中引用规则文件并重载:sudo systemctl reload prometheus 或通过Prometheus API /-/reload。
3) 测试:在Prometheus UI -> Alerts 检查告警是否触发。
1) 安装Alertmanager并在alertmanager.yml中配置receivers:email, slack, webhook等。示例Slack: slack_configs: - api_url: 'https://hooks.slack.com/services/xxx' channel: '#alerts'.
2) 在Prometheus配置中加入alerting: alertmanagers: - static_configs: - targets: ['
3) 验证流程:在Prometheus触发告警->Alertmanager接收->发送到Slack或邮件。可用amtool测试通知。
1) 常用命令:top/htop 查看CPU进程分布;iostat -x 1 查看磁盘IO;iftop 或 iperf3 检测网络带宽;vmstat/sar 查看系统整体负载。
2) 示例分析:磁盘IO高时:iostat -x 1查看await/svctm,使用iotop找出占用进程;网络延迟高用ping/traceroute与iftop定位。
3) 深入分析:使用perf record/perf report对热点函数采样(适用于C/C++/Go等),使用strace/lsof排查系统调用或文件句柄问题。
1) 安全:仅开放必要监控端口到监控机,使用防火墙白名单和SSH密钥;Prometheus/Grafana启用基本认证或反向代理。
2) 告警策略:区分severity(info/warn/critical),设置抑制(silence)规则,避免告警风暴。
3) 维护:定期清理Prometheus旧数据、升级node_exporter与Grafana、对报警规则做演练。
答:首先通过Grafana查看内存使用曲线与Swap增长趋势;若发现持续上升,SSH到服务器执行 free -m 与 ps aux --sort=-%mem | head -n 20 确认占用进程;使用 pmap
答:常见原因包括防火墙/安全组未放行9100端口、node_exporter未启动、Prometheus目标配置错误或网络路由问题。排查步骤:1) 在Prometheus主机执行 curl http://<目标IP>:9100/metrics 看是否能返回;2) 在目标主机上 systemctl status node_exporter 查看服务状态与日志;3) 检查iptables/ufw或云安全组端口规则;4) 若有反向代理或NAT,确认端口转发正确。
答:在Alertmanager配置slack receiver,使用slack_configs并填写api_url与channel。示例annotations里使用模板:summary: 'Alert: {{ .CommonLabels.alertname }} on {{ .CommonLabels.instance }}' description: 'Current value: {{ index .Alerts 0 .Annotations.value }}',这样发送到Slack时消息会包含主机名(instance)和触发的指标值。配置完成后在Prometheus触发测试告警并确认Slack收到格式化消息。