1. 安装与配置Prometheus服务器
任务目标
在本机安装Prometheus服务器,配置一个基本的抓取任务(scrape job),使Prometheus能够抓取自身暴露的监控指标,并通过Web UI验证目标(Target)状态为UP且指标数据可查询。
环境与依赖
- 操作系统:Linux(本文以Ubuntu 22.04为例)或macOS
- 已安装
wget或curl用于下载 - 已安装
tar用于解压 - 浏览器用于访问Prometheus Web UI
安装Prometheus
从Prometheus官网下载最新的稳定版(以v2.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可执行文件和一个默认的prometheus.yml配置文件。
配置基本抓取任务
编辑prometheus.yml,配置Prometheus抓取自身指标。默认配置已包含名为prometheus的job,其targets指向localhost:9090。确认配置文件内容如下:
global:
scrape_interval: 15s
scrape_configs:
- job_name: "prometheus"
static_configs:
- targets: ["localhost:9090"]
启动Prometheus
在解压目录下执行以下命令启动服务:
./prometheus --config.file=prometheus.yml
启动成功后,终端会输出日志,其中包含Server is ready to receive web requests字样,表示服务已就绪。
验证方法与判定标准
完成以下两步验证,全部满足即判定安装与配置成功:
- 验证目标状态:浏览器访问
http://localhost:9090/targets,在页面中可以看到名为prometheus的job,其Endpoint为http://localhost:9090/metrics,State列为UP。 - 验证指标采集:浏览器访问
http://localhost:9090/graph,在查询框中输入up,点击Execute按钮。预期输出为一行结果:up{instance="localhost:9090", job="prometheus"} 1。其中1表示Prometheus自身处于健康状态。
常见问题
- 端口被占用:若9090端口已被占用,启动会失败。可通过
--web.listen-address=:9091参数更换端口,同时更新配置文件中targets的端口。 - 配置文件语法错误:启动时若提示配置文件解析失败,检查YAML缩进,确保
scrape_configs下的每个job使用两个空格缩进。