CentOS7.4+HAProxy+Keepalived+EMQX搭建MQTT服务器高可用集群
操作系统:CentOS7.4。
版本:haproxy-1.8.19,keepalived-2.0.10,emqx3.0.1
环境配置:5台虚拟机,其中三台作为emqx服务器,两台用来装haproxy+keepalive实现负载均衡和高可用。
在192.168.206.135服务器上安装haproxy:
tar -xvf haproxy-1.8.19.tar.gz
进入解压目录
make TARGET=linux2628 ARCH=x86_64 PREFIX=/usr/local/haproxy
make install PREFIX=/usr/local/haproxy
使用uname -r查看系统版本,根据内核版本选择编译参数:
安装完成后查看版本:/usr/local/haproxy/sbin/haproxy -v
配置启动脚本:
复制haproxy文件到/usr/sbin下
cp /usr/local/haproxy/sbin/haproxy /usr/sbin/
复制haproxy脚本,到/etc/init.d下
cp ./examples/haproxy.init(解压目录) /etc/init.d/haproxy
sed -i ‘s#etc/$BASENAME#usr/local/haproxy/conf#g’ /etc/init.d/haproxy
chmod 755 /etc/init.d/haproxy
创建系统账号
useradd -r haproxy
创建配置文件
cd /usr/local/haproxy/
mkdir conf
cd conf
vi haproxy.cfg
配置文件内容—>>>
global
#设置日志
log 127.0.0.1 local3 info
chroot /usr/local/haproxy
#用户与用户组
user haproxy
group haproxy
#守护进程启动
daemon
#最大连接数
maxconn 4000
#默认配置
defaults
log global
option dontlognull
option http-server-close
retries 3
timeout http-request 10s
timeout queue 1m
timeout connect 60s
timeout client 2m
timeout server 2m
timeout http-keep-alive 10s
timeout check 10s
#前端配置
frontend emqx_front
bind *:1883
maxconn 4000
mode tcp
default_backend emqtt-backend
#后端配置,名称可自定义
backend emqtt-backend
#负载均衡方式
balance roundrobin
# 需要转发的ip及端口
server emqx1 192.168.206.129:1883 check inter 2000 rise 5 weight 1
server emqx2 192.168.206.130:1883 check inter 2000 rise 5 weight 1
server emqx3 192.168.206.131:1883 check inter 2000 rise 5 weight 1
frontend emqtt-admin-front
bind *:18083
mode http
default_backend emqtt-admin-backend
backend emqtt-admin-backend
mode http
balance roundrobin
server emq1 192.168.206.129:18083 check
server emq2 192.168.206.130:18083 check
server emq3 192.168.206.131:18083 check
listen admin_stats
stats enable
bind *:8080
mode http
option httplog
log global
maxconn 10
stats refresh 30s
stats uri /admin
stats realm haproxy
stats auth admin:admin
stats hide-version
stats admin if TRUE
打开rsyslog配置:vi /etc/rsyslog.conf
打开下面两行的注释
$ModLoad imudp
$UDPServerRun 514
并添加local3.* /var/log/haproxy.log
重启rsyslog:service rsyslog restart
启动HAProxy:service haproxy start
访问192.168.206.135:8080/admin,打开控制台:
192.168.206.136 haproxy的配置与135一样。
在192.168.206.135上安装Keepalived
安装 keepalived 需要用到 openssl:yum install gcc gcc-c++ openssl openssl-devel
tar -xvf keepalived-2.0.10.tar.gz
cd keepalived-2.0.10
./configure --prefix=/usr/local/keepalived(如果出现以下警告:WARNING - this build will not support IPVS with IPv6. Please install libnl/libnl-3 dev libraries to support IPv6 with IPVS.请安装:yum -y install libnl libnl-devel,重新执行./configure --prefix=/usr/local/keepalived)
make && make install
编辑配置文件keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
}
router_id mqtt40
vrrp_skip_check_adv_addr
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 79
priority 50
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.206.100
}
}
virtual_server 192.168.206.100 80{
delay_stop 6
lb_algo rr
lb_kind NAT
nat_mask 255.255.255.0
persistence_timeout 50
protocol TCP
real_server 192.168.206.135 80{
weight 1
}
real_server 192.168.206.136 80{
weight 1
}
}
配置:
mkdir /etc/keepalived
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf
cp ./keepalived/etc/init.d/keepalived(该目录为keepalived的解压目录) /etc/rc.d/init.d/keepalived
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/keepalived
cd /etc/init.d/
chmod +x keepalived
service keepalived start
如果出现这种情况Starting keepalived: /bin/bash: keepalived: command not found
[FAILED]
建立软连接即可:ln -s /usr/local/keepalived/sbin/keepalived /usr/bin/
192.168.206.136从机的配置文件keepalived.conf
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 79
priority 51
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.206.100
}
}
emqx集群搭建的文章地址:https://blog.csdn.net/Ferkl/article/details/88976900。
至此,已使用5台虚拟机搭建了一个简单的负载均衡,高可用的emqx服务器。