02 Prometheus入门安装教程
大家好,我是秋意零。今天分享一篇入门级Prometheus安装教程。
环境准备
三台Linux虚拟机(一台也可以)
准备Prometheus、相关组件安装包
Prometheus官网下载安装包比较慢,如果没有魔法。可关注公众号 【秋意零】回复101 获取
官网下载地址:https://prometheus.io/download/
上传/解压包
配置Hosts映射、上传安装包并进行解压
[root@node01 ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.200.7 node01
192.168.200.9 node02
192.168.200.10 node03
[root@node01 ~]# tar -vzxf prometheus-2.45.5.linux-amd64.tar.gz -C /usr/local/
[root@node01 ~]# tar -vzxf pushgateway-1.8.0.linux-amd64.tar.gz -C /usr/local/
[root@node01 ~]# tar -vzxf node_exporter-1.8.0.linux-amd64.tar.gz -C /usr/local/
配置Prometheus
1)进入Prometheus家目录,配置prometheus.yml文件
[root@node01 prometheus-2.45.5.linux-amd64]# 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:
# - 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"]
# 添加下列配置
- job_name: "pushgateway"
static_configs:
- targets: ["localhost:9091"]
labels:
instance: pushgateway
- job_name: "node exporter"
static_configs:
- targets: ["node01:9100","node02:9100","node03:9100"]
2)scp复制node_exporter(一个节点可不做)
[root@node01 local]# scp -r node_exporter-1.8.0.linux-amd64/ node02:/usr/local/
[root@node01 local]# scp -r node_exporter-1.8.0.linux-amd64/ node03:/usr/local/
3)启动node_exporter(三个节点都启动)
[root@node01 local]# cd node_exporter-1.8.0.linux-amd64/
[root@node01 node_exporter-1.8.0.linux-amd64]# nohup ./node_exporter &
浏览器访问
node_exporter服务地址:http://192.168.200.7:9100/
node_exporter服务监控指标地址:http://192.168.200.7:9100/metrics,可以看到当前 node exporter 获取到的当前主机的所有监控数据
4)启动Pushgateway、Prometheus
[root@node01 pushgateway-1.8.0.linux-amd64]# nohup ./pushgateway --web.listen-address :9091 > ./pushgateway.log 2>&1 &
[root@node01 prometheus-2.45.5.linux-amd64]# nohup ./prometheus --config.file=prometheus.yml > ./prometheus.log 2>&1 &
浏览器访问
pushgateway服务地址:http://192.168.200.7:9091
prometheus服务地址:http://192.168.200.7:9090/
查看组件状态是否正常
启停脚本
由于,咱们Prometheus服务以及相关组件,通过二进制命令手动启动的,不便于管理。所以我们可以编写Shell启停脚本管理。如下:
注意:服务的路径需要根据自己的存放位置自行修改
[root@node01 ~]# cat /usr/local/bin/moniter
#!/bin/bash
case $1 in
"start"){
echo "=== 启动 Prometheus ==="
nohup /usr/local/prometheus-2.45.5.linux-amd64/prometheus --config.file=/usr/local/prometheus-2.45.5.linux-amd64/prometheus.yml > /usr/local/prometheus-2.45.5.linux-amd64/prometheus.log 2>&1 &
echo "=== 启动 Node_exporter ==="
nohup /usr/local/node_exporter-1.8.0.linux-amd64/node_exporter > /usr/local/node_exporter-1.8.0.linux-amd64/node_exporter.log 2>&1 &
echo "=== 启动 Pushgateway ==="
nohup /usr/local/pushgateway-1.8.0.linux-amd64/pushgateway --web.listen-address :9091 > /usr/local/pushgateway-1.8.0.linux-amd64/pushgateway.log 2>&1 &
};;
"stop"){
echo "=== 停止 Pushgateway ==="
pgrep -f pushgateway | xargs kill
echo "=== 停止 Node_exporter ==="
pgrep -f node_exporter | xargs kill
echo "=== 停止 Prometheus ==="
pgrep -f prometheus | xargs kill
};;
esac
执行脚本
[root@node01 ~]# chmod +x /usr/local/bin/moniter
[root@node01 ~]# moniter stop
=== 停止 Pushgateway ===
=== 停止 Node_exporter ===
=== 停止 Prometheus ===
[root@node01 ~]#
[root@node01 ~]#
[root@node01 ~]# moniter start
=== 启动 Prometheus ===
=== 启动 Node_exporter ===
=== 启动 Pushgateway ===
更多运维干货,关注下方公众号