看到网上的资料甚少,可能各位大佬都不屑于写,俺这个小渣渣自己摸索尝试实现TSD的负载均衡,如果你想尝试实现TSD的负载均衡,首先得配置opentsdb的环境,可参考这篇文章。
Step-1:把opentsdb的配置文件复制多份,他们的区别只在于端口不同
root@xxxxxx/opentsdb-2.4.0/src# ls | grep opentsdb
opentsdb.conf
opentsdb_TSD_1.conf
opentsdb_TSD_2.conf
opentsdb_TSD_3.conf
root@xxxxxxx/opentsdb-2.4.0/src# cat opentsdb.conf
tsd.network.port = 4242
Step-2:分别使用不同的配置文件进行启动TSD(使用screen进行启动)
./build/tsdb tsd --config=./src/opentsdb.conf &
./build/tsdb tsd --config=./src/opentsdb_TSD_1.conf &
./build/tsdb tsd --config=./src/opentsdb_TSD_2.conf &
./build/tsdb tsd --config=./src/opentsdb_TSD_3.conf &
Step-3:打开对应端口的防火墙
iptables -I INPUT -p tcp --dport 4242 -j ACCEPT
iptables -I INPUT -p tcp --dport 4243 -j ACCEPT
iptables -I INPUT -p tcp --dport 4244 -j ACCEPT
iptables -I INPUT -p tcp --dport 4245 -j ACCEPT
此时,各个TSD可以独立接受读写请求,每个TSD是独立的,没有master,没有共享状态
Step-4:使用Nginx进行负载均衡
# 安装nginx
wget http://nginx.org/download/nginx-1.12.2.tar.gz
tar -zxvf nginx-1.12.2.tar.gz
cd nginx-1.12.2/
apt-get update
apt-get install libpcre3 libpcre3-dev
apt-get install zlib1g-dev
./configure
# 编译安装(文件位置/usr/local/nginx)
make
make install
编辑nginx.conf
文件,更改配置如下(只写关键部分)
# 配置负载均衡(算法:加权轮询)
http {
upstream testServer {
server localhost:4242 weight=3;
server localhost:4243 weight=3;
server localhost:4244 weight=3;
server localhost:4245 weight=3;
}
server {
listen 4246;
server_name localhost;
location / {
proxy_pass http://testServer;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
}
开启防火墙,前端请求4246端口即可。
iptables -I INPUT -p tcp --dport 4246 -j ACCEPT
网友评论