ubuntu 22.04 配置 Prometheus 和 Grafana 服务器监控

由于内存白菜价, 家里的虚拟机越来越多了。

1个是难以管理hostname 和 ip, 只靠/etc/hosts 去记住其实并不方便

试过宝塔linux面板, 可惜是单机的,不适用于管理集群linux服务器, 而且里面一股金钱气息。
至于他们另1个产品堡塔云, 估计是集群管理, 但是安装失败, 而且看了价格感觉像智商税。

公司用过Appdynamic, 功能强大可惜更不是我能想的。
在这里插入图片描述

只能还是去寻找开源方案。好在外能的google提供了一套成熟的开源方案

Prometheus - 用于管理机 - 集群服务器数据收集
node_exporter - 用于被管理节点 - 提供节点本身的服务器数据给Prometheus
Grafana - 数据展示工具

下面就会写出详细的安装步骤



1. 服务器准备

No hostname ip cpu 数 memory os comment
1 amdeuc-vm0 10.0.1.155 1 2 ubuntu 22.04 LTS server 监控机
2 amdeuc-vm1 10.0.1.156 2 8 ubuntu 22.04 LTS server 被监控机
3 amdeuc-vm2 10.0.1.157 1 4 ubuntu 22.04 LTS server 被监控机
4 amdeuc-vm3 10.0.1.158 1 4 ubuntu 22.04 LTS server 被监控机



2. 在宿主机安装Prometheus

2.1 安装

个人觉得不用下载安装包这么复杂, 直接从ubuntu源安装

sudo apt-get install prometheus

检查prometheus 有没有启动

gateman@amdeuc-vm0:~$ systemctl status prometheus
● prometheus.service - Monitoring system and time series database
     Loaded: loaded (/lib/systemd/system/prometheus.service; enabled; vendor preset: enabled)
     Active: active (running) since Thu 2022-11-24 15:58:52 UTC; 4h 3min ago
       Docs: https://prometheus.io/docs/introduction/overview/
             man:prometheus(1)
   Main PID: 23080 (prometheus)
      Tasks: 7 (limit: 23310)
     Memory: 44.6M
        CPU: 30.744s
     CGroup: /system.slice/prometheus.service
             └─23080 /usr/bin/prometheus



2.2 检查配置文件
gateman@amdeuc-vm0:/etc/prometheus$ cat prometheus.yml 
# Sample config for Prometheus.

global:
  scrape_interval:     15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
  evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
  # scrape_timeout is set to the global default (10s).

  # Attach these labels to any time series or alerts when communicating with
  # external systems (federation, remote storage, Alertmanager).
  external_labels:
      monitor: 'example'

# Alertmanager configuration
alerting
<think>好的,我需要帮助用户搭建PrometheusGrafana服务器监控系统。首先,我要回顾用户之前的问题,之前他们询问了Zabbix是否需要付费,现在转向了PrometheusGrafana的组合,这可能是因为他们需要开源免费的解决方案,并且更关注云原生或容器化环境的监控。 用户的需求很明确:搭建服务器监控系统。Prometheus作为监控工具,负责数据采集存储,而Grafana则用于数据可视化。我需要确保步骤清晰,适合不同技术水平的用户,并且覆盖常见的问题点。 首先,我应该分步骤介绍安装配置过程。可能需要包括安装Prometheus、Node Exporter(用于服务器指标)、Grafana,然后配置数据源仪表盘。同时,用户可能需要了解如何设置服务自启动,以及如何验证各个组件是否正常工作。 要注意用户可能的潜在问题,比如防火墙设置、权限问题,或者配置文件的正确性。比如,Prometheus默认使用9090端口,Grafana用3000,而Node Exporter用9100,需要确保这些端口开放。此外,SELinux或AppArmor可能会阻止服务运行,需要提示用户检查或临时禁用。 另外,用户可能对PromQL不熟悉,所以在配置警报或自定义仪表盘时可能会遇到困难。不过用户问题主要是搭建,可能不需要深入PromQL,但可以提及Grafana的社区仪表盘,方便用户导入使用。 需要检查之前的对话,用户之前得到的是关于Zabbix的详细回答,包含代码示例表格,所以这次回答也应该保持类似结构,使用清晰的步骤代码块,可能还包括配置文件的示例。同时,要使用中文,避免专业术语过多,但必要时解释清楚。 最后,总结一下优势,比如开源、灵活性、适合云环境等,帮助用户理解选择Prometheus+Grafana的原因。可能还要提到与其他工具(如Alertmanager)的集成,但用户当前问题可能不需要深入,可以简要提及作为扩展建议。</think>以下是 **Prometheus + Grafana 服务器监控体系** 的完整搭建指南,涵盖从基础安装到高级配置的全流程: --- ### 一、架构原理 ```mermaid graph LR A[被监控服务器] -->|Node Exporter 暴露指标| B(Prometheus Server) B -->|抓取&存储时序数据| C{Grafana} C -->|可视化查询| D[监控仪表盘] C -->|告警规则触发| E[Alertmanager] ``` --- ### 二、基础环境搭建(以 Ubuntu 22.04 为例) #### 1. 安装 Prometheus ```bash # 创建专用用户 sudo useradd --no-create-home --shell /bin/false prometheus # 下载二进制包(版本可替换为最新) wget https://github.com/prometheus/prometheus/releases/download/v2.47.2/prometheus-2.47.2.linux-amd64.tar.gz tar xvf prometheus-*.tar.gz sudo cp prometheus-*/prometheus /usr/local/bin/ sudo cp prometheus-*/promtool /usr/local/bin/ # 配置文件 sudo mkdir /etc/prometheus sudo nano /etc/prometheus/prometheus.yml ``` **配置文件模板**: ```yaml global: scrape_interval: 15s scrape_configs: - job_name: 'prometheus' static_configs: - targets: ['localhost:9090'] - job_name: 'node' static_configs: - targets: ['被监控服务器IP:9100'] ``` #### 2. 安装 Node Exporter(每个被监控服务器都需要) ```bash wget https://github.com/prometheus/node_exporter/releases/download/v1.7.0/node_exporter-1.7.0.linux-amd64.tar.gz tar xvf node_exporter-*.tar.gz sudo cp node_exporter-*/node_exporter /usr/local/bin/ # 创建系统服务 sudo nano /etc/systemd/system/node_exporter.service ``` **服务文件内容**: ```ini [Unit] Description=Node Exporter [Service] User=node_exporter ExecStart=/usr/local/bin/node_exporter [Install] WantedBy=multi-user.target ``` --- ### 三、Grafana 集成 #### 1. 安装 Grafana ```bash sudo apt-get install -y apt-transport-https sudo apt-get install -y software-properties-common wget wget -q -O - https://packages.grafana.com/gpg.key | sudo apt-key add - echo "deb https://packages.grafana.com/oss/deb stable main" | sudo tee /etc/apt/sources.list.d/grafana.list sudo apt-get update sudo apt-get install grafana sudo systemctl start grafana-server sudo systemctl enable grafana-server ``` #### 2. 配置数据源 1. 访问 `http://服务器IP:3000` 使用 admin/admin 登录 2. 进入 **Configuration > Data Sources > Add data source** 3. 选择 Prometheus,URL 填写 `http://localhost:9090` #### 3. 导入仪表盘模板 推荐使用官方模板库: - **Node Exporter 仪表盘**:ID `1860` - **服务器综合监控**:ID `8919` 导入方式: ```text Grafana 首页 -> Create -> Import -> 输入模板ID -> Load ``` --- ### 四、高级配置技巧 #### 1. Prometheus 告警规则 ```yaml # 创建告警规则文件 sudo nano /etc/prometheus/alerts.yml # 示例:CPU使用率超过90%触发告警 groups: - name: host_stats rules: - alert: HighCpuUsage expr: 100 - (avg by(instance) (rate(node_cpu_seconds_total{mode="idle"}[5m])) * 100) > 90 for: 5m labels: severity: critical annotations: summary: "高CPU使用率 (实例 {{ $labels.instance }})" ``` #### 2. 配置 Alertmanager ```bash # 下载安装 wget https://github.com/prometheus/alertmanager/releases/download/v0.26.0/alertmanager-0.26.0.linux-amd64.tar.gz tar xvf alertmanager-*.tar.gz sudo cp alertmanager-*/alertmanager /usr/local/bin/ # 邮件告警配置示例 sudo nano /etc/alertmanager/alertmanager.yml ``` ```yaml route: receiver: 'email-notice' receivers: - name: 'email-notice' email_configs: - to: 'admin@example.com' from: 'alert@monitor.com' smarthost: 'smtp.example.com:587' auth_username: 'user' auth_password: 'password' ``` --- ### 五、性能优化建议 1. **存储优化** - 启用本地SSD存储 - 设置数据保留策略:`--storage.tsdb.retention.time=30d` 2. **高可用方案** ```mermaid graph TB A[Prometheus A] -->|联邦集群| C[中心Prometheus] B[Prometheus B] -->|联邦集群| C C --> Grafana ``` 3. **安全加固** - 启用HTTPS通信 - 配置访问控制列表 (ACL) - 定期备份 `prometheus.yml` 告警规则文件 --- ### 六、成本对比 | 方案 | 监控100节点年成本 | 核心优势 | |---------------------|-------------------|-----------------------------| | Prometheus+Grafana | $0 (自建硬件除外) | 完全开源/高度定制化 | | 商业监控软件 | $8,000+ | 开箱即用/SLA支持 | | 云平台自带监控 | $3,000+ | 深度云集成/免维护 | --- **总结**:该方案通过开源组件实现企业级监控能力,适合需要深度定制化且具备基础运维能力的团队。关键优势在于灵活的指标采集能力Grafana强大的可视化生态,但需要投入学习PromQL查询语言的时间成本。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

nvd11

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值