Prometheus之安装部署
1. 准备工作与安装
1. ntp时间同步和时区
/usr/sbin/ntpdate us.pool.ntp.org 同步时间
timedatectl set-timezone Asia/Shanghai 同步时区
2. 下载prometheus
官网地址:https://github.com/prometheus/prometheus/releases
wget https://github.com/prometheus/prometheus/releases/download/v2.21.0/prometheus-2.21.0.linux-amd64.tar.gz
3. 解压prometheus-2.21.0.linux-amd64.tar.gz
tar -zxvf prometheus-2.21.0.linux-amd64.tar.gz
4. 复制cp -rf prometheus-2.21.0.linux-amd64 /usr/local/prometheus
5. 检查配置文件
./promtool check config ./prometheus.yml
6. 环境变量
/etc/profile文件最后一行,添加以下内容
export PROMETHEUS=/usr/local/prometheus
export PATH=$PATH:$PROMETHEUS
source /etc/profile
2. 配置文件
# 全局配置global:
# 默认抓取周期,可用单位ms、smhdwy #设置每15s采集数据一次,默认1分钟
[ scrape_interval: <duration> | default = 1m ]
# 默认抓取超时
[ scrape_timeout: <duration> | default = 10s ]
# 估算规则的默认周期 # 每15秒计算一次规则。默认1分钟
[ evaluation_interval: <duration> | default = 1m ]
# 和外部系统(例如AlertManager)通信时为时间序列或者警情(Alert)强制添加的标签列表
external_labels:
[ <labelname>: <labelvalue> ... ]
# 规则文件列表
rule_files:
[ - <filepath_glob> ... ]
# 抓取配置列表
scrape_configs:
[ - <scrape_config> ... ]
# Alertmanager相关配置
alerting:
alert_relabel_configs:
[ - <relabel_config> ... ]
alertmanagers:
[ - <alertmanager_config> ... ]
# 远程读写特性相关的配置
remote_write:
[ - <remote_write> ... ]
remote_read:
[ - <remote_read> ... ]
3. 服务管理
启动服务:./prometheus --config.file=prometheus.yml
启动参数
# 指定配置文件 --config.file="prometheus.yml" # 指定监听地址端口 --web.listen-address="0.0.0.0:9090" # 最大连接数 --web.max-connections=512 # tsdb数据存储的目录,默认当前data/ --storage.tsdb.path="data/" # premetheus 存储数据的时间,默认保存15天 --storage.tsdb.retention=15d 启动选项了解:./prometheus --help
4. 测试访问
http://172.16.1.198:9090/metrics
5. 作成系统服务
1. 在/usr/lib/systemd/system目录下,创建一个文件prometheus.service
[Unit] Description=https://prometheus.io [Service] Restart=on-failure ExecStart=/usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/prometheus.yml --storage.tsdb.path="data/" --storage.tsdb.retention=30d [Install] WantedBy=multi-user.target
2. 可以对prometheus.service进行操作
systemctl stop prometheus.service
systemctl start prometheus.service
systemctl status prometheus.service
6. 样例
global:
scrape_interval: 15s
evaluation_interval: 15s
# Alertmanager configuration
alerting:
alertmanagers:
- static_configs:
- targets:
- 172.16.1.198:9093
rule_files:
# - "rules/*.yml"
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']
- job_name: 'node'
file_sd_configs:
- files: ['/usr/local/prometheus/sd_config/node.yml']
refresh_interval: 5s
- job_name: 'mysql'
static_configs:
- targets: ['172.16.1.209:9104']
7. Docker容器安装部署
docker run -p 9090:9090 -v /tmp/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus
docker run -p 9090:9090 -v /prometheus-data prom/prometheus --config.file=/prometheus-data/prometheus.yml

浙公网安备 33010602011771号