[root@localhost Downloads]# wget http://www.rabbitmq.com/releases/erlang/erlang-18.3.4.4-1.el6.x86_64.rpm [root@localhost Downloads]# rpm -ivh erlang-18.3.4.4-1.el6.x86_64.rpm
[root@localhost Downloads]# wget http://www.rabbitmq.com/releases/rabbitmq-server/v3.6.6/rabbitmq-server-3.6.6-1.el6.noarch.rpm [root@localhost Downloads]# rpm -ivh rabbitmq-server-3.6.6-1.el6.noarch.rpm 出现以下错误: warning: rabbitmq-server-3.6.6-1.el6.noarch.rpm: Header V4 RSA/SHA1 Signature, key ID 6026dfca: NOKEY error: Failed dependencies: socat is needed by rabbitmq-server-3.6.6-1.el6.noarch 解决: [root@localhost Downloads]# yum install socat 出现以下错误: No package socat available. Error: Nothing to do 解决: [root@localhost Downloads]# wget --no-cache http://www.convirture.com/repos/definitions/rhel/6.x/convirt.repo -O /etc/yum.repos.d/convirt.repo 再执行 yum install socat 再执行安装命令 [root@localhost Downloads]# rpm -ivh rabbitmq-server-3.6.6-1.el6.noarch.rpm warning: rabbitmq-server-3.6.6-1.el6.noarch.rpm: Header V4 RSA/SHA1 Signature, key ID 6026dfca: NOKEY Preparing... ########################################### [100%] 1:rabbitmq-server ########################################### [100%]
[root@localhost bin]# pwd /usr/lib/rabbitmq/bin [root@localhost bin]# rabbitmq-server start RabbitMQ 3.6.6. Copyright (C) 2007-2016 Pivotal Software, Inc. ## ## Licensed under the MPL. See http://www.rabbitmq.com/ ## ## ########## Logs: /var/log/rabbitmq/rabbit@localhost.log ###### ## /var/log/rabbitmq/rabbit@localhost-sasl.log ########## Starting broker... completed with 6 plugins. 或者: [root@localhost bin]# service rabbitmq-server start
[root@localhost init.d]# cd /usr/lib/rabbitmq/bin/ [root@localhost bin]# ./rabbitmq-plugins enable rabbitmq_management
http://192.168.2.242:15672/ 能出现管理界面的时候就表示插件已经成功安装,可是,须要建立用户以后才能登陆,接下来看看一些经常使用的命令。node
#添加虚拟主机 [root@localhost bin]# ./rabbitmqctl add_vhost {vhostname} # 查看全部虚拟主机 [root@localhost bin]# ./rabbitmqctl list_vhosts # 删除虚拟主机 [root@localhost bin]# ./rabbitmqctl delete_vhost {vhostname} # 添加用户 [root@localhost bin]# ./rabbitmqctl add_user {username} {password} # 查看用户列表 [root@localhost bin]# ./rabbitmqctl list_users # 删除用户 [root@localhost bin]# ./rabbitmqctl delete_user {username} # 修改用户密码 [root@localhost bin]# ./rabbimqctl change_password {username} {newpassword} # 设定用户所属角色(一、administrator,二、monitoring, 三、management) [root@localhost bin]# ./rabbitmqctl set_user_tags test monitoring # 开启某个插件(重启服务器后生效) [root@localhost bin]# ./rabbitmq-plugins enable pluginname # 关闭某个插件(重启服务器后生效) [root@localhost bin]# ./rabbitmq-plugins disable pluginname # 权限管理(set_permissions [-pvhostpath] {user} {conf} {write} {read}) # conf:一个正则表达式match哪些配置资源可以被该用户访问。 # write:一个正则表达式match哪些配置资源可以被该用户写。 # read:一个正则表达式match哪些配置资源可以被该用户读。 [root@localhost bin]# ./rabbitmqctl set_permissions -p vhost_name username ".*" ".*" ".*" # 查看服务器状态信息 [root@localhost bin]# ./rabbitmqctl status # 查看队列列表 [root@localhost bin]# ./rabbitmqctl list_queues # 关闭应用 [root@localhost bin]# ./rabbitmqctl stop_app # 启动应用 [root@localhost bin]# ./rabbitmqctl start_app # 清除全部队列 [root@localhost bin]# ./rabbitmqctl reset # 查看某个vhost里的队列 [root@localhost bin]# ./rabbitmqctl list_queues -p vhost_name # 清除用户权限 [root@localhost bin]# ./rabbitmqctl clear_permissions -p vhostname username # 列出虚拟主机上的全部权限 [root@localhost bin]# ./rabbitmqctl list_permissions -p vhostpath # 列出用户权限 [root@localhost bin]# ./rabbitmqctl list_user_permissions username
#必须是在/etc/rabbitmq目录下 [root@localhost ~]# cd /etc/rabbitmq [root@localhost rabbitmq]#touch /etc/rabbitmq/rabbitmq-env.conf RABBITMQ_LOG_BASE=/var/log/rabbitmq [root@localhost rabbitmq]#touch /etc/rabbitmq/rabbitmq.config
Error: unable to connect to node rabbit@localhost: nodedownlinux
解决:服务没启动成功,具体的看看日志输出什么问题,再解决web
Error description:
{error,
{cannot_delete_plugins_expand_dir,
["/var/lib/rabbitmq/mnesia/rabbit@localhost-plugins-expand",
{cannot_delete,
"/var/lib/rabbitmq/mnesia/rabbit@localhost-plugins-expand",
eacces}]}}正则表达式
Log files (may contain more information):
/var/log/rabbitmq/rabbit@localhost.log
/var/log/rabbitmq/rabbit@localhost-sasl.logredis
Stack trace:
[{rabbit_plugins,setup,0,[{file,"src/rabbit_plugins.erl"},{line,71}]},
{rabbit,broker_start,0,[{file,"src/rabbit.erl"},{line,280}]},
{rabbit,start_it,1,[{file,"src/rabbit.erl"},{line,403}]},
{init,start_it,1,[]},
{init,start_em,1,[]}]vim
{"init terminating in do_boot",{error,{cannot_delete_plugins_expand_dir,["/var/lib/rabbitmq/mnesia/rabbit@localhost-plugins-expand",{cannot_delete,"/var/lib/rabbitmq/mnesia/rabbit@localhost-plugins-expand",eacces}]}}}
init terminating in do_boot ()bash
Crash dump is being written to: erl_crash.dump...done
解决:chown -R rabbitmq:rabbitmq /var/lib/rabbitmq/mnesia/服务器
=WARNING REPORT==== 8-Feb-2017::19:03:48 ===
HTTP access denied: user 'test' - invalid credentials
缘由:没有给用户分配角色,解决办法以下:cookie
# 设定用户所属角色(一、administrator,二、monitoring, 三、management) [root@localhost bin]# ./rabbitmqctl set_user_tags test monitoring
一、修改/etc/sysconfig/network 更新主机名,重启系统; 二、修改2台机器的hosts,配置域名解析 [root@localhost ~]# vim /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.168.2.242 server242 192.168.2.243 server243 192.168.2.244 server244 三、随便复制某一台机器上的erlang.cokie到另外一台,保证2台机器的 erlang.cookies文件里的值是一致的 (若是修改erlang cookie文件以前,erlang的进程和rabbitmq已经启动,把他们都stop或者kill掉。改完erlang cookie以后再重启) [root@server244 rabbitmq]# cd /var/lib/rabbitmq/ [root@server244 rabbitmq]# cat .erlang.cookie HZNVNRWDWVQJCNQCNSVI 把这个值复制到另外一台机器上,注意,这个erlang.cookie的默认权限是400,能够经过以下命令修改权限 [root@server242 rabbitmq]# chmod 777 .erlang.cookie 再修改里面的值 [root@server242 rabbitmq]# vim .erlang.cookie 修改完成以后把权限改回400 [root@server242 rabbitmq]# chmod 400 .erlang.cookie 一样的要修改${HOME}/.erlang.cookie里的值
[root@localhost bin]# ./rabbitmq-server -detached [root@localhost bin]# ./rabbitmqctl stop_app [root@localhost bin]# ./rabbitmqctl join_cluster rabbit@server114 [root@server242 bin]# ./rabbitmqctl start_app 查看集群加入状态 [root@server242 bin]# ./rabbitmqctl cluster_status
[root@server242 bin]# ./rabbitmqctl cluster_status Cluster status of node rabbit@server242 ... [{nodes,[{disc,[rabbit@server242,rabbit@server243,rabbit@server244]}]}, {running_nodes,[rabbit@server242,rabbit@server243,rabbit@server244]}, {cluster_name,<<"rabbit@server242">>}, {partitions,[]}, {alarms,[{rabbit@server242,[]},{rabbit@server243,[]},{rabbit@server244,[]}]}] 若是要修改集群节点类型 [root@server242 bin]# ./rabbitmqctl stop_app [root@server242 bin]# ./rabbitmqctl change_cluster_node_type ram [root@server242 bin]# ./rabbitmqtl start_app
退出集群: ./rabbitmqctl stop_app ./rabbitmqctl reset ./rabbitmqctl start_app
[root@server242 bin]# ./rabbitmqctl set_policy ha-all "^" '{"ha-mode":"all"}'
ps:app
若是整个集群重启,则先启动slave,再启动Master,到此,集群的搭建就完成了,可是要作到高可用,还得结合Haproxy,接下来我们就开始部署Haproxy。
[root@localhost ~]# wget http://fossies.org/linux/misc/haproxy-1.6.4.tar.gz [root@localhost ~]# tar -xvf haproxy-1.6.4.tar.gz
[root@localhost haproxy]# uname -r 2.6.32-642.15.1.el6.x86_64
[root@localhost haproxy]# make TARGET=linux2628 ARCH=x86_64 PREFIX=/usr/local/haproxy [root@localhost haproxy]# make install PREFIX=/usr/local/haproxy 参数解释: TARGET=linux2628 #内核版本,使用uname -r查看内核,如:2.6.18-371.el5,此时该参数就为linux26;kernel 大于2.6.28的用:TARGET=linux2628 ARCH=x86_64 #系统位数 PREFIX:安装路径
[root@localhost ~]# cd /usr/local/haproxy [root@localhost haproxy]# haproxy -v HA-Proxy version 1.6.4 2016/03/13 Copyright 2000-2016 Willy Tarreau <willy@haproxy.org>
有可能会遇到的问题:
[root@localhost sbin]# haproxy -v
-bash: /usr/sbin/haproxy: No such file or directory
解决:
[root@localhost sbin]# cp haproxy /usr/sbin/haproxy
1.6版本安装完成以后,是没有haproxy.cfg配置文件的,须要本身创建这个配置文件, 复制下面的配置信息,修改MQ集群每一个节点地址
global log 127.0.0.1 local0 log 127.0.0.1 local1 notice daemon nbproc 8 maxconn 4096 user haproxy group haproxy chroot /usr/local/haproxy defaults log global mode tcp option tcplog option dontlognull retries 3 option httpclose option abortonclose maxconn 4096 timeout connect 5000ms timeout client 10000ms timeout server 8000ms timeout check 3000 option httpclose balance roundrobin listen stats bind 0.0.0.0:1080 #登陆监控台的端口和地址 mode http option httplog log 127.0.0.1 local0 err warning maxconn 10 stats refresh 10s stats uri /stats #监控台地址:http://haproxy_ip:1080/stats stats realm Haproxy Manager stats auth user1:password #登陆监控台的帐户和密码 stats auth user2:password #登陆监控台的帐户和密码 stats hide-version stats admin if TRUE frontend Rabbitmq_frontend bind 0.0.0.0:5672 mode tcp option clitcpka timeout client 100m maxconn 10000 default_backend Rabbitmq_Backend backend Rabbitmq_Backend mode tcp timeout queue 100m timeout server 300m option srvtcpka option redispatch fullconn 10000 balance roundrobin option tcp-check server server242 192.168.2.242:5672 weight 3 maxconn 10000 check port 5672 inter 5000 rise 1 fall 2 backup server server243 192.168.2.243:5672 weight 3 maxconn 10000 check port 5672 inter 5000 rise 1 fall 2 server server244 192.168.2.244:5672 weight 3 maxconn 10000 check port 5672 inter 5000 rise 1 fall 2 listen rabbitmq_admin bind 0.0.0.0:8004 server node1 192.168.2.242:15672 server node2 192.168.2.243:15672 server node3 192.168.2.244:15672
[root@localhost haproxy]# haproxy -f haproxy.cfg 有可能报错: [ALERT] 071/223658 (16567) : parsing [../haproxy.cfg:7] : cannot find user id for 'haproxy' (0:Success) [ALERT] 071/223658 (16567) : parsing [../haproxy.cfg:8] : cannot find group id for 'haproxy' (0:Success) 解决: [root@localhost haproxy]# useradd haproxy -p /sbin/nologin
访问:http://192.168.2.241:1080/stats 能够看到已经把3台MQ加入负载了
由于咱们在haproxy配置文件里已经把RabbitMQ监控也加入进负载了,因此访问的时候也是访问haproxy的代理地址 http://192.168.2.241:8004
一点小问题,执行启动命令的时候有可能出现以下错误,多是配置文件出错,因此启动前最好先用命令测试一下配置文件是否正确
[root@localhost haproxy]# /usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/haproxy.cfg [ALERT] 060/220545 (3797) : sendmsg logger #1 failed: Connection refused (errno=111) [ALERT] 060/220545 (3797) : sendmsg logger #2 failed: Connection refused (errno=111) [ALERT] 060/220545 (3797) : sendmsg logger #1 failed: Connection refused (errno=111) [ALERT] 060/220545 (3797) : sendmsg logger #2 failed: Connection refused (errno=111) [ALERT] 060/220545 (3797) : sendmsg logger #1 failed: Connection refused (errno=111) [ALERT] 060/220545 (3797) : sendmsg logger #2 failed: Connection refused (errno=111) [ALERT] 060/220545 (3797) : sendmsg logger #1 failed: Connection refused (errno=111) [ALERT] 060/220545 (3797) : sendmsg logger #2 failed: Connection refused (errno=111)
内容以下
[root@localhost ~]# vim /etc/sysconfig/rsyslog #SYSLOGD_OPTIONS="-c 5" SYSLOGD_OPTIONS="-c 2 -m 0 -r -x" [root@localhost ~]# #重启 /etc/init.d/rsyslog [root@localhost ~]# /etc/init.d/rsyslog restart
Haproxy1.6.4 默认没端口,能够修改rsyslog.conf 让其出现端口
#先查看 rsyslog.conf的内容 [root@localhost ~]# egrep 'UDPServerRun|ModLoad' /etc/rsyslog.conf #去除注释 [root@localhost ~]# sed -i 's/#$UDPServerRun 514/$UDPServerRun 514/g' /etc/rsyslog.conf [root@localhost ~]# sed -i 's/#$ModLoad imudp/$ModLoad imudp/g' /etc/rsyslog.conf [root@localhost ~]# sed -n -e '/$ModLoad imudp/p' -e '/$UDPServerRun/p' /etc/rsyslog.conf #重启 /etc/init.d/rsyslog [root@localhost ~]# /etc/init.d/rsyslog restart [root@localhost ~]# netstat -lntup | grep rsyslog #看看是否已经有端口出现了 [root@localhost ~]# netstat -lntup | grep rsyslog udp 0 0 0.0.0.0:514 0.0.0.0:* 5031/rsyslogd udp 0 0 :::514 :::* 5031/rsyslogd [root@localhost ~]#
#在local7.*下面新增一行 local7.* /var/log/boot.log local0.* /var/log/haproxy/haproxy.log
[root@localhost ~]# touch /var/log/haproxy/haproxy.log #重启haproxy以后就能够看到以下日志了 [root@localhost ~]# tail -1000f /var/log/haproxy/haproxy.log Mar 7 02:56:38 127.0.0.1 haproxy[5302]: stats stats/<STATS> 0/0/0/0/63 200 43794 - - LR-- 1/0/0/0/0 0/0 "GET /stats HTTP/1.1" Mar 7 02:56:42 127.0.0.1 haproxy[5302]: rabbitmq_admin rabbitmq_admin/rb_node3 1/1/263233 283694 sD 0/0/0/0/0 0/0 Mar 7 02:56:47 127.0.0.1 haproxy[5301]: rabbitmq_admin rabbitmq_admin/rb_node1 1/0/8059 0 sD 3/2/2/0/0 0/0 Mar 7 02:56:48 127.0.0.1 haproxy[5302]: stats stats/<STATS> 2/0/0/0/58 200 43841 - - LR-- 1/0/0/0/0 0/0 "GET /stats HTTP/1.1" Mar 7 02:56:52 127.0.0.1 haproxy[5301]: rabbitmq_admin rabbitmq_admin/rb_node3 1/1/8053 0 sD 3/2/2/0/0 0/0
1.一、干掉主Server,再向HAProxy发送消息,2个mq从节点能够正常收到和消费消息
1.二、干掉1个mq从节点,再向HAProxy发送消息,剩下主和从节点能够正常收到和消费消息
1.三、干掉2个mq从节点,再向HAProxy发送消息,剩下的主节点能够正常收到和消费消息
以上3个场景经测试均可以实现高可用,过程就不写在里面了。
到此,RabbitMQ+Haproxy部署高可用集群就能够了。。关于优化那些,根据大家本身的业务去作。