柯尔特-眼镜蛇王(40)
一、环境准备与依赖安装
在开始部署前,请确保您的服务器满足以下最低配置要求:
- 操作系统:Ubuntu 20.04 LTS 或 CentOS 8
- CPU:4核
- 内存:8GB
- 磁盘空间:50GB
- 网络:开放端口 3000、9090、9093、9094
首先安装系统依赖包,执行以下命令:
``` Ubuntu/Debian 系统 sudo apt update sudo apt install -y wget curl gnupg2 software-properties-common CentOS/RHEL 系统 sudo yum install -y wget curl epel-release ```安装 Docker 和 Docker Compose,这是运行柯尔特-眼镜蛇王(40)的容器环境:
``` 安装 Docker curl -fsSL https://get.docker.com -o get-docker.sh sudo sh get-docker.sh sudo systemctl start docker sudo systemctl enable docker 安装 Docker Compose sudo curl -L "https://github.com/docker/compose/releases/download/v2.20.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose sudo chmod +x /usr/local/bin/docker-compose ```二、核心组件部署配置
创建项目目录并下载配置文件:
``` mkdir -p /opt/cobra-king && cd /opt/cobra-king wget https://raw.githubusercontent.com/cobra-king/config/main/docker-compose.yml ```编辑 docker-compose.yml 文件,配置核心服务:
``` version: '3.8' services: prometheus: image: prom/prometheus:v2.45.0 container_name: cobra_prometheus volumes: - ./prometheus.yml:/etc/prometheus/prometheus.yml - prom_data:/prometheus command: - '--config.file=/etc/prometheus/prometheus.yml' - '--storage.tsdb.path=/prometheus' - '--web.enable-lifecycle' ports: - "9090:9090" restart: unless-stopped grafana: image: grafana/grafana:10.0.3 container_name: cobra_grafana volumes: - grafana_data:/var/lib/grafana environment: - GF_SECURITY_ADMIN_PASSWORD=Admin@2024 ports: - "3000:3000" restart: unless-stopped alertmanager: image: prom/alertmanager:v0.25.0 container_name: cobra_alertmanager volumes: - ./alertmanager.yml:/etc/alertmanager/alertmanager.yml ports: - "9093:9093" - "9094:9094" restart: unless-stopped volumes: prom_data: grafana_data: ```创建 Prometheus 配置文件 prometheus.yml:
``` global: scrape_interval: 15s evaluation_interval: 15s alerting: alertmanagers: - static_configs: - targets: ['alertmanager:9093'] rule_files: - "alert_rules.yml" scrape_configs: - job_name: 'node_exporter' static_configs: - targets: ['localhost:9100'] metrics_path: /metrics scheme: http ```创建告警规则文件 alert_rules.yml:
``` groups: - name: node_alerts rules: - alert: HighCpuUsage expr: 100 - (avg by(instance)(irate(node_cpu_seconds_total{mode="idle"}[5m])) 100) > 80 for: 2m labels: severity: warning annotations: summary: "高CPU使用率 (实例 {{ $labels.instance }})" description: "CPU使用率超过80%,当前值 {{ $value }}%" - alert: HighMemoryUsage expr: (node_memory_MemTotal_bytes - node_memory_MemAvailable_bytes) / node_memory_MemTotal_bytes 100 > 85 for: 2m labels: severity: critical annotations: summary: "高内存使用率 (实例 {{ $labels.instance }})" description: "内存使用率超过85%,当前值 {{ $value }}%" ```创建 Alertmanager 配置文件 alertmanager.yml:
``` global: smtp_smarthost: 'smtp.gmail.com:587' smtp_from: 'your-email@gmail.com' smtp_auth_username: 'your-email@gmail.com' smtp_auth_password: 'your-app-password' route: group_by: ['alertname'] group_wait: 10s group_interval: 10s repeat_interval: 1h receiver: 'email-alerts' receivers: - name: 'email-alerts' email_configs: - to: 'admin@yourdomain.com' from: 'your-email@gmail.com' smarthost: 'smtp.gmail.com:587' auth_username: 'your-email@gmail.com' auth_password: 'your-app-password' send_resolved: true ```三、监控数据采集器部署
3.1 Node Exporter 安装
在被监控服务器上安装 Node Exporter:
``` 下载并安装 wget https://github.com/prometheus/node_exporter/releases/download/v1.6.0/node_exporter-1.6.0.linux-amd64.tar.gz tar xvfz node_exporter-1.6.0.linux-amd64.tar.gz sudo mv node_exporter-1.6.0.linux-amd64/node_exporter /usr/local/bin/ 创建系统服务 sudo tee /etc/systemd/system/node_exporter.service << EOF [Unit] Description=Node Exporter After=network.target [Service] User=node_exporter Group=node_exporter Type=simple ExecStart=/usr/local/bin/node_exporter [Install] WantedBy=multi-user.target EOF 创建专用用户并启动服务 sudo useradd -rs /bin/false node_exporter sudo systemctl daemon-reload sudo systemctl start node_exporter sudo systemctl enable node_exporter ```验证 Node Exporter 是否正常运行:
``` curl http://localhost:9100/metrics ```应该能看到大量的指标数据输出。
3.2 应用监控配置
对于 Spring Boot 应用,添加以下 Maven 依赖:
```在 application.yml 中配置:
``` management: endpoints: web: exposure: include: health,info,metrics,prometheus metrics: export: prometheus: enabled: true tags: application: ${spring.application.name} ```对于 Nginx,添加状态监控模块:
``` location /nginx_status { stub_status on; access_log off; allow 127.0.0.1; deny all; } ```使用 Nginx Exporter 采集数据:
``` docker run -d \ --name nginx-exporter \ -p 9113:9113 \ nginx/nginx-prometheus-exporter:0.11.0 \ -nginx.scrape-uri http://nginx-host:8080/nginx_status ```四、Grafana 仪表板配置
启动所有服务:
``` cd /opt/cobra-king docker-compose up -d ```访问 Grafana 控制台:
``` 地址:http://your-server-ip:3000 用户名:admin 密码:Admin@2024 ```配置数据源:
- 点击左侧齿轮图标进入"Configuration"
- 选择"Data Sources"
- 点击"Add data source"
- 选择"Prometheus"
- URL 填写:http://prometheus:9090
- 点击"Save & Test",显示"Data source is working"表示成功
导入监控仪表板:
- 点击左侧"+"号,选择"Import"
- 在"Import via grafana.com"输入:1860
- 点击"Load"
- 选择 Prometheus 数据源
- 点击"Import"
创建自定义告警面板:
- 点击"Create" -> "Dashboard" -> "Add new panel"
- 在 Metrics 中输入:rate(node_cpu_seconds_total{mode="idle"}[5m])
- 设置 Panel Title 为"CPU 使用率"
- 点击"Save"保存仪表板
五、告警测试与验证
测试 CPU 告警规则:
``` 创建高 CPU 负载测试 stress-ng --cpu 4 --timeout 300s ```等待 2 分钟后,检查告警状态:
``` 查看 Prometheus 告警 curl http://localhost:9090/api/v1/alerts 查看 Alertmanager 告警 curl http://localhost:9093/api/v2/alerts ```验证邮件告警是否收到,如果未收到,检查以下配置:
- Gmail 需要开启两步验证并生成应用专用密码
- 检查防火墙是否开放 587 端口
- 查看 Alertmanager 日志:docker logs cobra_alertmanager
配置企业微信告警(可选):
``` 在 alertmanager.yml 中添加 - name: 'wechat-alerts' wechat_configs: - corp_id: 'your-corp-id' to_party: '1' agent_id: '1000002' api_secret: 'your-api-secret' send_resolved: true ```六、日常维护与故障排查
6.1 数据保留策略
修改 Prometheus 数据保留时间,编辑 prometheus.yml:
``` 在启动命令中添加 command: - '--config.file=/etc/prometheus/prometheus.yml' - '--storage.tsdb.path=/prometheus' - '--storage.tsdb.retention.time=30d' - '--web.enable-lifecycle' ```重启服务生效:
``` docker-compose restart prometheus ```6.2 监控系统自身监控
添加 Prometheus 自监控配置:
``` 在 prometheus.yml 的 scrape_configs 中添加 - job_name: 'prometheus' static_configs: - targets: ['localhost:9090'] - job_name: 'alertmanager' static_configs: - targets: ['alertmanager:9093'] ```6.3 常见故障处理
问题1:Prometheus 启动失败
检查配置文件语法:
``` docker run --rm -v $(pwd):/config prom/prometheus:v2.45.0 \ --config.file=/config/prometheus.yml --check-config ```问题2:Grafana 无法连接数据源
检查网络连通性:
``` docker exec cobra_grafana ping prometheus ```如果无法 ping 通,检查 docker-compose 网络配置。
问题3:告警未触发
检查规则文件是否加载:
``` curl http://localhost:9090/api/v1/rules ```查看 Prometheus 目标状态:
``` curl http://localhost:9090/api/v1/targets ```七、性能优化建议
- 调整抓取间隔:对于生产环境,建议将 scrape_interval 调整为 30s
- 启用数据压缩:在 Prometheus 启动参数中添加 --storage.tsdb.wal-compression
- 配置远程存储:使用 Thanos 或 Cortex 实现长期存储和集群化
- 设置资源限制:在 docker-compose.yml 中为每个服务配置内存和 CPU 限制
完成以上所有步骤后,您的柯尔特-眼镜蛇王(40)监控平台已完全部署并运行。系统将自动采集服务器和应用指标,通过 Grafana 展示实时数据,并在异常时发送告警通知。
版权保护: 本文由 741卡盟 原创,转载请保留链接: http://741ka.com/gamenews/21215.html
- 上一篇:地下城与勇士魔刹石怎么获得
- 下一篇:纪念碑谷攻略第八关怎么过
