努力挣扎的生活 努力挣扎的生活
  • 前端文章

    • JavaScript
  • 学习笔记

    • 《JavaScript教程》
    • 《JavaScript高级程序设计》
    • 《ES6 教程》
    • 《Vue》
    • 《React》
    • 《TypeScript 从零实现 axios》
    • 《Git》
    • TypeScript
    • JS设计模式总结
  • 运维基础
  • 监控
  • 日志系统
  • gitlab安装
  • jenkins安装和管理
  • Jenkins工具集成
  • pipeline流水线
  • Docker
  • Kubernetes
  • Nexus
  • Rancher
  • Prometheus
  • ELK(EFK)
  • 虚拟化
  • Mysql
  • PostgreSQL
  • Redis
  • MongoDB
  • clickhouse
关于
  • 分类
  • 标签
  • 归档
  • 收藏
  • 更多
GitHub (opens new window)

yangfk

瑟瑟发抖的小运维
  • 前端文章

    • JavaScript
  • 学习笔记

    • 《JavaScript教程》
    • 《JavaScript高级程序设计》
    • 《ES6 教程》
    • 《Vue》
    • 《React》
    • 《TypeScript 从零实现 axios》
    • 《Git》
    • TypeScript
    • JS设计模式总结
  • 运维基础
  • 监控
  • 日志系统
  • gitlab安装
  • jenkins安装和管理
  • Jenkins工具集成
  • pipeline流水线
  • Docker
  • Kubernetes
  • Nexus
  • Rancher
  • Prometheus
  • ELK(EFK)
  • 虚拟化
  • Mysql
  • PostgreSQL
  • Redis
  • MongoDB
  • clickhouse
关于
  • 分类
  • 标签
  • 归档
  • 收藏
  • 更多
GitHub (opens new window)
  • 运维基础

  • 监控

    • zabbix 部署
    • zabbix 服务自动注册与发现
    • zabbix 告警 (灾难电话通知)
    • grafana 部署
    • grafana 接入zabbix数据
    • prometheus 安装
      • 什么是普罗米修斯?
        • 特征
        • 组件
      • Prometheus的单机部署
        • 安装服务端Prometheus
        • 安装数据采集节点node_exporter
        • Prometheus函数
        • 安装pushgateway
      • Prometheus部署到K8s集群
    • prometheus 数据接入grafana
  • 日志系统

  • 安全记录

//
  • 运维
  • 监控
yangfk
2021-08-25

prometheus 安装

//

# 什么是普罗米修斯?

Prometheus官网 (opens new window)

Prometheus是一个开源系统监控和警报工具包,最初在 SoundCloud 构建。自 2012 年成立以来,许多公司和组织都采用了 Prometheus,该项目拥有非常活跃的开发者和用户社区。它现在是一个独立的开源项目,独立于任何公司维护。为了强调这一点,并明确项目的治理结构,Prometheus 于 2016 年加入 云原生计算基金会,成为继Kubernetes之后的第二个托管项目。

Prometheus 将其指标收集并存储为时间序列数据,即指标信息与记录时的时间戳以及称为标签的可选键值对一起存储。

# 特征

普罗米修斯的主要特点是:

具有由度量名称和键/值对标识的时间序列数据的多维数据模型
PromQL,一种 利用这种维度的灵活查询语言
不依赖分布式存储;单个服务器节点是自治的
时间序列收集通过 HTTP 上的拉模型进行
通过中间网关支持推送时间序列
通过服务发现或静态配置发现目标
多种图形模式和仪表板支持

什么是指标?

用外行的话来说,指标是数字测量,时间序列意味着随着时间的推移记录变化。用户想要测量的内容因应用程序而异。
对于 Web 服务器,它可能是请求时间,对于数据库,它可能是活动连接数或活动查询数等。   
指标在理解为什么您的应用程序以某种方式工作方面起着重要作用。假设您正在运行一个 Web 应用程序并发现该应用程序很慢。
您将需要一些信息来了解您的应用程序发生了什么。例如,当请求数量很高时,应用程序可能会变慢。如果您有请求计数指标,您可以找出原因并增加服务器数量来处理负载。

# 组件

Prometheus 生态系统由多个组件组成,其中许多是可选的:

主要的Prometheus 服务器,用于抓取和存储时间序列数据
用于检测应用程序代码的客户端库
支持短期工作的推送网关
HAProxy、StatsD、Graphite 等服务的专用出口商。
处理警报的警报管理器
各种支持工具
大多数 Prometheus 组件都是用Go编写的,这使得它们易于构建和部署为静态二进制文件。

架构图

yfklife_prometheus

# Prometheus的单机部署

# 安装服务端Prometheus

prometheus下载页面 (opens new window)

官方安装文档 (opens new window)

  • 下载server

yfklife_prometheus

  • 安装到linux
wget -c https://github.com/prometheus/prometheus/releases/download/v2.34.0/prometheus-2.34.0.linux-amd64.tar.gz
tar xf prometheus-2.34.0.linux-amd64.tar.gz -C /usr/local/
ln -s /usr/local/prometheus-2.34.0.linux-amd64/ /usr/local/prometheus
ln -s /usr/local/prometheus/prometheus /usr/local/bin/

mkdir -p /data/prometheus/data #创建数据存储目录,数据盘
1
2
3
4
5
6
  • 添加到systemd

vi /usr/lib/systemd/system/prometheus.service

[Unit]
Description=Prometheus
#Documentation=https://prometheus.io/
After=network.target

[Service]
Type=simple
#User=prometheus
ExecStart=/usr/local/prometheus/prometheus \
  --config.file=/usr/local/prometheus/prometheus.yml \
  --web.listen-address=0.0.0.0:9090 \
  --web.read-timeout=5m \
  --web.max-connections=10 \
  --query.max-concurrency=20 \
  --query.timeout=2m \
  --storage.tsdb.path=/data/prometheus/data \
  --storage.tsdb.retention.time=15d

Restart=on-failure

[Install]
WantedBy=multi-user.target
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
常用参数 说明
prometheus -h 查看帮助
--config.file="prometheus.yml" 指定运行配置文件
--web.listen-address="0.0.0.0:9090" 指定运行端口,如果修改也要修改prometheus.yml里面的默认监听端口9090
--storage.tsdb.path="/data/prometheus/data" 数据存储目录
--storage.tsdb.retention.time="30d" 数据保留时间
  • 启动

检查启动配置文件

/usr/local/prometheus/promtool check config /usr/local/prometheus/prometheus.yml

systemctl daemon-reload 
systemctl start prometheus.service
systemctl enable prometheus.service #添加开机自启动
1
2
3
  • 启动方式二
#安装daemonize工具
git clone https://github.com/bmc/daemonize.git
cd daemonize
sh configure
make 
sudo make install

#启动
echo '/usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/prometheus.yml --web.listen-address=0.0.0.0:9090 --web.read-timeout=5m --web.max-connections=10 --query.max-concurrency=20 --query.timeout=2m --storage.tsdb.path=/data/prometheus/data --storage.tsdb.retention.time=15d' > /usr/local/prometheus/up.sh
chmod 750 /usr/local/prometheus/up.sh
daemonize -c /usr/local/prometheus /usr/local/prometheus/up.sh

1
2
3
4
5
6
7
8
9
10
11
12

查看WEB界面:http://IP_XXX:9090

# 安装数据采集节点node_exporter

prometheus下载页面 (opens new window)

  • 下载node_exporter

yfklife_prometheus

  • 安装到linux
wget -c https://github.com/prometheus/node_exporter/releases/download/v1.3.1/node_exporter-1.3.1.linux-amd64.tar.gz
tar xf node_exporter-1.3.1.linux-amd64.tar.gz -C /usr/local/
ln -s /usr/local/node_exporter-1.3.1.linux-amd64/ /usr/local/node_exporter
ln -s /usr/local/node_exporter/node_exporter /usr/local/bin/
1
2
3
4
  • 添加到systemd

vi /usr/lib/systemd/system/node_exporter.service

[Unit]
Description=Node Exporter
Documentation=https://github.com/prometheus/node_exporter
After=network.target

[Service]
#User=node_exporter
ExecStart=/usr/local/bin/node_exporter --web.listen-address=:9100 --web.telemetry-path=/node_metrics
Restart=on-failure

[Install]
WantedBy=multi-user.target
1
2
3
4
5
6
7
8
9
10
11
12
常用参数 说明
node_exporter -h 查看帮助
--web.listen-address=":9100" 监听端口为9100
--web.telemetry-path="/node_metrics" 暴露的路径
  • 启动
systemctl daemon-reload 
systemctl start node_exporter.service 
systemctl enable node_exporter.service #添加开机自启动
1
2
3

查看实时数据:http://IP_XXX:9100/node_metrics

配置服务端:prometheus.yml

scrape_configs:
  - job_name: "prometheus"
    static_configs:
      - targets: ["localhost:9090"]

  - job_name: "monitor"
    metrics_path: "/node_metrics"
    static_configs:
      - targets: ["192.168.5.238:9100"]
1
2
3
4
5
6
7
8
9

# Prometheus函数

prometheus函数说明官方地址 (opens new window)

sum()

	取平均值

increase()

	与counter类型数据使用的函数,自增函数,
	示例:increase(node_network_receive_bytes_total[1m])

rate()

	专门与counter类型数据使用的函数,取counter在 N(时间单位:y,d,h,m,s) 的平均每秒增量值,与increase()函数类似,也是自增函数,区别在于多了一个除以相应的时间秒数
	示例:rate(node_network_receive_bytes_total[1m])
	示例:rate(node_network_receive_bytes_total[60s]) * 60  曲线图等于 increase(node_network_receive_bytes_total[1m])

by()

	以什么标签为组,
	示例:sum(increase(node_cpu_seconds_total[1m])) by (instance)

topk()

	Topk 对于每个时间点,只取前三高的数组,这种情况下大概率会出现采集数据不连贯,topk函数一般进行瞬时报警,而不用于观察曲线图

count()

	把数值符合条件的输出条数
	示例:count(node_netstat_Tcp_CurrEstab > 100 )

# 安装pushgateway

pushgateway.jpg

pushgateway下载地址 (opens new window)

pushgateway-v1.5.1 (opens new window)

下载二进制包,启动

screen -S pushgateway #创建screen 后台运行pushgateway

wget https://github.com/prometheus/pushgateway/releases/download/v1.5.1/pushgateway-1.5.1.linux-amd64.tar.gz
tar xf pushgateway-1.5.1.linux-amd64.tar.gz -C /usr/local/
mv /usr/local/pushgateway-1.5.1.linux-amd64 /usr/local/pushgateway
/usr/local/pushgateway/pushgateway --web.listen-address 0.0.0.0:9092
1
2
3
4
5
6

添加job_name 重启配置服务端:prometheus.yml

scrape_configs:
  - job_name: 'pushgateway01'
    static_configs:
      - targets: ["192.168.5.173:9092"]
1
2
3
4

添加测试数据

echo "net_wait 200" |curl --data-binary @- http://192.168.5.173:9092/metrics/job/pushgateway01/instance/192.168.5.238

#说明,格式固定
pushgateway01: job 名称
192.168.5.238: 表示 instance 
1
2
3
4
5

通过Prometheus Graph页面,查看数据,搜索:net_wait 能看到刚刚curl过来的数据,后面就是shell脚本结合crontab 来采集数据了

# Prometheus部署到K8s集群

Prometheus部署到K8s集群🤞🤞

//
如果此文章对您有帮助,点击 -->> 请博主喝咖啡 (opens new window)
上次更新: 2023/05/25, 16:16:09
grafana 接入zabbix数据
prometheus 数据接入grafana

← grafana 接入zabbix数据 prometheus 数据接入grafana→

最近更新
01
Linux Polkit 权限提升漏洞(CVE-2021-4034)
03-28
02
postgreSQL维护
03-17
03
trivy系统漏洞扫描
02-25
更多文章>
Theme by Vdoing | Copyright © 2019-2025 yangfk | 湘ICP备2021014415号-1
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式
×
//