prometheus+node-exporter二进制安装使用
1.prometheu的安装配置使用
1.1下载prometheus的二进制包
下载地址:https://github.com/prometheus/prometheus/releases/download/v2.33.0/prometheus-2.33.0.linux-amd64.tar.gz
mkdir -p /data/
tar xvf prometheus-2.33.0.linux-amd64.tar.gz
ln -s /data/prometheus-2.33.0.linux-amd64 /data/prometheus
cd prometheus
[root@master-etcd1-234-31 prometheus]# ll
total 196060
drwxr-xr-x 2 3434 3434 38 Jan 30 2022 console_libraries
drwxr-xr-x 2 3434 3434 173 Jan 30 2022 consoles
-rw-r--r-- 1 3434 3434 11357 Jan 30 2022 LICENSE
-rw-r--r-- 1 3434 3434 3773 Jan 30 2022 NOTICE
-rwxr-xr-x 1 3434 3434 104419069 Jan 30 2022 prometheus
-rw-r--r-- 1 3434 3434 934 Jan 30 2022 prometheus.yml
-rwxr-xr-x 1 3434 3434 96325624 Jan 30 2022 promtool
1.2安装启动配置
配置文件为解压后当前目录会包含默认的Prometheus配置文件promethes.yml,下面配置文件做下简略的解析:
# 全局配置
global:
scrape_interval: 15s # 设置抓取间隔,默认为1分钟
evaluation_interval: 15s #估算规则的默认周期,每15秒计算一次规则。默认1分钟
# scrape_timeout #默认抓取超时,默认为10s
# Alertmanager相关配置
alerting:
alertmanagers:
- static_configs:
- targets:
# - alertmanager:9093
# 规则文件列表,使用'evaluation_interval' 参数去抓取
rule_files:
# - "first_rules.yml"
# - "second_rules.yml"
# 抓取配置列表
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']
1.3创建prometheus的用户及数据存储目录
为了安全,使用普通用户来启动prometheus服务。作为一个时序型的数据库产品,prometheus的数据默认会存放在应用所在目录下。
useradd -s /sbin/nologin -M prometheus
#创建数据目录
mkdir /data/prometheus/data -p
#修改目录属主
chown -R prometheus:prometheus /data/prometheus/
1.4prometheus的启动很简单,只需要直接启动解压目录的二进制文件prometheus即可,但是为了更加方便对prometheus进行管理,这里使用systemd来启停prometheus。
vim /lib/systemd/system/prometheus.service
[Unit]
Description=Prometheus
Documentation=https://prometheus.io/
After=network.target
[Service]
Type=simple
User=prometheus
ExecStart=/data/prometheus/prometheus --config.file=/data/prometheus/prometheus.yml --storage.tsdb.path=/data/prometheus/data
Restart=on-failure
[Install]
WantedBy=multi-user.target
编辑配置文件添加要监控的主机
# 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: ["localhost:9090"]
#prometheus-node
- job_name: "prometheus-node"
static_configs: #静态添加监控node
- targets: ["192.168.234.41:9100","192.168.234.42:9100","192.168.234.43:9100"]
修改完成重启服务
systemctl start prometheus
systemctl enable prometheus
2.监控软件node-exporter二进制安装方法
2.1下载软件包
wget https://github.com/prometheus/node_exporter/releases/download/v1.3.1/node_exporter-1.3.1.linux-amd64.tar.gz
tar xvf node_exporter-1.3.1.linux-amd64.tar.gz
ln -s node_exporter-1.3.1.linux-amd64.tar.gz node_exporter
#封装systemd以便于开机自启
vim /lib/systemd/system/node_exporter.service
[Unit]
Description=Prometheus Node Exporter
After=network.target
[Service]
ExecStart=/apps/node_exporter/node_exporter
User=prometheus
[Install]
WantedBy=multi-user.target
2.2配置启动.
systemctl start node_exporter
systemctl enable node_exporter
