RabbitMQ HAProxy高可用配置
1. 修改三台服务器 PC, node1, node2 的hosts文件,可以解析hostname到ip
2. 设置三台服务器的 erlang Cookie(文件地址:/var/lib/rabbitmq/.erlang.cookie)相同,只有这样不同服务器的RabbitMQ才可以通信
3. 在RabbitMQ的配置文件中设置集群配置,这样在RabbitMQ启动时就会自动加入进去,不需要每次手动设置
如node1节点的配置 cat /etc/rabbitmq/rabbitmq.conf
[
{rabbit,
[{cluster_nodes, {['rabbit@PC', 'rabbit@node2'],disc}}]
}
].
配置完成后,启动RabbimtMQ,查看集群状态
root@PC:~# rabbitmqctl cluster_status
Cluster status of node rabbit@PC
[{nodes,[{disc,[rabbit@PC,rabbit@node1,rabbit@node2]}]},
{running_nodes,[rabbit@node1,rabbit@node2,rabbit@PC]},
{cluster_name,<<"rabbit@PC">>},
{partitions,[]},
{alarms,[{rabbit@node1,[]},{rabbit@node2,[]},{rabbit@PC,[]}]}]
4. 配置成RabbitMQ启动后自动加入集群,RabbitMQ集群中各个节点除了队列,数据/状态都是各自保存相同的副本。消息队列只存储在创建该队列的节点上。一旦该服务器宕掉,就无法再读取该队列的消息,因此这样的RabbitMQ集群并不具备高可用性。为了高可用,还需要创建镜像队列。
创建镜像队列
rabbitmqctl set_policy pigeon_policy "^pigeon" '{"ha-mode":"nodes","ha-params":["rabbit@node1", "rabbit@node2"]}'
5. 安装配置HAProxy,并修改配置文件如下:
cat /etc/haproxy/haproxy.cfg
global
log 127.0.0.1 local0 notice
maxconn 10000
user haproxy
daemon
defaults
timeout connect 5s
timeout client 100s
timeout server 100s
listen rabbitmq
bind :5673
mode tcp
balance roundrobin
server PC 172.16.20.157:5672 check inter 5s rise 2 fall 3
server node2 172.16.20.61:5672 check inter 5s rise 2 fall 3
frontend front_rabbitmq_management
bind :15672
default_backend back_rabbitmq_management
backend back_rabbitmq_management
balance source
server PC PC:15672 check
server node2 node2:15672 check
如果连接HAProxy失败,看下是否是端口不通,如果不通则开通5673端口
firewall-cmd --permanent --add-port=5673/tcp
firewall-cmd --reload
1.如果服务器安装后修改了hostname,出现了 unable to connect to epmd (port 4369) on bogon: nxdomain (non-existing domain) 这个问题,请看下是否 /etc/hosts 文件、/etc/hostname文件和环境变量$HOSTNAME 是否一致。
2. 如果启动HAProxy出现 /etc/rc.d/init.d/haproxy: line 26: [: =: unary operator expected 这个错误,修改/etc/init.d/haproxy 文件的26行 [ ${NETWORKING} = "no" ] && exit 0 为 [ "${NETWORKING}" = "no" ] && exit 0
3. 如果Ubuntu 出现如下错误
rabbit@PC:
* connected to epmd (port 4369) on PC
* epmd reports: node 'rabbit' not running at all
no other nodes on PC
* suggestion: start the node
current node details:
- node name: 'rabbitmq-cli-15@PC'
- home dir: /var/lib/rabbitmq
- cookie hash: 3N+Ub3klGzwQE0bRAMczGA==
则 rm /var/log/rabbitmq/*, ps axu|grep rabbimtq 删掉rabbitmq进程,运行rabbitmq-server restart
4. 如果出现如下错误:
Error: {cannot_start_mnesia,
{{shutdown,{failed_to_start_child,mnesia_kernel_sup,killed}},
{mnesia_app,start,[normal,[]]}}}
则 rm -rf /var/lib/rabbitmq/mnesia, rm -rf /var/log/rabbitmq/* 然后重启rabbitmq server
5.如果出现如下错误:
[root@node2 ~]# rabbitmqctl status
Status of node rabbit@node2
Error: unable to connect to node rabbit@node2: nodedown
DIAGNOSTICS
===========
attempted to contact: [rabbit@node2]
rabbit@node2:
* unable to connect to epmd (port 4369) on node2: address (cannot connect to host/port)
在/etc/hosts文件里面添加解析到本地hostname的条目;如 192.168.22.55 myPC