influxdb
开源版本不支持集群模式,社区开源的方案有多种,本文主要介绍官方提供的高可用方案 influxdb-relay
。
架构图
┌─────────────────┐
│writes & queries │
└─────────────────┘
│
▼
┌───────────────┐
│ │
┌────────│ Load Balancer │─────────┐
│ │ │ │
│ └──────┬─┬──────┘ │
│ │ │ │
│ │ │ │
│ ┌──────┘ └────────┐ │
│ │ ┌─────────────┐ │ │┌──────┐
│ │ │/write or UDP│ │ ││/query│
│ ▼ └─────────────┘ ▼ │└──────┘
│ ┌──────────┐ ┌──────────┐ │
│ │ InfluxDB │ │ InfluxDB │ │
│ │ Relay │ │ Relay │ │
│ └──┬────┬──┘ └────┬──┬──┘ │
│ │ | | │ │
│ | ┌─┼──────────────┘ | │
│ │ │ └──────────────┐ │ │
│ ▼ ▼ ▼ ▼ │
│ ┌──────────┐ ┌──────────┐ │
│ │ │ │ │ │
└─▶│ InfluxDB │ │ InfluxDB │◀─┘
│ │ │ │
└──────────┘ └──────────┘
架构比较简单,由一个负载均衡器、两个或多个 InfluxDB Relay
和两个或多个 InfluxDB
后端数据库组成。InfluxDB Relay
对外提供 http 服务,写请求可通 http 或 udp 方式向 influxdb 写入数据,不支持读请求,也不支持数据库 schema 的修改( create、drop等 )。在写入数据之前必须预先创建好数据库。
集群节点
Node | Role |
---|---|
192.168.1.5 | influxdb-relay |
192.168.1.6 | influxdb |
192.168.1.7 | influxdb |
安装 influxdb
$ wget https://dl.influxdata.com/influxdb/releases/influxdb-1.8.2.x86_64.rpm
$ yum install -y influxdb-1.8.2.x86_64.rpm
// 启动服务
$ systemctl start influxdb.service
部署 influxdb-relay
安装 Golang
$ wget https://dl.google.com/go/go1.15.2.linux-amd64.tar.gz
$ tar -C /usr/local -xzf go1.15.2.linux-amd64.tar.gz
// 写入环境变量
$ vi ~/.bash_profile
...
PATH=$PATH:$HOME/bin:/usr/local/go/bin
...
$ source ~/.bash_profile
$ go version
go version go1.15.2 linux/amd64
安装 influxdb-relay
$ yum install git -y
$ go get -u github.com/influxdata/influxdb-relay
$ mkdir /etc/influxdb-relay
$ cp $GOPATH/src/github.com/influxdata/influxdb-relay/sample.toml /etc/influxdb-relay/relay.toml
// 编辑配置文件
$ cat /etc/influxdb-relay/relay.toml
[[http]]
name = "influx-http"
bind-addr = "127.0.0.1:9096"
output = [
{ name="db1", location = "http://192.168.1.6:8086/write" },
{ name="db2", location = "http://192.168.1.7:8086/write" },
]
systemd 守护进程
$ cp /root/go/bin/influxdb-relay /usr/local/bin/
// 编辑 systemd service 配置
$ cat /etc/systemd/system/influxdb-relay.service
[Unit]
Description=influxdb relay
After=syslog.target
After=network.target
[Service]
Type=simple
ExecStart=/usr/local/bin/influxdb-relay -config /etc/influxdb-relay/relay.toml
Restart=always
ProtectSystem=full
PrivateDevices=yes
PrivateTmp=yes
NoNewPrivileges=true
[Install]
WantedBy=multi-user.target
nginx 代理
influxdb-relay 提供双写策略,读请求可通过 nginx 进行代理转发
$ cat /etc/nginx/conf.d/influxdb-relay.conf
upstream db {
ip_hash;
server 192.168.1.6:8086 max_fails=1 fail_timeout=10s;
server 192.168.1.7:8086 max_fails=1 fail_timeout=10s;
}
server {
listen 9097;
location /ping {
proxy_pass http://db;
}
location /query {
proxy_pass http://db;
}
location /write {
limit_except POST {
deny all;
}
proxy_pass http://127.0.0.1:9096;
}
}
测试
write
$ curl -i -XPOST http://192.168.1.5:9096/write\?db\=quark_test --data-binary 'cpu_usage,host=master1 value=0.2'
HTTP/1.1 204 No Content
Date: Sun, 27 Sep 2020 07:44:53 GMT
192.168.1.6
$ influx -precision rfc3339 -database quark_test
Connected to http://localhost:8086 version 1.8.2
InfluxDB shell version: 1.8.2
> select * from cpu_usage;
name: cpu_usage
time host value
---- ---- -----
2020-09-27T07:44:53.912364901Z master1 0.2
192.168.1.7
$ influx -precision rfc3339 -database quark_test
Connected to http://localhost:8086 version 1.8.2
InfluxDB shell version: 1.8.2
> select * from cpu_usage;
name: cpu_usage
time host value
---- ---- -----
2020-09-27T07:44:53.912364901Z master1 0.2
read
$ curl -i -XPOST http://192.168.1.5:9097/query\?db\=quark_test --data-urlencode "q=select * from cpu_usage"
{"results":[{"statement_id":0,"series":[{"name":"cpu_usage","columns":["time","host","value"],"values":[["2020-09-27T07:44:53.912364901Z","master1",0.2],["2020-09-27T07:57:01.88854361Z","master1",0.3],["2020-09-27T09:04:08.122066387Z","master1",0.4],["2020-09-27T09:05:29.693797445Z","master1",0.5]]}]}]}