1 下载 Prometheus
$ wget https://github.com/prometheus/prometheus/releases/download/v2.41.0/prometheus-2.41.0.linux-amd64.tar.gz
$ tar xf prometheus-2.41.0.linux-amd64.tar.gz -C /data
$ mv /data/prometheus-2.41.0.linux-amd64 /data/prometheus
2 配置 Prometheus
$ cd /data/prometheus
$ vim prometheus.yml
# my global config
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).
# Alertmanager configuration
alerting:
alertmanagers:
- static_configs:
- targets:
# - alertmanager:9093
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
# - "first_rules.yml"
# - "second_rules.yml"
# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: 'prometheus'
scrape_interval: 60s
static_configs:
- targets:
- 'localhost:29090'
labels:
hostname: Prometheus-Server
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
static_configs:
- targets: ["localhost:29090"]
3 创建 Prometheus 服务
$ vim /usr/lib/systemd/system/prometheus.service
[Unit]
Description=https://prometheus.io
[Service]
Restart=on-failure
ExecStart=/data/prometheus/prometheus --config.file=/data/prometheus/prometheus.yml --web.listen-address=:29090 --web.max-connections=512 --storage.tsdb.path="/data/prometheus/data/" --storage.tsdb.retention=30d --web.enable-lifecycle
[Install]
WantedBy=multi-user.target
- 指定配置文件 --config.file=/data/prometheus/prometheus.yml
- 默认指定监听端口为9090,可使用 --web.listen-address=:29090 修改默认端口
- 最大连接数 --web.max-connections=512
- tsdb数据存储的目录,默认在data目录下,可使用 --storage.tsdb.path="/data/prometheus/data/" 指定目录
- premetheus 存储数据的时间,默认保存15天,可使用 --storage.tsdb.retention=30 设置保存时间
- --web.enable-lifecycle参数用户开启热加载,通过命令热加载无需重启 curl -XPOST 127.0.0.1:29090/-/reload --web.enable-lifecycle
启动选项了解:./prometheus --help
4 启动、开机自启
$ systemctl start prometheus
$ systemctl enable prometheus
5 访问测试
浏览器访问:http://[IP]:29090