默认情况下,prometheus将采集到的数据存储在本地的TSDB数据库中国,路径默认为prometheus安装目录的data目录.数据写入过程为先把数据写入wal日志并存放在内存,然后2小时候将内存数据保存至一个新的block块,同时再把新采集的数据写入内存并在2小时候再保存至一个新的block块,以此类推.
每个Block都是一个目录,以01开头.
# ls -l data/
total 20
drwxr-xr-x 3 root root 68 Aug 30 15:00 01GBPRSMVSBPZ320WVP2WCSAYQ
drwxr-xr-x 3 root root 68 Aug 31 13:00 01GBS4AJ6FJE1VSV8GF3JX6K7C
drwxr-xr-x 3 root root 68 Sep 1 08:10 01GBV65331GM9CX1WRVYZXTAEF
drwxr-xr-x 3 root root 68 Sep 1 13:00 01GBVPQB6HKD8G5N4H6K468XSC
drwxr-xr-x 3 root root 68 Sep 2 13:00 01GBY9414DKHWZ0Z2WRYMZAGGJ
drwxr-xr-x 3 root root 68 Sep 3 13:00 01GC0VGVQ7WMJXA9PTMVS59R6F
drwxr-xr-x 3 root root 68 Sep 3 19:00 01GC1G40KSVB2WHN87W2FXY3VG
drwxr-xr-x 3 root root 68 Sep 5 13:06 01GC60P2FAWNHACKMHVMDF1HR7
drwxr-xr-x 3 root root 68 Sep 6 11:00 01GC8BV57RWVX7D3ZGPVFW83F0
drwxr-xr-x 3 root root 68 Sep 7 07:42 01GCAJY36S7WAQ4334B4G4143R
drwxr-xr-x 3 root root 68 Sep 7 13:02 01GCB584JA8G51VPH3PRCX1BS1
drwxr-xr-x 3 root root 68 Sep 8 07:47 01GCD5KDZVQJ5ACKMTJR5DZFRJ
drwxr-xr-x 3 root root 68 Sep 8 07:47 01GCD5KETT01RSA4C30N3RAHQF
drwxr-xr-x 3 root root 68 Sep 8 07:47 01GCD5KFMN9HQZQSFK6C48869E
drwxr-xr-x 2 root root 34 Sep 8 08:00 chunks_head
-rw-r--r-- 1 root root 0 Sep 7 15:34 lock
-rw-r--r-- 1 root root 20001 Sep 8 08:18 queries.active
drwxr-xr-x 3 root root 113 Sep 8 07:47 wal
block会进行压缩,合并历史数据块,以及删除过期块,随着压缩,合并,block的数量会减少,在压缩过程中会发生三件事:定期执行压缩,合并小的block到大的block,清理过期块
每个块由5部分组成
文件/目录 | 作用 |
---|---|
chunks | 数据目录,通过–storage.tsdb.retention.size设定默认切割文件大小 |
chunks/000001 | 数据文件,如果数据量大还会有000002,000003…不足512Mb就只有1个 |
index | 索引文件,记录存储的数据索引信息,通过文件内的几个表来查找时序数据 |
meta.json | block元数据信息,包含了样本数,采集数据的起始时间,压缩历史 |
tombstones | 逻辑数据,主要记载删除和标记要删除的内容,删除标记,可在查询块时排除样本 |
# tree data/01GBPRSMVSBPZ320WVP2WCSAYQ/
data/01GBPRSMVSBPZ320WVP2WCSAYQ/
├── chunks
│ └── 000001
├── index
├── meta.json
└── tombstones
1 directory, 4 files
参数 | 含义 |
---|---|
–config.file=“prometheus.yml” | 指定配置文件 |
–web.listen-address=“0.0.0.0:9090” | 指定监听地址和端口 |
–storage.tsdb.path=“data/” | 指定数据存储目录 |
–storage.tsdb.retention.size=B,KB,MB,GB,TB,PB,EB | 指定chunk大小,默认512MB |
–storage.tsdb.retention.time=15 | 数据保存时长,默认15天 |
–query.timeout=2m | 最大查询超时时间 |
–query.max-concurrency=20 | 最大查询并发数 |
–web.read-timeout=5m | 最大空闲超时时间 |
–web.max-connections=512 | 最大并发连接数 |
–web.enable-lifecycle | 启用API动态加载配置功能 |
victoriametrics优点: 配置简单,可以实现读写分离
架构图如下:
单机版本无法实现读写分离
root@logstash:/apps# wget https://github.com/VictoriaMetrics/VictoriaMetrics/releases/download/v1.81.1/victoria-metrics-linux-amd64-v1.81.1.tar.gz
root@logstash:/apps# tar xf victoria-metrics-linux-amd64-v1.81.1.tar.gz
[Unit]
Description=victoria-metrics
After=network.target
[Service]
Restart=on-failure
WorkingDirectory=/apps/
ExecStart=/apps/victoria-metrics-prod -httpListenAddr=0.0.0.0:8428 -storageDataPath=/data/victoria -retentionPeriod=3m
[Install]
WantedBy=multi-user.target
启动参数
参数 | 含义 |
---|---|
-httpListenAddr=0.0.0.0:8428 | 监听地址和端口 |
-storageDataPath | 数据持久化目录,默认是victoria当前目录下的victoria-metrics-data |
-retentionPeriod | 数据保留时间,默认为1个月.默认单位为m(月),支持h(hour),d(day),w(week),y(year) |
启动服务
root@logstash:/apps# systemctl enable --now victoria-metrics.service
Created symlink /etc/systemd/system/multi-user.target.wants/victoria-metrics.service → /etc/systemd/system/victoria-metrics.service.
root@logstash:/apps# ss -ntl|grep 8428
LISTEN 0 4096 0.0.0.0:8428 0.0.0.0:*
启动后可以通过web访问页面
在prometheus.yml的global同级加入
global:
xxxx 略
remote_write:
- url: http://192.168.31.126:8428/api/v1/write
重启prometheus,此时在victoria:8428/vmui上做查询就可以看到数据了
接入数据,数据类型为Prometheus,Url写入victoria的地址
导入模板,数据源使用victoria
8919
集群组件:
root@rabbitmq-1:/apps# wget https://github.com/VictoriaMetrics/VictoriaMetrics/releases/download/v1.81.1/victoria-metrics-linux-amd64-v1.81.1-cluster.tar.gz
root@rabbitmq-1:/apps# tar xf victoria-metrics-linux-amd64-v1.81.1-cluster.tar.gz
root@rabbitmq-1:/apps# mv vm*-prod /usr/local/bin/
root@rabbitmq-1:/apps# mkdir -p /data/vmstorage-data
/etc/systemd/system/vmstorage.service
[Unit]
Description=Vmstorage Server
After=network.target
[Service]
Restart=on-failure
WorkingDirectory=/apps/ExecStart=/usr/local/bin/vmstorage-prod --loggerTimezone Asia/Shanghai -storageDataPath /data/vmstorage-data
[Install]
WantedBy=multi-user.target
默认端口:
服务 | 监听端口 |
---|---|
vmselect | 8481 |
vmstorage | 8482,对 vminsert写提供8400,对vmselect读提供8401 |
vminsert | 8480 |
vminsert写 | 8400 |
vmselect读 | 8401 |
将配置同步到另外两台服务器上
# scp /etc/systemd/system/vmstorage.service rabbitmq-2:/etc/systemd/system/
# scp /etc/systemd/system/vmstorage.service rabbitmq-3:/etc/systemd/system/
# scp /usr/local/bin/vm* rabbitmq-2:/usr/local/bin/
# scp /usr/local/bin/vm* rabbitmq-3:/usr/local/bin/
启动vmstorage
root@rabbitmq-1:/apps# systemctl enable --now vmstorage.service
Created symlink /etc/systemd/system/multi-user.target.wants/vmstorage.service → /etc/systemd/system/vmstorage.service.
root@rabbitmq-1:/apps# ss -ntl|grep 8482
LISTEN 0 20480 0.0.0.0:8482 0.0.0.0:*
/etc/systemd/system/vminsert.service
[Unit]
Description=Vminsert Server
After=network.target
[Service]
Restart=on-failure
WorkingDirectory=/apps/
ExecStart=/usr/local/bin/vminsert-prod -storageNode=192.168.31.51:8400,192.168.31.52:8400,192.168.31.53:8400
[Install]
WantedBy=multi-user.target
复制到其他节点
root@rabbitmq-1:/apps# scp /etc/systemd/system/vminsert.service rabbitmq-2:/etc/systemd/system/vminsert.service
vminsert.service 100% 257 701.7KB/s 00:00
root@rabbitmq-1:/apps# scp /etc/systemd/system/vminsert.service rabbitmq-3:/etc/systemd/system/vminsert.service
vminsert.service 100% 257 871.3KB/s 00:00
启动服务
root@rabbitmq-1:/apps# systemctl enable --now vminsert
Created symlink /etc/systemd/system/multi-user.target.wants/vminsert.service → /etc/systemd/system/vminsert.service.
root@rabbitmq-1:/apps# ss -ntl|grep 8480
LISTEN 0 20480 0.0.0.0:8480 0.0.0.0:*
同时其他两台服务器也启动这2个服务
# systemctl enable --now vmstorage vminsert
root@rabbitmq-2:~# ss -ntl|grep 84
LISTEN 0 20480 0.0.0.0:8400 0.0.0.0:*
LISTEN 0 20480 0.0.0.0:8401 0.0.0.0:*
LISTEN 0 20480 0.0.0.0:8480 0.0.0.0:*
LISTEN 0 20480 0.0.0.0:8482 0.0.0.0:*
root@rabbitmq-3:~# ss -ntl|grep 84
LISTEN 0 20480 0.0.0.0:8480 0.0.0.0:*
LISTEN 0 20480 0.0.0.0:8482 0.0.0.0:*
LISTEN 0 20480 0.0.0.0:8400 0.0.0.0:*
LISTEN 0 20480 0.0.0.0:8401 0.0.0.0:*
/etc/systemd/system/vmselect.service
[Unit]
Description=Vmselect Server
After=network.target
[Service]
Restart=on-failure
WorkingDirectory=/apps/
ExecStart=/usr/local/bin/vmselect-prod -storageNode=192.168.31.51:8401,192.168.31.52:8401,192.168.31.53:8401
[Install]
WantedBy=multi-user.target
启动服务
root@rabbitmq-3:~# systemctl enable --now vmselect.service
Created symlink /etc/systemd/system/multi-user.target.wants/vmselect.service → /etc/systemd/system/vmselect.service.
root@rabbitmq-3:~# ss -ntl|grep 8481
LISTEN 0 20480 0.0.0.0:8481 0.0.0.0:*
将服务同步到其他服务器,并启动
root@rabbitmq-1:/apps# scp /etc/systemd/system/vmselect.service rabbitmq-2:/etc/systemd/system/vmselect.service
root@rabbitmq-1:/apps# ssh rabbitmq-2 'systemctl enable --now vmselect.service&& ss -ntl|grep 8482'
Created symlink /etc/systemd/system/multi-user.target.wants/vmselect.service → /etc/systemd/system/vmselect.service.
LISTEN 0 20480 0.0.0.0:8482 0.0.0.0:*
root@rabbitmq-1:/apps# scp /etc/systemd/system/vmselect.service rabbitmq-3:/etc/systemd/system/vmselect.service
root@rabbitmq-1:/apps# ssh rabbitmq-3 'systemctl enable --now vmselect.service&& ss -ntl|grep 8482'
Created symlink /etc/systemd/system/multi-user.target.wants/vmselect.service → /etc/systemd/system/vmselect.service.
LISTEN 0 20480 0.0.0.0:8482 0.0.0.0:*
确保这些ip上的端口监听都正常
# curl 192.168.31.51:8480/metrics
# curl 192.168.31.51:8481/metrics
# curl 192.168.31.51:8482/metrics
# curl 192.168.31.52:8480/metrics
# curl 192.168.31.52:8481/metrics
# curl 192.168.31.52:8482/metrics
# curl 192.168.31.53:8480/metrics
# curl 192.168.31.53:8481/metrics
# curl 192.168.31.53:8482/metrics
修改prometheus.yml配置
remote_write:
- url: http://192.168.31.51:8480/insert/0/prometheus
- url: http://192.168.31.52:8480/insert/0/prometheus
- url: http://192.168.31.53:8480/insert/0/prometheus
重启prometheus
# systemctl restart prometheus
13824
默认情况下,数据被vminsert的组件基于hash算法分别持久化到不同的vmstorage上,可以使用vminsert组件支持的-replicationFactor=N复制功能,将数据分别在各个节点保存一份完整的副本,以实现数据高可用.