乐趣区

关于prometheus:Prometheus部署及nodeexporter部署方案

筹备工作

1、创立用户和配置环境参数

(1)、创立用户和创立所需目录

[root@centos ~]# groupadd prometheus
[root@centos ~]# useradd -d /home/prometheus -g prometheus -m prometheus
[root@centos ~]# chmod 755 /home/prometheus
[root@centos ~]# mkdir -p /home/prometheus/software
[root@centos ~]# mkdir -p /home/prometheus/yunwei
[root@centos ~]# chown -R prometheus:prometheus /home/prometheus
[root@centos ~]# mkdir -p /data/prometheus
[root@centos ~]# chown -R prometheus:prometheus /data/prometheus

(2)、下载

https://github.com/prometheus/prometheus/releases/download/v2.42.0/prometheus-2.42.0.linux-amd64.tar.gz

部署计划

1、部署

(1)、敞开防火墙

[root@centos ~]# systemctl stop firewalld
[root@centos ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@centos ~]# setenforce 0
[root@centos ~]# getenforce 
Permissive
[root@centos ~]# vi /etc/sysconfig/selinux 

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected.
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted

(2)、解压安装包并备份配置文件

[prometheus@centos ~]$ tar zxf $HOME/software/prometheus-2.42.0.linux-amd64.tar.gz -C $HOME
[prometheus@centos ~]$ mv prometheus-2.42.0.linux-amd64 prometheus-2.42.0
[prometheus@centos ~]$ cp prometheus-2.42.0/prometheus.yml prometheus-2.42.0/prometheus.yml_init

(3)、创立所需目录

[prometheus@centos ~]$ mkdir -p /data/prometheus/prometheus-2.42.0/logs
[prometheus@centos ~]$ mkdir -p /data/prometheus/prometheus-2.42.0/data

2、调整配置文件

[prometheus@centos ~]$ vi $HOME/prometheus-2.42.0/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"

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.

    static_configs:
      - targets: ["192.168.19.149:9090"]

3、启停服务与创立脚本

(1)、查看 prometheus 版本信息

# 查看版本信息
[prometheus@centos ~]$ $HOME/prometheus-2.42.0/prometheus --version
prometheus, version 2.42.0 (branch: HEAD, revision: 225c61122d88b01d1f0eaaee0e05b6f3e0567ac0)
  build user:       root@c67d48967507
  build date:       20230201-07:53:32
  go version:       go1.19.5
  platform:         linux/amd64

(2)、失常启动命令及参数抉择

# 启动服务
[prometheus@centos ~]$ $HOME/prometheus-2.42.0/prometheus --config.file=prometheus.yml
# 指定配置文件
--config.file="prometheus.yml"
# 默认指定监听地址端口,可批改端口
--web.listen-address="0.0.0.0:9090"
# 开启 API 服务(为管理控制操作启用 API 端点。)--web.enable-admin-api
# 最大连接数
--web.max-connections=512
# tsdb 数据存储的目录,默认以后 data/
--storage.tsdb.path="data/"
# premetheus 存储数据的工夫,默认保留 15 天
--storage.tsdb.retention.time=365d
#通过命令热加载无需重启 curl -XPOST 192.168.19.149:9090/-/reload
--web.enable-lifecycle
# 能够启用 TLS 或身份验证 的配置文件的门路
--web.config.file=""
# 启动选项理解:
[prometheus@centos ~]$ $HOME/prometheus-2.42.0/prometheus --help

(3)、启动脚本配置

[prometheus@centos ~]$ vi $HOME/yunwei/prometheus-2.42.0_start.sh
#!/bin/bash
#
cd $HOME/prometheus-2.42.0
nohup ./prometheus --config.file="prometheus.yml" --web.max-connections=512 --storage.tsdb.path="/data/prometheus/prometheus-2.42.0/data/" --storage.tsdb.retention.time=365d --web.enable-lifecycle --web.enable-admin-api >> /data/prometheus/prometheus-2.42.0/logs/prometheus-2.42.0.log 2>&1 &

tail -f /data/prometheus/prometheus-2.42.0/logs/prometheus-2.42.0.log

(4)、进行脚本配置

[prometheus@centos ~]$ vi $HOME/yunwei/prometheus-2.42.0_stop.sh
#!/bin/bash
#
prom_pid=`ps -ef|grep prometheus|grep storage.tsdb.retention.time|grep -v grep|awk '{print $2}'`
kill -9 $prom_pid

(5)、热加载重启配置

[prometheus@centos ~]$ vi $HOME/yunwei/prometheus-2.42.0_reload.sh
#!/bin/bash
#
curl -XPOST 192.168.19.149:9090/-/reload

治理配置

1、创立目录

[prometheus@centos ~]$ mkdir $HOME/exporter
[prometheus@centos ~]$ mkdir -p $HOME/yunwei/exporter/node
[prometheus@centos ~]$ mkdir -p /data/prometheus/exporter/node_exporter_1.5.0/logs

2、下载

https://github.com/prometheus/node_exporter/releases/download/v1.5.0/node_exporter-1.5.0.linux-amd64.tar.gz

3、部署 node_exporter

[prometheus@centos ~]$ tar zxf $HOME/software/node_exporter-1.5.0.linux-amd64.tar.gz -C $HOME/exporter
[prometheus@centos ~]$ mv $HOME/exporter/node_exporter-1.5.0.linux-amd64 $HOME/exporter/node_exporter

4、查看 node_exporter 版本及参数

# 查看版本
[prometheus@centos node_exporter]$ $HOME/exporter/node_exporter/node_exporter --version
node_exporter, version 1.5.0 (branch: HEAD, revision: 1b48970ffcf5630534fb00bb0687d73c66d1c959)
  build user:       root@6e7732a7b81b
  build date:       20221129-18:59:09
  go version:       go1.19.3
  platform:         linux/amd64

# 查看相干参数
[prometheus@centos ~]$ $HOME/node_exporter --help
# 申请 URL 门路地址
--web.telemetry-path="/metrics"
# 最大申请数量
--web.max-requests=40
# web 监听端口地址
--web.listen-address=:9100
# 指定配置文件
--web.config.file=""

5、配置 prometheus.yml

[prometheus@centos ~]$ vi prometheus-2.42.0/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"

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.

    static_configs:
      - targets: ["192.168.19.149:9090"]
      
# 新增 job_name 并增加治理主机参数
  - job_name: "node_exporter"
    static_configs:
      - targets: ['192.168.19.149:9100']
        labels:
          host_ip: 192.168.19.149

6、脚本配置

(1)、启动脚本

[prometheus@centos ~]$ vi $HOME/yunwei/exporter/node/node_exporter_1.5.0_start.sh
#!/bin/bash
#
cd $HOME/exporter/node_exporter
./node_exporter --web.listen-address=:9100 >> /data/prometheus/exporter/node_exporter_1.5.0/logs/node_exporter.log &

(2)、进行脚本

[prometheus@centos ~]$ vi $HOME/yunwei/exporter/node/node_exporter_1.5.0_stop.sh
#!/bin/bash
#
node_exp_pid=`ps -ef|grep node_exporter|grep 9100|awk '{print $2}'`
kill -9 $node_exp_pid
退出移动版