Prometheus学习

监控架构介绍:

基本架构:

Prometheus 和 Zabbix 的对比:

安装和使用:

  • Prometheus 采集、存储数据
  • Grafana 用于图表展示
  • alertmanager 用于接收 Prometheus 发送的警告信息
  • node-exporter 用于收集操作系统和硬件信息的 metrics
shell 复制代码
# 从创建一个专门的 prometheus 用户
useradd -M -s /usr/sbin/nologin prometheus
# 更改 prometheus 用户的文件权限
chown prometheus:prometheus -R /opt/prometheus

#  Prometheus 以 prometheus 用户身份运行
sudo chown -R prometheus:prometheus /opt/prometheus/prometheus/data
# 755 权限设置允许 Prometheus 用户读写目录,但其他用户只能读和执行(进入目录) 
sudo chmod -R 755 /opt/prometheus/prometheus/data


Prometheus Server 安装:

shell 复制代码
# 下载 prometheus 二进制压缩包
wget https://github.com/prometheus/prometheus/releases/download/v2.53.2/prometheus-2.53.2.linux-amd64.tar.gz

# 解压
tar -zxvf prometheus-2.53.2.linux-amd64.tar.gz

mkdir /opt/prometheus -p
mv prometheus-2.53.2.linux-amd64 /opt/prometheus/prometheus

# 创建 systemd 服务
cat > /etc/systemd/system/prometheus.service << "EOF"
[Unit]
Description=Prometheus Server
Documentation=https://prometheus.io/docs/introduction/overview
After=network-online.target

[Service]
Type=simple
User=prometheus
Group=prometheus
Restart=on-failure
ExecStart=/opt/prometheus/prometheus/prometheus \
  -- config.file=/opt/prometheus/prometheus/prometheus.yml \
  -- storage.tsdb.path=/opt/prometheus/prometheus/data \  # 数据存储的路径
  
  -- storage.tsdb.retention.time=60d \  # 数据存储的时长
  -- wed.enable-lifecycle  # 热加载配置

[Install]
WantedBy=multi-user.target
EOF
shell 复制代码
# 重启 Prometheus
systemctl daemon-reload
systemctl start prometheus.service
# 设置 Prometheus 为开机自启
systemctl enable prometheus.service
# 检查 Prometheus 服务的开启状态
systemctl status prometheus.service

# 查看 Prometheus 服务的日志,进行故障排除
journalctl -u prometheus.service -f 

prometheus 监控指标:http://192.168.10.6:9090/metrics

Alert Manager 安装:

shell 复制代码
wget https://github.com/prometheus/alertmanager/releases/download/v0.27.0/alertmanager-0.27.0.linux-amd64.tar.gz
# 或者在本地下载,通过 scp 命令拷贝到远程 linux 服务端


# 解压
tar -zxvf alertmanager-0.27.0.linux-amd64.tar.gz

mkdir /opt/prometheus -p
mv alertmanager-0.27.0.linux-amd64 /opt/prometheus/alertmanager

# 更改 alertmanager 文件夹权限
chown prometheus:prometheus -R /opt/prometheus/alertmanager

# 创建 systemd 服务
cat > /etc/systemd/system/alertmanager.service << "EOF"

[Unit]
Description=Alert Manager
Wants=network-online.target
After=network-online.target

[Service]
Type=simple
User=prometheus
Group=prometheus
ExecStart=/opt/prometheus/alertmanager/alertmanager \
  --config.file=/opt/prometheus/alertmanager/alertmanager.yml \
  --storage.path=/opt/prometheus/alertmanager/data

[Install]
WantedBy=multi-user.target

EOF



shell 复制代码
# 重启 Prometheus
systemctl daemon-reload
systemctl start alertmanager.service
# 设置 Prometheus 为开机自启
systemctl enable alertmanager.service
# 检查 Prometheus 服务的开启状态
systemctl status alertmanager.service

# 查看 alertmanager 服务的日志,进行故障排除
journalctl -u alertmanager.service -f 

Alertmanager 的指标:http://192.168.10.6:9093/metrics

shell 复制代码
# 修改 prometheus 配置:/opt/prometheus/prometheus/prometheus.yml
alerting:
  alertmanagers:
  - static_configs:
    - targets:
      - localhost:9093 # 需根据实际情况填写 alertmanager 的地址

rule_files:
  - "alert.yml"   # 需根据实际修改文件名

# 增加触发器配置文件 /opt/prometheus/prometheus/alert.yml
cat > /opt/prometheus/prometheus/alert.yml << "EOF"
groups:
- name : Prometheus alert
  rules:
  # 对任何实例超过30s无法联系的情况,发送警报
  - alert: 服务警告
    expr: up == 0
    for: 30s
    labels:
      severity: critical
    annotations:
      instance: "{{ $labels.instance }}"
	  description: "{{ $labels.job }} 服务已关闭"
EOF
# 检查配置
cd /opt/prometheus/prometheus
./prometheus check config prometheus.yml

重启 prometheus 或 重新加载配置文件:

shell 复制代码
# 重启
systemctl restart prometheus

# 重载,需要 --web.enable-lifecycle 配置
curl -x POST http://localhost:9090/-/reload

Grafana 安装:

shell 复制代码
wget https://dl.grafana.com/enterprise/release/grafana-enterprise-9.4.3.linux-amd64.tar.gz
tar -zxvf grafana-enterprise-9.4.3.linux-amd64.tar.gz

mv grafana-9.4.3 /opt/prometheus/grafana

chown prometheus:prometheus -R /opt/prometheus/grafana

cat > /etc/systemd/system/grafana-server.service << "EOF"
[Unit]
Description=Grafana Server
Documentation=http://docs.grafana.org

[Service]
Type=simple
User=prometheus
Group=prometheus
Restart=on-failure
ExecStart=/opt/prometheus/grafana/bin/grafana-server \
  --config=/opt/prometheus/grafana/conf/defaults.ini \
  --homepath=/opt/prometheus/grafana

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
systemctl start grafana-server
systemctl status grafana-server

# 查看 alertmanager 服务的日志,进行故障排除
journalctl -u grafana-server.service -f

Grafana 的访问地址:http://192.168.10.6:3000

通过 gitee 下载安装:

使用 grafana 展示 prometheus 的图形

PromQL:

快速入门:

相关推荐
江南风月1 天前
WGCLOUD监控系统的Restful Http接口一览
运维·zabbix·运维开发·prometheus
文青小兵1 天前
Linux云计算——docker 监控(五)
linux·docker·云计算·grafana·prometheus
weixin_468466852 天前
Prometheus监控服务部署与实战指南
服务器·后端·python·docker·自动化·prometheus
江华森2 天前
Prometheus 全栈监控体系部署与使用指南
prometheus
codeejun3 天前
每日一Go-70、Prometheus + Grafana 从采集到告警的完整实战(Go + Kind)
golang·grafana·prometheus
Cat_Rocky5 天前
k8s-Prometheus的manifests 清单部署
linux·kubernetes·prometheus
成为你的宁宁6 天前
【Prometheus基于文件的服务发现】
服务发现·prometheus
zhojiew6 天前
在Ray集群中使用vLLM部署LLM模型并集成Prometheus和Grafana进行指标观测的实践
grafana·prometheus·vllm
Cat_Rocky6 天前
K8S部署EFK日志收集技术栈
容器·kubernetes·prometheus
D4c-lovetrain6 天前
云原生实战:K8s 一键部署 Prometheus+Grafana+EFK 完整可观测平台
云原生·kubernetes·prometheus