1) 准备一台台湾原生IP云服务器(Ubuntu 20.04/22.04 或 CentOS 7/8),确保能SSH登录并有root或sudo权限。
2) 检查网络与防火墙:开放Prometheus(9090)、Node Exporter(9100)、Grafana(3000)和Alertmanager(9093)端口;使用iptables或云主机控制台放通。
3) 同步时区:sudo timedatectl set-timezone Asia/Taipei。
1) 下载与安装(以Ubuntu为例):
sudo useradd -rs /bin/false node_exporter
wget https://github.com/prometheus/node_exporter/releases/download/v1.5.0/node_exporter-1.5.0.linux-amd64.tar.gz
tar xzf ... && sudo cp node_exporter-*/node_exporter /usr/local/bin/
2) 创建systemd服务:/etc/systemd/system/node_exporter.service 内容为:
[Unit]…
[Service] ExecStart=/usr/local/bin/node_exporter
[Install] WantedBy=multi-user.target
然后 sudo systemctl daemon-reload && sudo systemctl enable --now node_exporter。
1) 下载Prometheus并解压:wget https://github.com/prometheus/prometheus/releases/download/v2.48.0/prometheus-2.48.0.linux-amd64.tar.gz
2) 在/prometheus目录中放prometheus.yml,示例scrape_configs加入node_exporter目标:
- job_name: 'node' static_configs: - targets: ['your-server-ip:9100']
3) 以systemd方式运行Prometheus,启动后用curl http://localhost:9090/targets确认目标在线。
1) 下载并运行Alertmanager,编辑alertmanager.yml,配置接收器(邮箱、Webhook 或 Slack/LINE)。例如SMTP:smtp_smarthost: 'smtp.example.com:587',smtp_auth_username: 'notify@example.com'。
2) 在Prometheus中配置alerting.rules示例:
groups: - name: node_rules rules: - alert: NodeDown expr: up{job="node"} == 0 for: 2m labels: severity: critical annotations: description: "主机 {{ $labels.instance }} 无响应"
3) 在Prometheus配置文件prometheus.yml中加入alertmanager的地址,然后重启Prometheus并观察Alertmanager的告警接收页面。
1) 安装Grafana并添加Prometheus为数据源(URL指向Prometheus)。
2) 导入常用Dashboard(Node Exporter Full)并建立告警面板(Grafana 8+ 支持Alerting规则)。
3) 将Grafana告警通知与Alertmanager或Webhook集成,实现告警统一管理。
1) 在管理主机搭建Git仓库(GitLab/GitHub或裸仓库)。在每台台湾云服务器创建deploy用户并配置SSH公钥:sudo adduser deploy && mkdir /home/deploy/.ssh && echo "pubkey" > authorized_keys。
2) 使用裸仓库+post-receive钩子自动部署:在服务器/var/repo/app.git,post-receive中写checkout到/opt/app并执行脚本权限设置。示例:GIT_WORK_TREE=/opt/app git checkout -f。
1) 在管理机安装Ansible:pip install ansible 或 apt install ansible。创建inventory列出台湾IP。
2) 编写playbook示例:复制脚本到目标并设置为systemd服务或定时任务。
3) 运行示例:ansible-playbook -i inventory deploy.yml --limit taiwan_servers。优点:无侵入、可回滚、一次执行多台。
1) 优先采用systemd管理脚本(可监控、自动重启)。示例service文件 ExecStart=/usr/bin/python3 /opt/app/run.py Restart=on-failure。
2) 对于周期任务优先使用systemd timer替代cron:创建.timer与.service,timer管理粒度与日志更清晰。
3) 日志集中:将脚本日志写入systemd-journald或/var/log/app/*.log,配合Filebeat上报到ELK或Loki方便告警条件设定。
1) 测试报警链路:停止node_exporter或模拟服务异常,观察Prometheus target变为down并在Alertmanager接收告警。
2) 常用排查命令:systemctl status node_exporter、journalctl -u node_exporter -f、curl http://localhost:9090/targets。
3) 安全:限制Prometheus/Alertmanager访问仅允许运维网络或使用HTTP Basic/反向代理,定期更新二进制与依赖。
1) 将监控配置与脚本纳入同一Git仓库,使用CI(GitHub Actions/GitLab CI)自动跑lint、测试并触发Ansible部署。
2) 建议:对重要告警增加抑制策略(免打扰时间)、建立Runbook并在告警注释中附带修复命令示例。
答:在目标服务器确认node_exporter运行:sudo systemctl status node_exporter;本机或Prometheus服务器上curl http://目标IP:9100/metrics,若能看到大量metrics文本说明抓取端可用;在Prometheus UI的Targets页确认状态为UP。
答:推荐使用Git管理脚本并通过Ansible或CI触发部署:每次发布使用tag或release,部署前先在测试环境跑playbook,若发现问题可通过Git回退到上一个tag并用Ansible回滚代码与systemd重启服务。
答:先分类告警(severity),对频繁误报的规则加入for(持续时间)限制、阈值平滑(例如avg_over_time),并设置抑制和分组(Alertmanager)与免打扰窗口;同时优化监控指标粒度,必要时引入熔断或去噪脚本再上报。