RabbitMQ + Haproxy 高可用集羣部署實戰方案

Linux環境:Centos6.8 64-bit

RabbitMQ版本:v3.6.6

RabbitMQ機器:192.168.2.24二、192.168.2.24三、192.168.2.244

Haproxy版本:v1.6.4

Haproxy機器:192.168.2.241

 

1、RabbitMQ的安裝

一、安裝erlang

[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

二、安裝rabbitmq

[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%]

三、啓動rabbitmq

[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

四、啓用web管理界面

[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

八、一些問題

    8.一、執行rabbitmqctc list_users出現以下錯誤

    Error: unable to connect to node rabbit@localhost: nodedownlinux

    解決:服務沒啓動成功,具體的看看日誌輸出什麼問題,再解決web

    8.二、執行 rabbitmq-server start 的時候出現以下錯誤
/usr/lib/rabbitmq/bin/rabbitmq-env: line 52: cd: /var/lib/rabbitmq/mnesia/rabbit@localhost-plugins-expand: Permission denied
BOOT FAILED
===========

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/服務器

    8.三、新建用戶以後,登陸的時候出現以下

    =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

 

2、集羣的建立

一、建立以前的準備工做

一、修改/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。

3、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

五、新增haproxy.cfg配置文件

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

六、啓動Haproxy並登陸監控臺

[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加入負載了

七、RabbitMQ監控界面,查看各節點運行狀況

由於咱們在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)

4、爲HAProxy增長日誌文件

1、修改/etc/sysconfig/rsyslog

內容以下

[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

2、爲Haproxy1.6.4 新增默認端口

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 ~]#

3、修改/etc/rsyslog.conf

#在local7.*下面新增一行
local7.*                                                /var/log/boot.log
local0.*                                                /var/log/haproxy/haproxy.log

4、新建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

5、集羣測試

一、三種測試場景

 1.一、幹掉主Server,再向HAProxy發送消息,2個mq從節點能夠正常收到和消費消息

 1.二、幹掉1個mq從節點,再向HAProxy發送消息,剩下主和從節點能夠正常收到和消費消息

 1.三、幹掉2個mq從節點,再向HAProxy發送消息,剩下的主節點能夠正常收到和消費消息

以上3個場景經測試均可以實現高可用,過程就不寫在裏面了。

到此,RabbitMQ+Haproxy部署高可用集羣就能夠了。。關於優化那些,根據大家本身的業務去作。

相關文章
相關標籤/搜索