3台 主机环境
192.168.10.51 master node51 (HAProxy也配置在此)
192.168.10.52 node52
192.168.10.53 node53
node51|node52|node53 表明不同主机单独配置
如
192.168.10.51 配置为 node51
192.168.10.52 配置为 node52
添加 rabbimtq 运行依赖 erlang repo 源
cat > /etc/yum.repos.d/esl-erlang.repo << "EOF"
[erlang-solutions]
name=Centos $releasever - $basearch - Erlang Solutions
baseurl=http://binaries.erlang-solutions.com/rpm/centos/$releasever/$basearch
gpgcheck=1
gpgkey=http://binaries.erlang-solutions.com/debian/erlang_solutions.asc
enabled=1
EOF
安装 rabbimtq 运行依赖 erlang
yum install -y erlang
配置 nodename (固定node名称 不然每次启动node名称随机 可能会导致报nodename不匹配错误)
mkdir -p /etc/rabbitmq
touch /etc/rabbitmq/rabbitmq-env.conf
cat > /etc/rabbitmq/rabbitmq-env.conf << "EOF"
NODENAME=rabbit@node51|node52|node53
EOF
配置 hostname
mv /etc/hostname /etc/hostname.bak
echo "node51|node52|node53" > /etc/hostname
配置 node hosts
cat > /etc/hosts << "EOF"
127.0.0.1 node51|node52|node53
::1 node51|node52|node53
192.168.10.51 node51
192.168.10.52 node52
192.168.10.53 node53
EOF
添加 rabbitmq repo 源
cat > /etc/yum.repos.d/rabbitmq.repo << "EOF"
[bintray-rabbitmq-server]
name=bintray-rabbitmq-rpm
baseurl=https://dl.bintray.com/rabbitmq/rpm/rabbitmq-server/v3.7.x/el/7/
gpgcheck=1
gpgkey=https://github.com/rabbitmq/signing-keys/releases/download/2.0/rabbitmq-release-signing-key.asc
repo_gpgcheck=0
enabled=1
EOF
安装 rabbitmq
yum install -y rabbitmq-server
配置开机自启 rabbitmq
systemctl enable rabbitmq-server
启动 rabbitmq
systemctl start rabbitmq-server
检查状态
rabbitmqctl status
启用 管理插件 不启动的话 访问15672端口 404
rabbitmq-plugins enable rabbitmq_management
添加用户 (管理员)
rabbitmqctl add_user admin password
rabbitmqctl set_permissions admin ".*" ".*" ".*"
rabbitmqctl set_user_tags admin administrator
重启 rabbitmq
systemctl restart rabbitmq-server
在node51 node52 node53 机器都安装好 mq 之后 准备配置主从模式
检查node51主机的erlang cookie值
cat /var/lib/rabbitmq/.erlang.cookie
(只需要在从机上把主机的erlang cookie复制过来 对应教程中的 node52和node53中操作)
rabbitmqctl stop_app
scp root@node51:/var/lib/rabbitmq/.erlang.cookie /var/lib/rabbimtq/
chmod 400 /var/lib/rabbitmq/.erlang.cookie
chown rabbitmq:rabbitmq /var/lib/rabbitmq/.erlang.cookie
rabbitmqctl join_cluster rabbit@node51
rabbitmqctl start_app
rabbitmqctl cluster_status
优化网络配置
vim /etc/sysctl.conf
#### General gigabit tuning:
net.core.rmem_max = 8738000
net.core.wmem_max = 6553600
net.ipv4.tcp_rmem = 8192 873800 8738000
net.ipv4.tcp_wmem = 4096 655360 6553600
# VERY important to reuse ports in TCP_WAIT
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_max_tw_buckets = 360000
net.core.netdev_max_backlog = 2500
vm.min_free_kbytes = 65536
vm.swappiness = 0
fs.file-max = 655360
使配置生效
sysctl -p
systemctl restart rabbitmq-server
在任意一台mq上执行镜像策略
rabbitmqctl set_policy ha-all "^" '{"ha-mode":"all"}'
安装 haproxy
yum install -y haproxy
利用 haproxy 反代 rabitmq 配置负载均衡
cat > /etc/haproxy/haproxy.cfg << "EOF"
global
#日志输出配置,所有日志都记录在本机,通过local0输出
log 127.0.0.1 local0 notice
#改变当前的工作目录
chroot /var/lib/haproxy
#当前进程PID
pidfile /var/run/haproxy.pid
#最大连接数
maxconn 10000
user haproxy
group haproxy
#以守护进程方式运行haproxy #debug #quiet
daemon
# turn on stats unix socket
stats socket /var/lib/haproxy/stats
defaults
#应用全局的日志配置
log global
#默认的模式mode{tcp|http|health}
#tcp是4层,http是7层,health只返回OK
mode tcp
#日志类别tcplog
option tcplog
#不记录健康检查日志信息
option dontlognull
#3次失败则认为服务不可用
retries 3
#每个进程可用的最大连接数
maxconn 2000
#连接超时
timeout connect 10s
#客户端超时
timeout client 50s
#服务端超时
timeout server 50s
# 代理 rabbimtq 负载均衡
listen rabbitmq
bind :5671
#配置TCP模式
mode tcp
#加权轮询
balance roundrobin
#RabbitMQ集群节点配置,其中node51~node53为RabbitMQ集群节点ip地址
server node51 node51:5672 check inter 5s rise 2 fall 3
server node52 node52:5672 check inter 5s rise 2 fall 3
server node53 node53:5672 check inter 5s rise 2 fall 3
# weight - 调节服务器的负重
# check - 允许对该服务器进行健康检查
# inter - 设置连续的两次健康检查之间的时间,单位为毫秒(ms),默认值 2000(ms)
# rise - 指定多少次连续成功的健康检查后,可认定该服务器处于可操作状态,默认值 2
# fall - 指定多少次不成功的健康检查后,认为服务器为当掉状态,默认值 3
# maxconn - 指定可被发送到该服务器的最大并发连接数
#代理 rabbimtq 管理后台ui
listen rabbitmq_management
bind :15671
mode http
balance source
server node51 node51:15672 check
server node52 node52:15672 check
server node53 node53:15672 check
#haproxy状态监控
listen stats :9000
mode http
stats enable
stats hide-version
#设置haproxy监控地址为http://192.168.10.51:9000/
stats uri /
#haproxy监控 访问账号密码为 haproxy:haproxy
stats auth haproxy:haproxy
EOF
Thanks to demonkid98
Thanks to zch