1. 安装与配置Prometheus服务器
任务目标
在本机安装并启动Prometheus服务器,通过配置文件定义基本的抓取任务,最终在Web UI中确认目标状态为UP且能够查询到采集的指标数据。
环境准备
本案例以 Linux/macOS 环境为例,需要能够访问互联网以下载Prometheus二进制包。无需额外依赖,Prometheus为单二进制文件,开箱即用。
安装与启动
下载Prometheus最新稳定版(以2.53.0为例)并解压:
wget https://github.com/prometheus/prometheus/releases/download/v2.53.0/prometheus-2.53.0.linux-amd64.tar.gz
tar xvf prometheus-2.53.0.linux-amd64.tar.gz
cd prometheus-2.53.0.linux-amd64
解压目录中已包含默认的 prometheus.yml 配置文件,可直接启动:
./prometheus --config.file=prometheus.yml
启动后,Prometheus默认监听在 9090 端口。访问 http://localhost:9090 可打开Web UI。
配置抓取任务
编辑 prometheus.yml,配置一个抓取任务:抓取Prometheus自身暴露的指标,并添加一个模拟的本地目标(使用node_exporter作为示例,若未安装可仅保留Prometheus自身)。以下为最小可用配置:
global:
scrape_interval: 15s
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']
- job_name: 'node'
static_configs:
- targets: ['localhost:9100']
若本机未运行node_exporter,第二个job会显示为DOWN,这属于预期现象,不影响验证Prometheus自身抓取。如需完整验证两个目标均为UP,可另开终端下载并启动node_exporter:
wget https://github.com/prometheus/node_exporter/releases/download/v1.8.2/node_exporter-1.8.2.linux-amd64.tar.gz
tar xvf node_exporter-1.8.2.linux-amd64.tar.gz
cd node_exporter-1.8.2.linux-amd64
./node_exporter
验证方法
- 目标状态检查:浏览器打开
http://localhost:9090/targets,确认配置的job对应的Endpoint状态为UP(若未启动node_exporter,则该目标显示DOWN,Prometheus自身目标必须为UP)。 - 指标查询验证:在Web UI的
Graph页面输入prometheus_tsdb_head_samples_appended_total或up,点击Execute。若返回时间序列数据点,说明指标采集链路正常。 - 命令行验证(可选):执行
curl http://localhost:9090/api/v1/query?query=up,预期返回JSON格式结果,其中value为1表示目标健康。
通过条件:Prometheus自身目标状态为UP,且通过PromQL查询 up{job="prometheus"} 返回值为1,即表示安装配置成功。
常见问题
- 端口被占用:若9090被占用,启动时添加
--web.listen-address=:9091更换端口,并相应修改访问地址。 - 配置语法错误:启动时若输出
Error parsing config,使用./promtool check config prometheus.yml校验配置格式。 - 抓取超时:若目标长时间显示DOWN,检查目标端口是否可达,或使用
curl http://localhost:9100/metrics确认node_exporter是否正常返回指标。