配置yum源
yum -y install wget
cd /etc/yum.repos.d/
wget http://mirrors.163.com/.help/CentOS7-Base-163.repo
wget -O /etc/yum.repos.d/epel-7.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum clean all
yum makecache
yum -y install lrzsz
配置prometheus
[root@localhost opt]# dir
prometheus-2.18.1.linux-amd64.tar.gz
[root@localhost opt]# tar -zxvf prometheus-2.18.1.linux-amd64.tar.gz
[root@localhost opt]# cp -r prometheus-2.18.1.linux-amd64 /usr/local/prometheus
[root@jgxxpt-nginx2 prometheus]# cat 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:
- 192.168.145.11:9093
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
- "rules/*_rules.yml"
- "rules/*_alerts.yml"
# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
static_configs:
- targets: ["localhost:9090"]
- targets: ["localhost:9093"]
- job_name: "node_exporter"
file_sd_configs:
- files: ['/usr/local/prometheus/sd_config/*.yml']
# 每过5秒动态发现服务配置
refresh_interval: 5s
设置systemd服务
[root@localhost ~]# cat /etc/systemd/system/prometheus.service
[Unit]
Description=Prometheus server daemon
After=network.target
[Service]
Type=simple
User=root
Group=root
ExecStart=/usr/local/prometheus/prometheus \
--config.file=/usr/local/prometheus/prometheus.yml \
--web.enable-lifecycle \
--storage.tsdb.path="/usr/local/prometheus/data" \
--storage.tsdb.retention=15d \
--web.console.templates="/usr/local/prometheus/consoles" \
--web.console.libraries="/usr/local/prometheus/console_libraries" \
--web.max-connections=512 \
--web.external-url "http://192.168.1.4:9090" \
--web.listen-address=0.0.0.0:9090
Restart=on-failure
[Install]
WantedBy=multi-user.target
参数说明:
ExecStart=/usr/local/prometheus/prometheus #启动运行prometheus程序所在的路径
--config.file=/usr/local/prometheus/prometheus.yml #指定prometheus.yml配置文件路径
--storage.tsdb.path="/usr/local/prometheus/data" #指定监控指标数据存储的路径
--storage.tsdb.retention=15d #历史数据最大保留时间,默认15天
--web.console.templates="/usr/local/prometheus/consoles" #指定控制台模板目录路径
--web.console.libraries="/usr/local/prometheus/console_libraries" #指定控制台库目录路径
--web.max-connections=512 #设置最大同时连接数
--web.external-url "http://192.168.1.4:9090" #用于生产返回prometheus相对的绝对链接地址,可以在后续告警通知内容中直接点击链接地址访问prometheus Web UI。其格式为:http://{ip或者域名}:9090
--web.listen-address=0.0.0.0:9090 #prometheus默认监控端口
sudo systemctl daemon-reload
sudo systemctl enable --now prometheus
systemctl命令介绍:
systemctl daemon-reload #通知systemctl重新加载配置文件
systemctl enable prometheus.service #设置为开机自启动
systemctl disable prometheus.service #如果不想设置为开机启动,可以关闭。关闭开机自启动
systemctl start prometheus.service #开启服务
systemctl status prometheus.service #查看服务状态
systemctl restart prometheus.service #重启服务
systemctl stop prometheus.service #关闭服务
常见问题
1.利用promtool工具校验配置正确性
promtool是Prometheus的一个命令行工具,它提供了一些功能来帮助用户进行Prometheus配置文件(如prometheus.yml)的检查、规则检查和调试。
解释
promtool check config: 验证Prometheus配置文件的语法和设置。
promtool命令:
Prometheus自带了一个工具promtool,可以用来检查Prometheus配置文件的语法和语义错误。你可以使用以下命令:
promtool check config /path/to/prometheus.yml
这个工具会检查配置文件的格式和有效性,并给出相应的错误或警告信息。
[root@prod prometheus]# ./promtool check config /usr/local/prometheus/prometheus.yml
Checking /usr/local/prometheus/prometheus.yml
FAILED: parsing YAML file /usr/local/prometheus/prometheus.yml: yaml: line 32: did not find expected key
[root@prod prometheus]# vim prometheus.yml
[root@prod prometheus]# ./promtool check config /usr/local/prometheus/prometheus.yml
Checking /usr/local/prometheus/prometheus.yml
SUCCESS: /usr/local/prometheus/prometheus.yml is valid prometheus config file syntax
2.Lifecycle API is not enabled.#
prometheus启动后修改配置文件就需要再重启生效
可以通过以下方式 热加载
curl -X POST http://localhost:9090/-/reload
请求接口后返回 Lifecycle API is not enabled. 那么就是启动的时候没有开启热更新配置,需要在启动的命令行增加参数: --web.enable-lifecycle