Prometheus+TDengine集群实现监控体系高可用

news/2024/5/18 22:53:58 标签: prometheus, tdengine, 高可用监控体系

背景

为避免再次出现因Prometheus宕机导致业务无法查看历史数据受到影响,准备将Prometheus架构从单节点方式升级为高可用集群方式并将后端存储由本地存储改为远端分布式时序数据库存储。分布式时序数据库采用国产数据库TDengine。

架构

解释:虚线代表Prometheus master节点的Prometheus服务故障之后的线路。

IP地址主机名操作系统软件网卡名
10.0.0.10grafanaUbuntu 20.04.4 LTS arm64架构grafana_10.3.1ens160
10.0.0.11prometheus01Ubuntu 20.04.4 LTS arm64架构node_exporter-1.7.0, prometheus 2.45.3, keepalivedens160
10.0.0.12TDengine01Ubuntu 20.04.4 LTS arm64架构TDengine-server-3.0.3.0ens160
10.0.0.13prometheus02Ubuntu 20.04.4 LTS arm64架构node_exporter-1.7.0, prometheus 2.45.3, keepalivedens160
10.0.0.14TDengine02Ubuntu 20.04.4 LTS arm64架构TDengine-server-3.0.3.0ens160
10.0.0.15TDengine03Ubuntu 20.04.4 LTS arm64架构TDengine-server-3.0.3.0ens160
10.0.0.16TDengine04Ubuntu 20.04.4 LTS arm64架构TDengine-server-3.0.3.0ens160

解释:

在10.0.0.11和10.0.0.13上分别部署node_exporter-1.7.0, prometheus 2.45.3, keepalived,并且将Prometheus01的权重调高,Prometheus02处于stop状态;
Grafana连接Prometheus的VIP地址;
Prometheus01 读写设置为TDengine01节点; 
Prometheus02 读写设置为TDengine02节点;
编写Keepalived脚本 实现当Prometheus01故障时 自动开启Prometheus02;
TDengine是集群方式

操作步骤

1.修改主机名

hostnamectl set-hostname grafana
hostnamectl set-hostname prometheus01
hostnamectl set-hostname prometheus02
hostnamectl set-hostname TDengine01
hostnamectl set-hostname TDengine02
hostnamectl set-hostname TDengine03
hostnamectl set-hostname TDengine04

2.设置时区以及时间同步

全部都要机器都要操作

# 设置时区
timedatectl set-timezone Asia/Shanghai
# 安装基础软件
apt install -y lrzsz net-tools ntpdate
# 同步时间
/usr/sbin/ntpdate ntp1.aliyun.com
crontab -l > crontab_conf ; echo "*/5 * * * * /usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1" >> crontab_conf && crontab crontab_conf && rm -f crontab_conf
timedatectl set-local-rtc 1

3.配置/etc/hosts

TDengine机器需要操作

vim /etc/hosts

10.0.0.12 TDengine01
10.0.0.14 TDengine02
10.0.0.15 TDengine03
10.0.0.16 TDengine04

4.安装Prometheus

只是安装Prometheus暂时不要启动,后面对配置文件更改后再启动prometheus01,02不启动;

wget https://github.com/prometheus/prometheus/releases/download/v2.45.3/prometheus-2.45.3.linux-arm64.tar.gz

mv prometheus-2.45.3.linux-arm64.tar.gz /etc/
cd /etc
tar -zxvf prometheus-2.45.3.linux-arm64.tar.gz
mv prometheus-2.45.3.linux-arm64 prometheus
rm -rf prometheus-2.45.3.linux-arm64.tar.gz
cd prometheus
mkdir data
mv prometheus promtool  /usr/local/bin/

cat > /etc/systemd/system/prometheus.service << EOF
[Unit]
Description=Prometheus
Documentation=https://prometheus.io/
After=network.target
[Service]
ExecStart=/usr/local/bin/prometheus --config.file=/etc/prometheus/prometheus.yml --storage.tsdb.path=/etc/prometheus/data --web.listen-address=0.0.0.0:9090
WorkingDirectory=/etc/prometheus/
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF

5.安装node_export

wget https://github.com/prometheus/node_exporter/releases/download/v1.7.0/node_exporter-1.7.0.linux-arm64.tar.gz
tar -xvzf node_exporter-1.7.0.linux-arm64.tar.gz
cp node_exporter-1.7.0.linux-arm64/node_exporter /usr/local/bin/node_exporter
rm -rf  node_exporter-1.7.0.linux-arm64*
cat > /etc/systemd/system/node_exporter.service << EOF
[Unit]
Description=node_exporter
Documentation=https://prometheus.io/
After=network.target

[Service]
ExecStart=/usr/local/bin/node_exporter
Restart=on-failure
RestartSec=20

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
systemctl restart node_exporter
systemctl enable node_exporter
systemctl status node_exporter

6.安装keepalived

prometheus01和prometheus02都需要进行安装并配置

apt -y install keepalived ipvsadm
systemctl enable keepalived

prometheus01机器Keepalived配置文件

vim /etc/keepalived/keepalived.conf

! Configuration File for keepalived
global_defs {
   router_id 1
}
vrrp_script check_prome {
    script "/etc/keepalived/check_prome.sh"
    interval 1
    weight -50
}
vrrp_instance VI_1 {
    state MASTER
    interface ens160
    virtual_router_id 1
    mcast_src_ip 10.0.0.11
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 123456
    }
    track_script {
       check_prome
    }
    virtual_ipaddress {
        10.0.0.20
    }
    notify_master "/etc/keepalived/notify.sh master"
    notify_backup "/etc/keepalived/notify.sh backup"
    notify_fault "/etc/keepalived/notify.sh fault"
}

prometheus02机器Keepalived配置文件

vim /etc/keepalived/keepalived.conf

! Configuration File for keepalived
global_defs {
   router_id 2
}

vrrp_instance VI_1 {
    state BACKUP
    interface ens160
    virtual_router_id 1
    mcast_src_ip 10.0.0.13
    priority 80
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 123456
    }
    virtual_ipaddress {
        10.0.0.20
    }
    notify_master "/etc/keepalived/notify.sh master"
    notify_backup "/etc/keepalived/notify.sh backup"
    notify_fault "/etc/keepalived/notify.sh fault"
}

注释

notify_master 当前节点成为主节点时触发脚本
notify_backup 当前节点成为备节点时触发脚本
notify_fault 当前节点转为"失败"状态时触发的脚本

7.设置Keepalived脚本

prometheus_check.sh脚本

只需要在Prometheus01机器上设置

vim /etc/keepalived/check_prome.sh
chmod +x /etc/keepalived/check_prome.sh
记得创建脚本后赋予执行权限
#!/bin/bash
processes_num=$(ps -ef |grep [p]rometheus|wc -l)
if [ $processes_num = 0 ]
then
   exit 1
else
   exit 0
fi

prometheus01 prometheus02机器都需要设置,内容一致。

notify.sh脚本

vim /etc/keepalived/notify.sh
chmod +x /etc/keepalived/notify.sh
记得创建脚本后赋予执行权限
#!/bin/bash
case $1 in
    master)
        systemctl start prometheus
        ;;
    backup)
        systemctl stop prometheus
        ;;
    fault)
        systemctl stop prometheus
        ;;
    *)
        echo "不支持该参数,请检查输入的参数是否正确"
esac

8.安装TDengine集群

wget https://www.taosdata.com/assets-download/3.0/TDengine-server-3.0.3.0-Linux-arm64.tar.gz

tar -zxvf TDengine-server-3.0.3.0-Linux-arm64.tar.gz
cd TDengine-server-3.0.3.0
./install.sh

修改TDengine集群配置文件

各节点配置文件中的firstEp 配置保持一致

vim /etc/taos/taos.cfg
firstEp                   TDengine01:6030
fqdn                      TDengine01
serverPort                6030

# 暂时保留 可能没用
monitor 1
monitorFQDN TDengine01
audit 1

启动TDengine服务

systemctl enable taosd
systemctl enable taosadapter
systemctl restart taosd
systemctl restart taosadapter
systemctl status taosd
systemctl status taosadapter

taos
show dnodes;

CREATE DNODE "TDengine02:6030";
CREATE DNODE "TDengine03:6030";
CREATE DNODE "TDengine04:6030";
show dnodes;

# 保留时间为1天
CREATE DATABASE prometheus KEEP 1 DURATION 1;
use prometheus;
show stables;
select * from metrics limit 10\G;

修改默认密码

# 修改root密码
SHOW USERS;
ALTER USER root PASS 'NUma@numa1';

9.配置Prometheus01

vim /etc/prometheus/prometheus.yml

global:
  scrape_interval: 15s 
  evaluation_interval: 15s 
scrape_configs:
  - job_name: "prometheus"
    static_configs:
      - targets: ["10.0.0.11:9100", "10.0.0.13:9100"]
remote_write:
  - url: "http://10.0.0.12:6041/prometheus/v1/remote_write/prometheus"
    basic_auth:
      username: root
      password: NUma@numa1
    remote_timeout: 30s
    queue_config:
        capacity: 100000
        max_shards: 1000
        max_samples_per_send: 1000
        batch_send_deadline: 5s
        min_backoff: 30ms
        max_backoff: 100ms
remote_read:
  - url: "http://10.0.0.12:6041/prometheus/v1/remote_read/prometheus"
    basic_auth:
      username: root
      password: NUma@numa1
    remote_timeout: 10s
    read_recent: true

10.配置Prometheus02

Prometheus 设置从TDengine04节点写入读取数据

vim /etc/prometheus/prometheus.yml

global:
  scrape_interval: 15s 
  evaluation_interval: 15s 
scrape_configs:
  - job_name: "prometheus"
    static_configs:
      - targets: ["10.0.0.11:9100", "10.0.0.13:9100"]
remote_write:
  - url: "http://10.0.0.16:6041/prometheus/v1/remote_write/prometheus"
    basic_auth:
      username: root
      password: NUma@numa1
    remote_timeout: 30s
    queue_config:
        capacity: 100000
        max_shards: 1000
        max_samples_per_send: 1000
        batch_send_deadline: 5s
        min_backoff: 30ms
        max_backoff: 100ms
remote_read:
  - url: "http://10.0.0.16:6041/prometheus/v1/remote_read/prometheus"
    basic_auth:
      username: root
      password: NUma@numa1
    remote_timeout: 10s
    read_recent: true

11.启动Prometheus01

systemctl daemon-reload
systemctl start prometheus
systemctl enable prometheus
systemctl restart prometheus
systemctl status prometheus

启动keepalived并检查VIP

# 启动
systemctl start  keepalived
# 检查VIP
root@prometheus01:~# ip a

12.安装Grafana

sudo apt-get install -y adduser libfontconfig1 musl
wget https://dl.grafana.com/oss/release/grafana_10.3.1_arm64.deb
sudo dpkg -i grafana_10.3.1_arm64.deb
# 启动Grafana
sudo /bin/systemctl daemon-reload
sudo /bin/systemctl enable grafana-server
sudo /bin/systemctl start grafana-server
# 默认账户名密码
admin/admin
# 配置数据源时设置VIP地址 切记!!!
我这里设置的是http://10.0.0.20:9090/
# 导入Linux监控面板
8919

13.查看TDengine数据

taos -uroot -p'NUma@numa1'
use prometheus;
show stables;
select * from metrics limit 10\G;

14.实验验证

在压测磁盘的时候进行操作:

1、 关闭Prometheus01,查看VIP是否会转移到Prometheus02 并且自动设置Prometheus启动

2、启动Prometheus01,查看VIP是否回到了Prometheus01 并且Prometheus02的Prometheus服务关闭

验证操作过程中 显示磁盘读写的图表是否中断,全程图表没有中断则说明高可用架构已经实现。

# 压测命令
while true; do  dd if=/dev/nvme0n1p2  of=/testrw.dbf bs=4k && rm -rf /testrw.dbf; done

数据备份

taosdump  -uroot -p'NUma@numa1' -D prometheus  -o /root/backup/

数据还原

taosdump  -uroot -p'NUma@numa1' -D prometheus  -i /root/backup/

数据清理

TDengine 数据建模 | TDengine 文档 | 涛思数据 (taosdata.com)

数据库 | TDengine 文档 | 涛思数据 (taosdata.com)

采用Keep方式在创建数据库时指定数据保留时间,此处为测试,设置的是保留一天

CREATE DATABASE prometheus KEEP 1 DURATION 1;

周二下午4:48 第一条数据的时间(等周三下午4:48再查看一下试试)

监控TDengine

taosKeeper 是 TDengine 3.0 版本监控指标的导出工具,通过简单的几项配置即可获取 TDengine 的运行状态。taosKeeper 使用 TDengine RESTful 接口,所以不需要安装 TDengine 客户端即可使用。

taosKeeper | TDengine 文档 | 涛思数据 (taosdata.com)

编辑配置文件

root@lsy:~# vim /etc/taos/keeper.toml 
# Start with debug middleware for gin
debug = false

# Listen port, default is 6043
port = 6043

# log level
loglevel = "info"

# go pool size
gopoolsize = 50000

# interval for TDengine metrics
RotationInterval = "15s"

[tdengine]
host = "127.0.0.1"
port = 6041
username = "root"
password = "NUma@numa1"

# list of taosAdapter that need to be monitored
[taosAdapter]
address = ["127.0.0.1:6041"]

[metrics]
# metrics prefix in metrics names.
prefix = "taos"

# database for storing metrics data
database = "log"

# export some tables that are not super table
tables = []

[environment]
# Whether running in cgroup.
incgroup = false

启动

systemctl start taoskeeper
systemctl enable taoskeeper
systemctl status taoskeeper

查看监控结果

$ taos -uroot -p'NUma@numa1'
# 如上示例,使用 log 库作为监控日志存储位置
> use log;
> select * from cluster_info limit 1;

结果示例:

taos> select * from cluster_info limit 1;
           ts            |            first_ep            | first_ep_dnode_id |   version    |    master_uptime     | monitor_interval |  dbs_total  |  tbs_total  | stbs_total  | dnodes_total | dnodes_alive | mnodes_total | mnodes_alive | vgroups_total | vgroups_alive | vnodes_total | vnodes_alive | connections_total |  protocol   |           cluster_id           |
===============================================================================================================================================================================================================================================================================================================================================================================
 2024-02-20 11:08:22.409 | lsy:6030                       |                 1 | 3.0.3.0      |              0.00000 |               30 |           2 |           8 |          19 |            1 |            1 |            1 |            1 |             4 |             4 |            4 |            4 |                 4 |           1 | 4072125278433533572            |
Query OK, 1 row(s) in set (0.005329s)

taos> 

导出监控指标

root@lsy:~# curl http://127.0.0.1:6043/metrics|wc -l
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 15575    0 15575    0     0  7604k      0 --:--:-- --:--:-- --:--:-- 7604k
261
root@lsy:~# 

集成Prometheus

  - job_name: "taoskeeper"
    static_configs:
      - targets: ["localhost:6043"]

grafana导入监控面板

# 面板ID
18587

请添加图片描述

参考文档

TDengine 发布历史及下载链接 | TDengine 文档 | 涛思数据 (taosdata.com)

TDengine 权限管理 | 用户增删改查,授权与撤销授权_tdengine 用户访问权限-CSDN博客

产品简介 - 《TDengine v3.0 中文文档》 - 书栈网 · BookStack


http://www.niftyadmin.cn/n/5385043.html

相关文章

MySQL8的ONLY_FULL_GROUP_BY SQL模式兼容问题

文章目录 1. 问题描述2. 解决方法1. 修改查询2. 修改SQL模式3. 使用ANY_VALUE()函数 1. 问题描述 Cause: java.sql.SQLSyntaxErrorException: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column btc-cloud.t1.id which is not funct…

C#上位机与三菱PLC的通信08---开发自己的通讯库(A-1E版)

1、A-1E报文回顾 具体细节请看&#xff1a; C#上位机与三菱PLC的通信03--MC协议之A-1E报文解析 C#上位机与三菱PLC的通信04--MC协议之A-1E报文测试 2、为何要开发自己的通讯库 前面使用了第3方的通讯库实现了与三菱PLC的通讯&#xff0c;实现了数据的读写&#xff0c;对于通…

【经验分享】自然语言处理技术有哪些局限性和挑战?

个人认为&#xff0c;主要是两个难点&#xff1a; 1.语料&#xff0c;通常的语料很好解决&#xff0c;用爬虫从互联网上就可以采集和标注训练。但是我们接触很多项目和客户需求都是专业性很强的&#xff0c;例如&#xff1a;航天材料、电气设备、地理信息、化学试剂 等等。往往…

get和set方法太多太臃肿?快使用 lombok

目录 0. lombok 介绍 1. lombok 使用 1.1 创建一个 maven 项目 1.2 在项目中引用依赖 1.3 在 idea 中添加 lombok 插件 1.4 使用 lombok 注解 1.5 Idea 运行报 Lombok requires enables annotation process 错误解决办法 0. lombok 介绍 当我们写一个类时&#xff0c;为了…

教学设计与课堂教学的关系是什么

当你走进教室&#xff0c;是否曾思考过这背后的精心策划&#xff1f;每一堂课的呈现&#xff0c;都源于细致的教学设计。那么&#xff0c;教学设计与课堂教学之间&#xff0c;又隐藏着怎样的秘密呢&#xff1f; 教学设计&#xff0c;就像是一部剧本&#xff0c;为演员&#xf…

飞常准查航班小程序采集

仅限学习使用 <html> <head> </head> <body><script src"AesUtil.js"></script><script src"md5.js"></script><script>function test(a) { return true; }function serialize(o) {var n argumen…

SVC模型的校准 Model Calibration

关于机器学习模型校准的文章写的比较好的&#xff1a;Sk-learn 中文社区-概率校准[1]、知乎-模型校准 Calibration[2]、CSDN-机器学习_预测概率校准[3]。 看了这些文章后&#xff0c;有几个点需要明确一下&#xff1a; 1、所谓的模型校准&#xff08;概率校准&#xff09;并不是…

智慧驿站_智慧文旅驿站_轻松的驿站智慧公厕_5G智慧公厕驿站_5G模块化智慧公厕

多功能城市智慧驿站是在智慧城市建设背景下&#xff0c;所涌现的一种创新型社会配套设施。其中&#xff0c;智慧公厕作为城市智慧驿站的重要功能基础&#xff0c;具备社会配套不可缺少的特点&#xff0c;所以在应用场景上&#xff0c;拥有广泛的需求和要求。那么&#xff0c;城…