测试环境
prometheus-2.54.1.linux-amd64.tar.gz
下载地址:
https://www.prometheus.io/download/
https://github.com/prometheus/prometheus/releases/download/v2.54.1/prometheus-2.54.1.linux-amd64.tar.gz
node_exporter-1.8.2.linux-amd64.tar.gz
下载地址:
https://github.com/prometheus/node_exporter/releases/download/v1.8.2/node_exporter-1.8.2.linux-amd64.tar.gz
https://prometheus.io/download/#node_exporter
pushgateway-1.9.0.linux-amd64.tar.gz
下载地址:
https://www.prometheus.io/download/#pushgateway
https://github.com/prometheus/pushgateway/releases/download/v1.9.0/pushgateway-1.9.0.linux-amd64.tar.gz
victoria-metrics-linux-amd64-v1.103.0.tar.gz
下载地址:
https://github.com/VictoriaMetrics/VictoriaMetrics/releases
https://github.com/VictoriaMetrics/VictoriaMetrics/releases/tag/v1.103.0
https://github.com/VictoriaMetrics/VictoriaMetrics/releases/download/v1.103.0/victoria-metrics-linux-amd64-v1.103.0.tar.gz
grafana-7.5.6-1.x86_64.rpm
下载地址:https://dl.grafana.com/oss/release/grafana-7.5.6-1.x86_64.rpm
CentOS 7.9
注意:prometheus
,victoria-metrics
,grafana
,pushgateway
都可以安装在不同机器上,本文仅涉及学习实践,所以,都安装在同一台机器上了。
实践过程
VictoriaMetrics安装
# wget https://github.com/VictoriaMetrics/VictoriaMetrics/releases/download/v1.103.0/victoria-metrics-linux-amd64-v1.103.0.tar.gz
# tar -xvzf victoria-metrics-linux-amd64-v1.103.0.tar.gz -C /usr/local/bin # 解压后会生成一个名为victoria-metrics-prod的二进制文件
# 创建一个用于存储VictoriaMetrics数据的文件夹
# mkdir -p /usr/data/victoria-metrics
# 创建服务
# vi /etc/systemd/system/victoriametrics.service
[Unit]
Description=Victoria metrics service
After=network.target[Service]
Type=simple
Restart=always
TimeoutStartSec=30
Restart=on-failure
RestartSec=5s
ExecStart=/usr/local/bin/victoria-metrics-prod -storageDataPath=/usr/data/victoria-metrics -retentionPeriod=30d -selfScrapeInterval=10s
ExecStop=/bin/kill $MAINPID
ExecReload=/bin/kill -HUP $MAINPIDPrivateTmp=yes[Install]
WantedBy=multi-user.target
说明:
-storageDataPath
设置数据目录路径(如果目录路径不存在,程序启动时会自动创建)。VictoriaMetrics会将所有数据存储在此目录中。默认存储在当前工作目录中的victoria-metrics-data
目录
-retentionPeriod
设置存储数据的保留。自动删除旧的数据。默认保留时间为1个月(31天)。 最小值为24h
或者1d
,-retentionPeriod=3
设置数据仅存储3个月,-retentionPeriod=1d
设置数据仅保留1天。
一般情况下,只需要设置上述两个参数标识即可,其它参数已经有足够好的默认值,仅在确实需要修改它们时才进行设置,执行./victoria-metrics-prod --help
可以查看所有可获取的参数描述和及其默认值所有可获取的参数描述和默认值。
默认的,VictoriaMetrics通过端口8428
监听Prometheus查询API请求
建议为 VictoriaMetrics设置监控
前台启动查看
# /usr/local/bin/victoria-metrics-prod -storageDataPath=/usr/data/victoria-metrics -retentionPeriod=30d -selfScrapeInterval=10s
2024-09-03T16:33:42.187Z info VictoriaMetrics/lib/logger/flag.go:12 build version: victoria-metrics-20240828-135248-tags-v1.103.0-0-g5aeb759df9
2024-09-03T16:33:42.187Z info VictoriaMetrics/lib/logger/flag.go:13 command-line flags
2024-09-03T16:33:42.187Z info VictoriaMetrics/lib/logger/flag.go:20 -retentionPeriod="30d"
2024-09-03T16:33:42.187Z info VictoriaMetrics/lib/logger/flag.go:20 -selfScrapeInterval="10s"
2024-09-03T16:33:42.187Z info VictoriaMetrics/lib/logger/flag.go:20 -storageDataPath="/usr/data/victoria-metrics"
2024-09-03T16:33:42.187Z info VictoriaMetrics/app/victoria-metrics/main.go:73 starting VictoriaMetrics at "[:8428]"...
2024-09-03T16:33:42.187Z info VictoriaMetrics/app/vmstorage/main.go:107 opening storage at "/usr/data/victoria-metrics" with -retentionPeriod=30d
2024-09-03T16:33:42.189Z info VictoriaMetrics/lib/memory/memory.go:42 limiting caches to 611758080 bytes, leaving 407838720 bytes to the OS according to -memory.allowedPercent=60
2024-09-03T16:33:42.205Z info VictoriaMetrics/app/vmstorage/main.go:121 successfully opened storage "/usr/data/victoria-metrics" in 0.018 seconds; partsCount: 0; blocksCount: 0; rowsCount: 0; sizeBytes: 0
2024-09-03T16:33:42.205Z info VictoriaMetrics/app/vmselect/promql/rollup_result_cache.go:127 loading rollupResult cache from "/usr/data/victoria-metrics/cache/rollupResult"...
2024-09-03T16:33:42.207Z info VictoriaMetrics/app/vmselect/promql/rollup_result_cache.go:156 loaded rollupResult cache from "/usr/data/victoria-metrics/cache/rollupResult" in 0.001 seconds; entriesCount: 0, sizeBytes: 0
2024-09-03T16:33:42.207Z info VictoriaMetrics/app/victoria-metrics/main.go:84 started VictoriaMetrics in 0.020 seconds
2024-09-03T16:33:42.207Z info VictoriaMetrics/lib/httpserver/httpserver.go:121 starting server at http://127.0.0.1:8428/
2024-09-03T16:33:42.207Z info VictoriaMetrics/lib/httpserver/httpserver.go:122 pprof handlers are exposed at http://127.0.0.1:8428/debug/pprof/
2024-09-03T16:33:42.208Z info VictoriaMetrics/app/victoria-metrics/self_scraper.go:46 started self-scraping `/metrics` page with interval 10.000 seconds
2024-09-03T16:33:52.293Z info VictoriaMetrics/lib/storage/partition.go:202 creating a partition "2024_09" with smallPartsPath="/usr/data/victoria-metrics/data/small/2024_09", bigPartsPath="/usr/data/victoria-metrics/data/big/2024_09"
2024-09-03T16:33:52.295Z info VictoriaMetrics/lib/storage/partition.go:211 partition "2024_09" has been created
关闭前台启动的进程,启动服务并设置开机启动
# systemctl daemon-reload && sudo systemctl enable --now victoriametrics.service
# 查看服务是否启动成功
# systemctl status victoriametrics.service
环境变量
所有VictoriaMetrics组件都允许通过%{ENV_VAR}
语法在yaml
配置文件(如-promgraft.config
)和命令行标志中引用环境变量。例如,如果VictoriaMetrics启动时存在METRICS_AUTH_KEY=top-secret
环境变量,则-metricsAuthKey=%{METRICS_AUTH_KEY}
会被VictoriaMetrics自动扩展为-metricsAuthKey=top-secret
。
VictoriaMetrics在启动时递归扩展环境变量中的%{ENV_VAR}
引用。例如,如果 BAR=a%{BAZ}
且BAZ=bc
,则FOO=%{BAR}
环境变量扩展为FOO=abc
。
此外,所有VictoriaMetrics组件都允许根据以下规则通过环境变量设置参数标识值:
- 必须设置
-envflag.enable
- 参数标识名称中的每个
.
字符必须替换成_
( 例如,-insert.maxQueueDuration <duration>
将转换为insert_maxQueueDuration=<duration>
) - 对于重复标识,一种可用的可选语法将不同的标识值放在一起,通过
,
分隔 (例如-storageNode <nodeA> -storageNode <nodeB>
可以转换成storageNode=<nodeA>,<nodeB>
) - 可以通过
-envflag.prefix
为环境变量设置前缀。例如,如果-envflag.prefix=VM_
, 那么环境变量必须以VM_
开头
开放防火墙端口
# firewall-cmd --permanent --zone=public --add-port=8248/tcp
success
# firewall-cmd --reload
success
除了以二进制方式启动,VictoriaMetrics也支持Docker安装,具体可参考链接 https://hub.docker.com/r/victoriametrics/victoria-metrics/
参考链接
https://docs.victoriametrics.com/quick-start/
Prometheus安装与配置
# wget https://github.com/prometheus/prometheus/releases/download/v2.54.1/prometheus-2.54.1.linux-amd64.tar.gz
# tar -C /usr/local/ -xvzf prometheus-2.54.1.linux-amd64.tar.gz
# cd /usr/local/prometheus-2.54.1.linux-amd64
# ls
console_libraries consoles LICENSE NOTICE prometheus prometheus.yml promtool
# ln -s /usr/local/prometheus-2.26.0.linux-amd64/prometheus /usr/local/bin/prometheus
# cp prometheus.yml prometheus.yml.bak
# echo ''> prometheus.yml
# vi prometheus.yml
将prometheus.yml
内容替换为以下内容
global:scrape_interval: 15s # 默认,每15秒采样一次目标# 一份采样配置仅包含一个 endpoint 来做采样
# 下面是 Prometheus 本身的endpoint:
scrape_configs:# job_name 将被被当作一个标签 `job=<job_name>`添加到该配置的任意时序采样.- job_name: 'prometheus'# 覆盖全局默认值,从该job每5秒对目标采样一次scrape_interval: 5sstatic_configs:- targets: ['localhost:9090']remote_write:- url: http://192.168.88.132:8428/api/v1/writequeue_config:max_samples_per_send: 10000capacity: 20000max_shards: 30
说明:
为了能给VictoriaMetrics发送数据,在Prometheus配置文件中,增加remote_write 配置。 添加以下代码到 Prometheus 配置文件(一般是在prometheus.yml
):
remote_write:- url: http://<victoriametrics-addr>:8428/api/v1/write
注意:添加时,需要替换<victoriametrics-addr>
为VictoriaMetrics
主机名称或者IP地址,形如以下
remote_write:- url: http://192.168.88.132.71.170:8428/api/v1/write
启动Prometheus服务
# ./prometheus
ts=2024-09-04T15:50:32.906Z caller=main.go:601 level=info msg="No time or size retention was set so using the default time retention" duration=15d
ts=2024-09-04T15:50:32.906Z caller=main.go:645 level=info msg="Starting Prometheus Server" mode=server version="(version=2.54.1, branch=HEAD, revision=e6cfa720fbe6280153fab13090a483dbd40bece3)"
ts=2024-09-04T15:50:32.906Z caller=main.go:650 level=info build_context="(go=go1.22.6, platform=linux/amd64, user=root@812ffd741951, date=20240827-10:56:41, tags=netgo,builtinassets,stringlabels)"
ts=2024-09-04T15:50:32.906Z caller=main.go:651 level=info host_details="(Linux 3.10.0-1160.el7.x86_64 #1 SMP Mon Oct 19 16:18:59 UTC 2020 x86_64 localhost.localdomain (none))"
ts=2024-09-04T15:50:32.906Z caller=main.go:652 level=info fd_limits="(soft=4096, hard=4096)"
ts=2024-09-04T15:50:32.906Z caller=main.go:653 level=info vm_limits="(soft=unlimited, hard=unlimited)"
ts=2024-09-04T15:50:32.917Z caller=web.go:571 level=info component=web msg="Start listening for connections" address=0.0.0.0:9090
ts=2024-09-04T15:50:32.925Z caller=main.go:1160 level=info msg="Starting TSDB ..."
ts=2024-09-04T15:50:32.930Z caller=tls_config.go:313 level=info component=web msg="Listening on" address=[::]:9090
ts=2024-09-04T15:50:32.930Z caller=tls_config.go:316 level=info component=web msg="TLS is disabled." http2=false address=[::]:9090
ts=2024-09-04T15:50:32.932Z caller=head.go:626 level=info component=tsdb msg="Replaying on-disk memory mappable chunks if any"
ts=2024-09-04T15:50:32.932Z caller=head.go:713 level=info component=tsdb msg="On-disk memory mappable chunks replay completed" duration=5.601µs
ts=2024-09-04T15:50:32.933Z caller=head.go:721 level=info component=tsdb msg="Replaying WAL, this may take a while"
ts=2024-09-04T15:50:32.933Z caller=head.go:793 level=info component=tsdb msg="WAL segment loaded" segment=0 maxSegment=0
ts=2024-09-04T15:50:32.933Z caller=head.go:830 level=info component=tsdb msg="WAL replay completed" checkpoint_replay_duration=25.237µs wal_replay_duration=560.14µs wbl_replay_duration=141ns chunk_snapshot_load_duration=0s mmap_chunk_replay_duration=5.601µs total_replay_duration=605.091µs
ts=2024-09-04T15:50:32.938Z caller=main.go:1181 level=info fs_type=XFS_SUPER_MAGIC
ts=2024-09-04T15:50:32.938Z caller=main.go:1184 level=info msg="TSDB started"
ts=2024-09-04T15:50:32.938Z caller=main.go:1367 level=info msg="Loading configuration file" filename=prometheus.yml
ts=2024-09-04T15:50:32.940Z caller=dedupe.go:112 component=remote level=info remote_name=b93975 url=http://192.168.88.132:8428/api/v1/write msg="Starting WAL watcher" queue=b93975
ts=2024-09-04T15:50:32.940Z caller=dedupe.go:112 component=remote level=info remote_name=b93975 url=http://192.168.88.132:8428/api/v1/write msg="Starting scraped metadata watcher"
ts=2024-09-04T15:50:32.945Z caller=main.go:1404 level=info msg="updated GOGC" old=100 new=75
ts=2024-09-04T15:50:32.945Z caller=main.go:1415 level=info msg="Completed loading of configuration file" filename=prometheus.yml totalDuration=6.619214ms db_storage=25.792µs remote_storage=1.190631ms web_handler=652ns query_engine=18.267µs scrape=3.897727ms scrape_sd=49.586µs notify=1.164µs notify_sd=954ns rules=60.122µs tracing=62.555µs
ts=2024-09-04T15:50:32.945Z caller=main.go:1145 level=info msg="Server is ready to receive web requests."
ts=2024-09-04T15:50:32.945Z caller=manager.go:164 level=info component="rule manager" msg="Starting rule manager..."
ts=2024-09-04T15:50:32.945Z caller=dedupe.go:112 component=remote level=info remote_name=b93975 url=http://192.168.88.132:8428/api/v1/write msg="Replaying WAL" queue=b93975
ts=2024-09-04T15:50:40.288Z caller=dedupe.go:112 component=remote level=info remote_name=b93975 url=http://192.168.88.132:8428/api/v1/write msg="Done replaying WAL" duration=7.342804783s
说明:如果希望使用非默认配置文件,可以在执行命令时指定具体的配置文件,类似如下:
# ./prometheus --config.file=./custom_prometheus.yml
备注:重启命令
kill -HUP `pid_of_prometheus`
当然也可直接Ctrl + c终止Prometheus进程,然后重新运行。
Prometheus把传入的数据写入到本地存储的同时将数据复制到远程存储。这意味着即使远程存储不可用,存储在本地,--storage.tsdb.retention.time
指定数据保留期内的数据依然可用。
如果需要从多个 Prometheus 实例往 VictoriaMetrics 发送数据,添加external_labels
配置到Prometheus配置文件的global
结点,形如以下:
global:external_labels:datacenter: dc-123
如上,以上配置告诉 Prometheus 添加 datacenter=dc-123
标签到发送给远程存储的每个时间序列。标签名称可以是任意的,比如 datacenter
。标签值必须在所有 Prometheus 实例中保持唯一,这样,可以通过该标签过滤或者分组时序。
对于高负载的Prometheus实例(每秒200k+个样本),可以应用以下调优:
remote_write:- url: http://<victoriametrics-addr>:8428/api/v1/writequeue_config:max_samples_per_send: 10000capacity: 20000max_shards: 30
使用远程写入增加 Prometheus 约25%
的内存使用率,这取决于数据形态(原文:Using remote write increases memory usage for Prometheus up to ~25% and depends on the shape of data
)。如果你正面临太高内存消耗的问题,尝试降低 max_samples_per_send
和 capacity
参数配置值(注意:这两个参数是紧密相连的)查看更多关于远程写入调优.
建议升级Prometheus到 v2.12.0 或更高,因为之前版本使用 remote_write
存在问题。
也可以查看下 vmagent 和 vmalert, 也是用于减少Prometheus更快和更少的资源消耗的一种选择方案。
参考链接:https://docs.victoriametrics.com/#prometheus-setup
创建服务
# vi /etc/systemd/system/rometheus.service
[Unit]
Description=Prometheus service
After=network.target[Service]
Type=simple
Restart=always
TimeoutStartSec=30
Restart=on-failure
RestartSec=5s
ExecStart=/usr/local/prometheus-2.54.1.linux-amd64/prometheus --config.file=/usr/local/prometheus-2.54.1.linux-amd64/prometheus.yml
ExecStop=/bin/kill $MAINPID
ExecReload=/bin/kill -HUP $MAINPID[Install]
WantedBy=multi-user.target
注意:配置文件必须使用绝对路径,否则运行服务报错,找不到文件/prometheus.yml
先手动停止上述前台运行的prometheus,然后执行以下命令
# systemctl daemon-reload && systemctl enable --now prometheus
# systemctl status prometheus
grafana安装与配置
# yum install grafana-7.5.6-1.x86_64.rpm
说明:如果使用yum
命令安装找不到软件包,可以考虑下载安装,如下
# wget https://dl.grafana.com/oss/release/grafana-7.5.6-1.x86_64.rpm
# yum install -y fontconfig urw-fonts
# rpm -ivh grafana-7.5.6-1.x86_64.rpm
warning: grafana-7.5.6-1.x86_64.rpm: Header V4 RSA/SHA256 Signature, key ID 24098cb6: NOKEY
Preparing... ################################# [100%]
Updating / installing...1:grafana-7.5.6-1 ################################# [100%]
### NOT starting on installation, please execute the following statements to configure grafana to start automatically using systemdsudo /bin/systemctl daemon-reloadsudo /bin/systemctl enable grafana-server.service
### You can start grafana-server by executingsudo /bin/systemctl start grafana-server.service
POSTTRANS: Running script
# /bin/systemctl daemon-reload~]# /bin/systemctl enable grafana-server.service
Created symlink from /etc/systemd/system/multi-user.target.wants/grafana-server.service to /usr/lib/systemd/system/grafana-server.service.
# /bin/systemctl start grafana-server.service
说明:如果不执行yum install -y fontconfig urw-fonts
命令,安装grafana时可能报错,如下
warning: grafana-7.5.6-1.x86_64.rpm: Header V4 RSA/SHA256 Signature, key ID 24098cb6: NOKEY
error: Failed dependencies:fontconfig is needed by grafana-7.5.6-1.x86_64urw-fonts is needed by grafana-7.5.6-1.x86_64
修改grafana配置[可选]
# vim /etc/grafana/grafana.ini
浏览器输入网址:http://<grafana-addr>:3000
访问看看效果:
备注:grafana默认登录账号密码为admin/admin
参考链接:https://grafana.com/grafana/download?pg=get&plcmt=selfmanaged-box1-cta1
开放防火墙端口
# firewall-cmd --permanent --zone=public --add-port=3000/tcp
success
# firewall-cmd --reload
success
创建Prometheus数据源
使用以下URL创建 Prometheus数据源 (创建数据源时仅修改URL
,其它保持默认) :
http://<victoriametrics-addr>:8428
如下,替换<victoriametrics-addr>
为VictoriaMetrics
主机名称或者IP地址,形如http://192.168.55.88.132:8428
,然后使用PromQL 或MetricsQL用创建的数据源创建图表
Access模式简介
访问模式控制如何处理对数据源的请求。如果没有其他说明,Server(default)
应该是首选方式。
Server
访问模式(默认)
来自浏览器发起的所有请求都将发送到Grafana后端/服务器,后者将请求转发到数据源,从而规避可能的跨域源资源共享Cross-Origin Resource Sharing (CORS)
要求。如果选择该访问模式,则要求URL
可被grafana后端/服务器访问。Browser 访问模式
来自浏览器的所有请求都将直接发送给数据源,并可能受到跨域源资源共享要求的约束。如果选择该访问模式,则要求URL可从浏览器访问。·
参考链接:https://docs.victoriametrics.com/#grafana-setup
安装 pushgateway
何时使用pushgateway
pushgateway是一种中间服务,允许从Prometheus无法抓取的job中推送指标。有关详细信息,请参阅推送指标.
建议仅在某些有限的情况下使用Pushgateway。盲目使用pushgateway而不是Prometheus通常的拉取模型来收集一般指标时,会有几个陷阱:
- 当通过单个Pushgateway监视多个实例时,pushgateway既成单点故障,也是潜在的瓶颈。
- 将失去Prometheus通过
up
指标(每次抓取时生成)进行的自动实例健康监测。 - Pushgateway永远不会忘记推送给它的序列(series),并将它们永远暴露给Prometheus,除非通过Pushgateway API手动删除这些序列
当一个job的多个实例通过instance
标签或其它类似的在Pushgateway中区分它们的指标时,后一点尤其重要。即使原始实例被重命名或删除,实例的指标依然保留在pushgateway中。这是因为pushgateway作为指标缓存的生命周期与向其推送指标的进程的生命周期从根本上是分开的。与Prometheus通常的拉取式监控相比:当实例消失(有意或无意)时,其指标将自动消失。使用Pushgateway时,情况并非如此,必须自己手动删除任何过时的指标或自动化此生命周期同步。
通常,Pushgateway的唯一有效使用场景是捕获服务级别批处理作业的结果。“服务级别”的批处理作业是指与特定机器或作业实例在语义上无关的作业(例如,删除整个服务的多个用户的批处理作业)。此类作业的指标不应包括机器或实例标签,以将特定机器或实例的生命周期与推送的指标解耦。这减轻了Pushgateway中管理过时指标的负担。另请参阅监控批处理作业的最佳实践.
参考链接:
https://prometheus.io/docs/practices/pushing/?#when-to-use-the-pushgateway
安装pushgateway
# wget https://github.com/prometheus/pushgateway/releases/download/v1.9.0/pushgateway-1.9.0.linux-amd64.tar.gz
# tar -C /usr/local/ -xvzf pushgateway-1.9.0.linux-amd64.tar.gz
# ln -s /usr/local/pushgateway-1.9.0.linux-amd64/pushgateway /usr/local/bin/pushgateway
# pushgateway
ts=2024-09-04T17:38:16.325Z caller=main.go:87 level=info msg="starting pushgateway" version="(version=1.9.0, branch=HEAD, revision=d1ca1a6a426126a09a21f745e8ffbaba550f9643)"
ts=2024-09-04T17:38:16.325Z caller=main.go:88 level=info build_context="(go=go1.22.4, platform=linux/amd64, user=root@2167597b1e9c, date=20240608-15:04:08, tags=unknown)"
ts=2024-09-04T17:38:16.328Z caller=tls_config.go:313 level=info msg="Listening on" address=[::]:9091
ts=2024-09-04T17:38:16.328Z caller=tls_config.go:316 level=info msg="TLS is disabled." http2=false address=[::]:9091
配置Prometheus实例
为了访问pushgateway指标,需要正确配置本地运行的Prometheus实例。以下prometheus.yml
示例配置文件将告诉Prometheus实例通过localhost:9100
从 Node Exporter中抓取数据,以及抓取频率:
global:scrape_interval: 15sscrape_configs:
- job_name: nodestatic_configs:- targets: ['localhost:9100']
修改prometheus配置文件
global:scrape_interval: 15s # 默认,每15秒采样一次目标# 一份采样配置仅包含一个 endpoint 来做采样
# 下面是 Prometheus 本身的endpoint:
scrape_configs:# job_name 将被被当作一个标签 `job=<job_name>`添加到该配置的任意时序采样.- job_name: 'prometheus'# 覆盖全局默认值,从该job每5秒对目标采样一次scrape_interval: 5sstatic_configs:- targets: ['localhost:9090']- job_name: 'pushgateway'static_configs:# 注意:不能写成['http://192.168.88.132:9090'],这样运行会报错- targets: ['192.168.88.132:9091']remote_write:- url: http://192.168.88.132:8428/api/v1/writequeue_config:max_samples_per_send: 10000capacity: 20000max_shards: 30
重启Prometheus
# system restart prometheus
# system status prometheus
创建服务
# vi /etc/systemd/system/pushgateway.service
[Unit]
Description=Pushgateway service
After=network.target[Service]
Type=simple
Restart=always
TimeoutStartSec=30
Restart=on-failure
RestartSec=5s
ExecStart=/usr/local/pushgateway-1.9.0.linux-amd64/pushgateway
ExecStop=/bin/kill $MAINPID
ExecReload=/bin/kill -HUP $MAINPID[Install]
WantedBy=multi-user.target
先手动停止上述前台运行的pushgateway,然后执行以下命令
# systemctl daemon-reload && systemctl enable --now pushgateway
# systemctl status pushgateway
Node Exporter安装与配置
注意:只需在需要被监控的机器上安装(本例中为一台redis服务器,IP:192.168.88.131
)
# wget https://github.com/prometheus/node_exporter/releases/download/v1.8.2/node_exporter-1.8.2.linux-amd64.tar.gz
# tar -C /usr/local/ -xvzf node_exporter-1.8.2.linux-amd64.tar.gz
# ln -s /usr/local/node_exporter-1.8.2.linux-amd64/node_exporter /usr/local/bin/node_exporter
# node_exporter
ts=2024-09-04T16:01:28.241Z caller=node_exporter.go:193 level=info msg="Starting node_exporter" version="(version=1.8.2, branch=HEAD, revision=f1e0e8360aa60b6cb5e5cc1560bed348fc2c1895)"
ts=2024-09-04T16:01:28.241Z caller=node_exporter.go:194 level=info msg="Build context" build_context="(go=go1.22.5, platform=linux/amd64, user=root@03d440803209, date=20240714-11:53:45, tags=unknown)"
ts=2024-09-04T16:01:28.242Z caller=node_exporter.go:196 level=warn msg="Node Exporter is running as root user. This exporter is designed to run as unprivileged user, root is not required."
ts=2024-09-04T16:01:28.242Z caller=filesystem_common.go:111 level=info collector=filesystem msg="Parsed flag --collector.filesystem.mount-points-exclude" flag=^/(dev|proc|run/credentials/.+|sys|var/lib/docker/.+|var/lib/containers/storage/.+)($|/)
ts=2024-09-04T16:01:28.242Z caller=filesystem_common.go:113 level=info collector=filesystem msg="Parsed flag --collector.filesystem.fs-types-exclude" flag=^(autofs|binfmt_misc|bpf|cgroup2?|configfs|debugfs|devpts|devtmpfs|fusectl|hugetlbfs|iso9660|mqueue|nsfs|overlay|proc|procfs|pstore|rpc_pipefs|securityfs|selinuxfs|squashfs|sysfs|tracefs)$
ts=2024-09-04T16:01:28.242Z caller=diskstats_common.go:111 level=info collector=diskstats msg="Parsed flag --collector.diskstats.device-exclude" flag=^(z?ram|loop|fd|(h|s|v|xv)d[a-z]|nvme\d+n\d+p)\d+$
ts=2024-09-04T16:01:28.243Z caller=node_exporter.go:111 level=info msg="Enabled collectors"
ts=2024-09-04T16:01:28.243Z caller=node_exporter.go:118 level=info collector=arp
ts=2024-09-04T16:01:28.243Z caller=node_exporter.go:118 level=info collector=bcache
ts=2024-09-04T16:01:28.243Z caller=node_exporter.go:118 level=info collector=bonding
ts=2024-09-04T16:01:28.243Z caller=node_exporter.go:118 level=info collector=btrfs
ts=2024-09-04T16:01:28.243Z caller=node_exporter.go:118 level=info collector=conntrack
ts=2024-09-04T16:01:28.243Z caller=node_exporter.go:118 level=info collector=cpu
ts=2024-09-04T16:01:28.243Z caller=node_exporter.go:118 level=info collector=cpufreq
ts=2024-09-04T16:01:28.243Z caller=node_exporter.go:118 level=info collector=diskstats
ts=2024-09-04T16:01:28.243Z caller=node_exporter.go:118 level=info collector=dmi
ts=2024-09-04T16:01:28.243Z caller=node_exporter.go:118 level=info collector=edac
ts=2024-09-04T16:01:28.243Z caller=node_exporter.go:118 level=info collector=entropy
ts=2024-09-04T16:01:28.243Z caller=node_exporter.go:118 level=info collector=fibrechannel
ts=2024-09-04T16:01:28.243Z caller=node_exporter.go:118 level=info collector=filefd
ts=2024-09-04T16:01:28.243Z caller=node_exporter.go:118 level=info collector=filesystem
ts=2024-09-04T16:01:28.243Z caller=node_exporter.go:118 level=info collector=hwmon
ts=2024-09-04T16:01:28.243Z caller=node_exporter.go:118 level=info collector=infiniband
ts=2024-09-04T16:01:28.243Z caller=node_exporter.go:118 level=info collector=ipvs
ts=2024-09-04T16:01:28.243Z caller=node_exporter.go:118 level=info collector=loadavg
ts=2024-09-04T16:01:28.243Z caller=node_exporter.go:118 level=info collector=mdadm
ts=2024-09-04T16:01:28.243Z caller=node_exporter.go:118 level=info collector=meminfo
ts=2024-09-04T16:01:28.243Z caller=node_exporter.go:118 level=info collector=netclass
ts=2024-09-04T16:01:28.243Z caller=node_exporter.go:118 level=info collector=netdev
ts=2024-09-04T16:01:28.243Z caller=node_exporter.go:118 level=info collector=netstat
ts=2024-09-04T16:01:28.243Z caller=node_exporter.go:118 level=info collector=nfs
ts=2024-09-04T16:01:28.243Z caller=node_exporter.go:118 level=info collector=nfsd
ts=2024-09-04T16:01:28.243Z caller=node_exporter.go:118 level=info collector=nvme
ts=2024-09-04T16:01:28.243Z caller=node_exporter.go:118 level=info collector=os
ts=2024-09-04T16:01:28.243Z caller=node_exporter.go:118 level=info collector=powersupplyclass
ts=2024-09-04T16:01:28.243Z caller=node_exporter.go:118 level=info collector=pressure
ts=2024-09-04T16:01:28.243Z caller=node_exporter.go:118 level=info collector=rapl
ts=2024-09-04T16:01:28.243Z caller=node_exporter.go:118 level=info collector=schedstat
ts=2024-09-04T16:01:28.243Z caller=node_exporter.go:118 level=info collector=selinux
ts=2024-09-04T16:01:28.243Z caller=node_exporter.go:118 level=info collector=sockstat
ts=2024-09-04T16:01:28.243Z caller=node_exporter.go:118 level=info collector=softnet
ts=2024-09-04T16:01:28.243Z caller=node_exporter.go:118 level=info collector=stat
ts=2024-09-04T16:01:28.243Z caller=node_exporter.go:118 level=info collector=tapestats
ts=2024-09-04T16:01:28.243Z caller=node_exporter.go:118 level=info collector=textfile
ts=2024-09-04T16:01:28.243Z caller=node_exporter.go:118 level=info collector=thermal_zone
ts=2024-09-04T16:01:28.243Z caller=node_exporter.go:118 level=info collector=time
ts=2024-09-04T16:01:28.243Z caller=node_exporter.go:118 level=info collector=timex
ts=2024-09-04T16:01:28.243Z caller=node_exporter.go:118 level=info collector=udp_queues
ts=2024-09-04T16:01:28.243Z caller=node_exporter.go:118 level=info collector=uname
ts=2024-09-04T16:01:28.243Z caller=node_exporter.go:118 level=info collector=vmstat
ts=2024-09-04T16:01:28.243Z caller=node_exporter.go:118 level=info collector=watchdog
ts=2024-09-04T16:01:28.243Z caller=node_exporter.go:118 level=info collector=xfs
ts=2024-09-04T16:01:28.243Z caller=node_exporter.go:118 level=info collector=zfs
ts=2024-09-04T16:01:28.244Z caller=tls_config.go:313 level=info msg="Listening on" address=[::]:9100
ts=2024-09-04T16:01:28.244Z caller=tls_config.go:316 level=info msg="TLS is disabled." http2=false address=[::]:9100
通过输出可知,Node Exporter已在运行,并且在端口9100暴露指标
Node Exporter 指标
通过请求/metrics
端点来确认指标是否已成功暴露:
# curl http://localhost:9100/metrics
看到类似下输出则表明暴露成功。
# HELP go_gc_duration_seconds A summary of the pause duration of garbage collection cycles.
# TYPE go_gc_duration_seconds summary
go_gc_duration_seconds{quantile="0"} 0
go_gc_duration_seconds{quantile="0.25"} 0
go_gc_duration_seconds{quantile="0.5"} 0
go_gc_duration_seconds{quantile="0.75"} 0
go_gc_duration_seconds{quantile="1"} 0
go_gc_duration_seconds_sum 0
go_gc_duration_seconds_count 0
...
Node Exporter现在暴露了Prometheus可以抓取的指标,包括输出中更深层级的各种系统指标(以node_
为前缀)。要查看这些指标可以执行以下命令:
# curl http://localhost:9100/metrics | grep "node_"
配置Prometheus实例
修改prometheus配置文件
global:scrape_interval: 15s # 默认,每15秒采样一次目标# 一份采样配置仅包含一个 endpoint 来做采样
# 下面是 Prometheus 本身的endpoint:
scrape_configs:# job_name 将被被当作一个标签 `job=<job_name>`添加到该配置的任意时序采样.- job_name: 'prometheus'# 覆盖全局默认值,从该job每5秒对目标采样一次scrape_interval: 5sstatic_configs:- targets: ['localhost:9090']- job_name: 'pushgateway'static_configs:# 注意:不能写成['http://192.168.88.131:9091'],这样运行会报错- targets: ['192.168.88.131:9091']- job_name: 'redis_node'static_configs:- targets: ['192.168.88.131:9100']remote_write:- url: http://192.168.88.132:8428/api/v1/writequeue_config:max_samples_per_send: 10000capacity: 20000max_shards: 30
重启Prometheus
通过Prometheus expression browser查看Node Exporter 指标
特定于Node Exporter的指标前缀为Node_
,包括Node_cpu_seconds_total
和Node_Exporter_build_info
等指标。
点击以下链接查看一些示例指标
Metric | Meaning |
---|---|
rate(node_cpu_seconds_total{mode="system"}[1m]) |
最近一分钟,每秒在系统模式下消耗的CPU时间平均值(以秒为单位) |
node_filesystem_avail_bytes |
非root用户可用的文件系统空间(以字节为单位) |
rate(node_network_receive_bytes_total[1m]) |
最近一分钟,每秒接收的平均网络流量(以字节为单位) |
参考链接:https://prometheus.io/docs/guides/node-exporter/#monitoring-linux-host-metrics-with-the-node-exporter