容器方式(docker-compose)

1、目录规划及配置文件准备(根据自己实际情况调整)

  1. mkdir -p /data/storage/prometheus/data
  2. cd /data/storage/prometheus
  3. cat >prometheus.yml<<EOF
  4. global:
  5. scrape_interval: 15s # 默认抓取周期
  6. external_labels:
  7. monitor: 'codelab-monitor'
  8. alerting:
  9. alertmanagers:
  10. - static_configs:
  11. - targets:
  12. #- 192.168.1.58:9093
  13. rule_files:
  14. #- "/prime/prometheus/rules/node_down_alerts.yml"
  15. #- "*rules.yml"
  16. #- "rules/*_rules.yml"
  17. #- "rules/*_alerts.yml"
  18. scrape_configs:
  19. - job_name: 'node_service_discovery' # 这段配置的是基于文件的服务发现
  20. file_sd_configs:
  21. - files:
  22. - node_targets/*.yaml # node_targets目录与prometheus.yml同级
  23. refresh_interval: 60m
  24. - files:
  25. - node_targets/*.json
  26. refresh_interval: 60m
  27. - job_name: 'node' # 服务的名称
  28. scrape_interval: 5s
  29. metrics_path: /metrics #获取指标的url
  30. static_configs:
  31. #- targets: ['172.30.11.7:9100'] # 这个为监听指定服务服务的ip和port,需要修改为自己的ip
  32. 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/
image.png
根据系统环境,下载对应的二进制包。然后解压即可

将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