容器方式(docker-compose)
1、目录规划及配置文件准备(根据自己实际情况调整)
mkdir -p /data/storage/prometheus/data
cd /data/storage/prometheus
cat >prometheus.yml<<EOF
global:
scrape_interval: 15s # 默认抓取周期
external_labels:
monitor: 'codelab-monitor'
alerting:
alertmanagers:
- static_configs:
- targets:
#- 192.168.1.58:9093
rule_files:
#- "/prime/prometheus/rules/node_down_alerts.yml"
#- "*rules.yml"
#- "rules/*_rules.yml"
#- "rules/*_alerts.yml"
scrape_configs:
- job_name: 'node_service_discovery' # 这段配置的是基于文件的服务发现
file_sd_configs:
- files:
- node_targets/*.yaml # node_targets目录与prometheus.yml同级
refresh_interval: 60m
- files:
- node_targets/*.json
refresh_interval: 60m
- job_name: 'node' # 服务的名称
scrape_interval: 5s
metrics_path: /metrics #获取指标的url
static_configs:
#- targets: ['172.30.11.7:9100'] # 这个为监听指定服务服务的ip和port,需要修改为自己的ip
EOF
2、准备docker-compose.yml文件(根据自己实际情况调整挂载的目录和端口)
version: "3.7"
services:
prometheus:
image: prom/prometheus:v2.36.1
container_name: "prometheus"
restart: always
user: root
ports:
- "9090:9090"
volumes:
- "/data/storage/prometheus:/etc/prometheus/"
- "/data/storage/prometheus/data:/prometheus"
command:
- '--config.file=/etc/prometheus/prometheus.yml'
- '--storage.tsdb.path=/prometheus'
- '--web.console.libraries=/usr/share/prometheus/console_libraries'
- '--web.console.templates=/usr/share/prometheus/consoles'
- '--web.enable-lifecycle'
二进制安装方式
下载安装包
https://prometheus.io/download/
根据系统环境,下载对应的二进制包。然后解压即可
将Prometheus配置为系统服务
1、进入 /``usr/lib/systemd/system/
目录
cd /usr/lib/systemd/system/
2、创建系统服务文件
path=/data/service/prometheus/prometheus
config_path=${path}/prometheus.yaml
consoles_path=${path}/consoles
consoles_libraries_path=${path}/consoles_libraries
cat >> promentheus.service <<EOF
[Unit]
Description=https://prometheus.io
After=network.target
[Service]
Restart=on-failure
ExecStart=/usr/local/prometheus/prometheus/ \
--config.file=/usr/local/prometheus/prometheus.yml \
[Install]
WantedBy=multi-user.target
EOF
# vim /etc/systemd/system/prometheus.service
[Unit]
Description=Prometheus
Documentation=https://prometheus.io/
After=network.target
[Service]
Type=simple
User=prometheus
ExecStart=/export/prometheus/prometheus --config.file=/export/prometheus/prometheus.yml --storage.tsdb.path=/export/prometheus/data
Restart=on-failure
[Install]
WantedBy=multi-user.target
3、生效系统system文件
systemctl daemon-reload
4、启动服务
systemctl start prometheus