我是靠谱客的博主 开朗小蘑菇,最近开发中收集的这篇文章主要介绍rabbitmq+keepalived高可用,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

192.168,2,180  rabbitmq1 master
192.168.2.181  rabbitmq2 slave
192.168.2.222  VIP
两台机器都安装rabbitmq 依赖erlang
yum install erlang
wget http://www.rabbitmq.com/releases/rabbitmq-server/v3.6.2/rabbitmq-server-3.6.2-1.noarch.rpm
yum install -y rabbitmq-server-3.6.2-1.noarch.rpm
如果报错的话:rpm -i --nodeps rabbitmq-server-3.6.2-1.noarch.rpm
/etc/init.d/rabbitmq-server start
chkconfig rabbitmq-server on
启用插件rabbitmq management
rabbitmq-plugins enable rabbitmq_management
/etc/init.d/rabbitmq-server restart

配置MQ集群
cookie文件
因为RabbitMQ的集群是通过Erlang的集群来实现的,所以,要求三台机器的
/var/lib/rabbitmq/.erlang.cookie 文件内容一致,用VI等工具将它的内容修改为 zHDCGETPYWOWREASJUAB
由于RabbitMQ在启动Booker时会检查该文件的权限,必须为400,否则会报错,所以要修改文件的权限
chmod 400 .erlang.cookie
修改各机器hosts
     192.168.2.180    pzs-test-1
     192.168.2.181    pzs-test-2
 
   2.3 加入集群
     对主节点:
     #启动Broker
     rabbitmq-server -detached > nohup.out&
     #启动集群
     rabbitmqctl start_app
      #查看集群状态
     rabbitmqctl cluster_status
 
     对备节点:
     rabbitmq-server -detached > nohup.out&
     rabbitmqctl start_app
     rabbitmqctl stop_app
     #加入集群
     rabbitmqctl join_cluster --ram rabbit@pzs-test-1
     rabbitmqctl start_app
     #查看集群状态
     rabbitmqctl cluster_status
 
     在三台机器运行以下命令:
   设置成镜像队列
     rabbitmqctl set_policy ha-all "^" '{"ha-mode":"all"}' //["^"匹配所有]

rabbitmq1上keepalived设置:
vi /etc/keepalived/scripts/rabbitmq_check.sh
#!/bin/bash
k1=`/usr/bin/nmap -sS 127.0.0.1 -p 5672 | grep 5672 | awk '{printf $2}'`
if [ "$k1"x != "open"x ]; then
        /usr/sbin/rabbitmq-server -detached
fi
Q1=`/usr/bin/nmap -sS 127.0.0.1 -p 5672 | grep 5672 | awk '{printf $2}'`
if [ "$Q1"x != "open"x ]; then
        /etc/init.d/keepalived stop
fi
chmod +x /etc/keepalived/scripts/rabbitmq_check.sh

vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
   notification_email {
        }
   notification_email_from root@local
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id RABBITMQ
}
vrrp_script chk_rabbitmq {
   script "/etc/keepalived/scripts/rabbitmq_check.sh"
   interval 2
   weight 2
}
vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 70
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.2.222/24 dev eth0
    }
        track_script {
         chk_rabbitmq
        }
}

rabbitmq2上keepalived设置:
vi /etc/keepalived/scripts/rabbitmq_check.sh
#!/bin/bash
k1=`/usr/bin/nmap -sS 127.0.0.1 -p 5672 | grep 5672 | awk '{printf $2}'`
if [ "$k1"x != "open"x ]; then
        /usr/sbin/rabbitmq-server -detached
fi
Q1=`/usr/bin/nmap -sS 127.0.0.1 -p 5672 | grep 5672 | awk '{printf $2}'`
if [ "$Q1"x != "open"x ]; then
        /etc/init.d/keepalived stop
fi
chmod +x /etc/keepalived/scripts/rabbitmq_check.sh

vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
   notification_email {
        }
   notification_email_from root@local
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id RABBITMQ
}
vrrp_script chk_rabbitmq {
   script "/etc/keepalived/scripts/rabbitmq_check.sh "
   interval 2
   weight 2
}
vrrp_instance VI_1 {
    state SLAVE
    interface eth0
    virtual_router_id 70
    priority 70
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.2.222/24 dev eth0
    }
track_script {
    chk_rabbitmq
   }
}

最后

以上就是开朗小蘑菇为你收集整理的rabbitmq+keepalived高可用的全部内容,希望文章能够帮你解决rabbitmq+keepalived高可用所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(47)

评论列表共有 0 条评论

立即
投稿
返回
顶部