一、Prometheus-V2.44.0安装
1、下载、解压软件
wget https://github.com/prometheus/prometheus/releases/download/v2.43.0/prometheus-2.44.0.linux-amd64.tar.gz
mkdir -p /opt/prometheus
tar xvf prometheus-2.44.0.linux-amd64.tar.gz -C /opt/prometheus
mv /opt/prometheus/prometheus-2.44.0.linux-amd64/* /opt/prometheus
2、prometheus.yml配置文件解析
#grep -Ev '^#|^$排除空格和注释
cat /opt/prometheus/prometheus.yml | grep -Ev '^#|^$'
global:
scrape_interval: 15s # 每15秒抓取一次。默认值为每1分钟
evaluation_interval: 15s # 每15秒评估一次规则。默认值为每1分钟
# scrape_timeout is set to the global default (10s). # 捉取的超时时间
alerting: # 指定 prometheus 将报警规则推送到指定的alertmanager 实例地址
alertmanagers:
- static_configs:
- targets:
# - alertmanager:9093
rule_files: # prometheus 根据这些规则信息,会推送报警信息到alertmanager中。
# - "first_rules.yml"
# - "second_rules.yml"
scrape_configs: # 一个 scrape_config 片段指定一组目标和参数, 目标就是实例,指定采集的端点
# 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"]
# 目前保持默认配置文件即可
3、环境变量配置
cat >/etc/profile.d/prometheus.sh<<'EOF'
#!/bin/bash
export PROMETHEUS_HOME=/opt/prometheus
export PATH=$PATH:${PROMETHEUS_HOME}/bin
EOF
source /etc/profile.d/prometheus.sh
chmod +x /etc/profile.d/prometheus.sh
4、设置systemd服务
cat > /usr/lib/systemd/system/prometheus.service <<'EOF'
[Unit]
Description=prometheus server project
After=network.target
[Service]
Type=simple
ExecStart=/opt/prometheus/prometheus \
--config.file=/opt/prometheus/prometheus.yml \
--storage.tsdb.path=/opt/prometheus/data \
--web.enable-lifecycle
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
mkdir -p /opt/prometheus/data
#环境变量
#临时修改
#PATH=/opt/prometheus:$PATH
#永久修改
vim /root/.bashrc
PATH=/opt/prometheus:$PATH
# 注意:其它的参数,我们可以基于prometheus --help 查看更多
5、启动服务
systemctl daemon-reload
systemctl start prometheus.service
systemctl enable prometheus.service
systemctl status prometheus.service -l
netstat -tunlp | grep -i prom
#tcp6 0 0 :::9090 :::* LISTEN 16825/prometheus
http://192.168.32.60:9090/metrics
6、总结
#Prometheus部署:软件解压、环境变量配置、配置文件修改、服务配置、启动服务
#访问prometheus
http://192.168.32.60:9090
#把Prometheus做成一个服务来进行管理,使之更加方便
vim /usr/lib/systemd/system/prometheus.service
#创建一个prometheus.service文件
[Unit]
Description=prometheus
[Service]
ExecStart=/opt/prometheus/prometheus --config.file=/opt/prometheus/prometheus.yml \
--web.enable-lifecycle \
--storage.tsdb.path=/opt/prometheus/data
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
[Install]
WantedBy=multi-user.target
--------------------------------------------------------------------------------------------------------------------------------
#Prometheus启动参数说明
#--config.file – 指明prometheus的配置文件路径
#--web.enable-lifecycle – 指明prometheus配置更改后可以进行热加载
#--storage.tsdb.path – 指明监控数据存储路径
#--storage.tsdb.retention --指明数据保留时间
———————————————————————————————————————————
#说明我已经向systemd注册prometheus.service文件了,意思是重新加载systemd相关服务
systemctl daemon-reload
service prometheus restart
#检查prometheus的进程是否存在
ps aux|grep prometheus
#Prometheus简介
一级目录
Alerts # Prometheus的告警信息菜单
Graph # Prometheus的图形展示界面,这是prometheus默认访问的界面
Status # Prometheus的状态数据界面
Help # Prometheus的帮助信息界面
Status子菜单:
Runtime & Build Information # 服务主机的运行状态信息及内部的监控项基本信息
TSDB Status # 时间序列数据库状态
Command-Line Flags # 启动时候从配置文件中加载的属性信息
Configuration # 配置文件的具体内容(yaml格式)
Rules # 查询、告警、可视化等数据分析动作的规则记录
Targets # 监控的目标对象,包括主机、服务等以endpoint形式存在
Service Discovery # 自动发现的各种Targets对象列表
二、node_exporter-V1.5.0安装
1、下载node_exporter
wget https://github.com/prometheus/node_exporter/releases/download/v1.5.0/node_exporter-1.5.0.linux-amd64.tar.gz
2、客户端node_exporter解压及安装,192.168.0.60(每台服务器都需安装):
tar -zxvf node_exporter-1.5.0.linux-amd64.tar.gz
cp -R node_exporter-1.5.0.linux-amd64 /usr/local/node_exporter
touch /usr/lib/systemd/system/node_exporter.service
3、配置node_exporter.service文件
vim /usr/lib/systemd/system/node_exporter.service
#在node_exporter.service中加入如下代码:
[Unit]
Description=node_exporter
After=network.target
[Service]
Type=simple
User=root
ExecStart=/usr/local/node_exporter/node_exporter
Restart=on-failure
[Install]
WantedBy=multi-user.target
4、启动 node_exporter 服务并设置开机启动
systemctl daemon-reload
systemctl enable node_exporter.service
systemctl start node_exporter.service
systemctl status node_exporter.service -l
5、修改prometheus,加入node客户端。
cd /opt/prometheus
vim prometheus.yml
在最后面增加如下配置
#测试,增加新job,命名为nodes,需要注意,要严格注意间距和格式,建议参考范例所示的格式。
- job_name: 'nodes'
static_configs:
- targets: ['192.168.0.60:9100','192.168.0.61:9100','192.168.0.62:9100','192.168.0.63:9100','192.168.0.64:9100']
三、GrafanaV9.5.2-1
1、安装grafana
yum install -y https://dl.grafana.com/enterprise/release/grafana-enterprise-9.5.2-1.x86_64.rpm
systemctl start grafana-server
systemctl status grafana-server -l
2、登录grafana
默认端口为3000,输入http://192.168.0.63:3000/访问
granafa首次登录账户名和密码admin/admin,可以修改
3、配置grafana
配置数据源Data sources->Add data source -> Prometheus,输入prometheus数据源的信息,主要是输入name和url
Grafana图形显示Linux硬件信息
Grafana官方提供模板地址: https://grafana.com/grafana/dashboards
本次要导入的模板: https://grafana.com/grafana/dashboards/11074