Centos 6.9 Install HAProxy+rabbitmq 3.6.7

192.168.31.252+192.168.31.253-Haproxy  单独两台部署(安装)

https://blog.csdn.net/hanzheng260561728/article/details/80611190

192.168.31.254虚拟IP-keepalived(部署安装192.168.31.252+192.168.31.253上)

https://blog.csdn.net/hanzheng260561728/article/details/80606702

192.168.31.2 192.168.31.3 192.168.31.4安装rabbitmq

192.168.31.2 192.168.31.3 192.168.31.4 修改主机名称
临时修改主机名
hostname node1

修改配置文件
vi /etc/hostname
node1

设置centos的主机名
vi /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=node1.com

vi /etc/hosts
192.168.31.2 node1
192.168.31.3 node2
192.168.31.3 node3

依赖包安装
yum install make gcc gcc-c++ kernel-devel m4 ncurses-devel openssl-devel unixODBC-devel python-simplejson xz perl -y

下载otp插件
wget http://erlang.org/download/otp_src_18.1.tar.gz

下载rabbitmq安装包
wget http://www.rabbitmq.com/releases/rabbitmq-server/v3.6.7/rabbitmq-server-generic-unix-3.6.7.tar.xz

tar -zxvf otp_src_18.1.tar.gz
cd otp_src_18.1
./configure --prefix=/usr/local/erlang --with-ssl -enable-threads -enable-smmp-support -enable-kernel-poll --enable-hipe --without-javac
make && make install


解压otp
vi /etc/profile
export PATH=$PATH:/usr/local/erlang/bin
source /etc/profile

解压rabbitmq
tar -xvf rabbitmq-server-generic-unix-3.6.7.tar.xz

重命名
mv rabbitmq_server-3.6.7 /usr/local/rabbitmq

添加环境变量
vi /etc/profile
#set erlang environment
export PATH=$PATH:/usr/local/erlang/bin

#set rabbitmq environment
export PATH=$PATH:/usr/local/rabbitmq/sbin

刷新环境变量
source /etc/profile

配置开机启动脚本
vi /etc/init.d/rabbitmq

#!/bin/sh
#
# rabbitmq-server RabbitMQ broker
#
# chkconfig: - 80 05
# description: Enable AMQP service provided by RabbitMQ
#
### BEGIN INIT INFO
# Provides:          rabbitmq-server
# Required-Start:    $remote_fs $network
# Required-Stop:     $remote_fs $network
# Description:       RabbitMQ broker
# Short-Description: Enable AMQP service provided by RabbitMQ broker
### END INIT INFO
# Source function library.
. /etc/rc.d/init.d/functions

export HOME=/root
PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/erlang/bin
NAME=rabbitmq-server
DAEMON=/usr/local/rabbitmq/sbin/${NAME}
CONTROL=/usr/local/rabbitmq/sbin/rabbitmqctl
DESC=rabbitmq-server
USER=root
INIT_LOG_DIR=/var/log/rabbitmq
PID_FILE=/var/run/rabbitmq/pid

START_PROG="daemon"
LOCK_FILE=/var/lock/subsys/$NAME

test -x $DAEMON || exit 0
test -x $CONTROL || exit 0

RETVAL=0
set -e

[ -f /etc/default/${NAME} ] && . /etc/default/${NAME}

ensure_pid_dir () {
    PID_DIR=`dirname ${PID_FILE}`
    if [ ! -d ${PID_DIR} ] ; then
        mkdir -p ${PID_DIR}
        chown -R ${USER}:${USER} ${PID_DIR}
        chmod 755 ${PID_DIR}
    fi
}

remove_pid () {
    rm -f ${PID_FILE}
    rmdir `dirname ${PID_FILE}` || :
}

start_rabbitmq () {
    status_rabbitmq quiet
    if [ $RETVAL = 0 ] ; then
        echo RabbitMQ is currently running
    else
        RETVAL=0
        ensure_pid_dir
        set +e
        RABBITMQ_PID_FILE=$PID_FILE $START_PROG $DAEMON \
            > "${INIT_LOG_DIR}/startup_log" \
            2> "${INIT_LOG_DIR}/startup_err" \
            0<&- &
        $CONTROL wait $PID_FILE >/dev/null 2>&1
        RETVAL=$?
        set -e
        case "$RETVAL" in
            0)
                echo SUCCESS
                if [ -n "$LOCK_FILE" ] ; then
                    touch $LOCK_FILE
                fi
                ;;
            *)
                remove_pid
                echo FAILED - check ${INIT_LOG_DIR}/startup_\{log, _err\}
                RETVAL=1
                ;;
        esac
    fi
}

stop_rabbitmq () {
    status_rabbitmq quiet
    if [ $RETVAL = 0 ] ; then
        set +e
        $CONTROL stop ${PID_FILE} > ${INIT_LOG_DIR}/shutdown_log 2> ${INIT_LOG_DIR}/shutdown_err
        RETVAL=$?
        set -e
        if [ $RETVAL = 0 ] ; then
            remove_pid
            if [ -n "$LOCK_FILE" ] ; then
                rm -f $LOCK_FILE
            fi
        else
            echo FAILED - check ${INIT_LOG_DIR}/shutdown_log, _err
        fi
    else
        echo RabbitMQ is not running
        RETVAL=0
    fi
}

status_rabbitmq() {
    set +e
    if [ "$1" != "quiet" ] ; then
        $CONTROL status 2>&1
    else
        $CONTROL status > /dev/null 2>&1
    fi
    if [ $? != 0 ] ; then
        RETVAL=3
    fi
    set -e
}

rotate_logs_rabbitmq() {
    set +e
    $CONTROL rotate_logs ${ROTATE_SUFFIX}
    if [ $? != 0 ] ; then
        RETVAL=1
    fi
    set -e
}

restart_running_rabbitmq () {
    status_rabbitmq quiet
    if [ $RETVAL = 0 ] ; then
        restart_rabbitmq
    else
        echo RabbitMQ is not runnning
        RETVAL=0
    fi
}

restart_rabbitmq() {
    stop_rabbitmq
    start_rabbitmq
}

if  [ ! -d $INIT_LOG_DIR ]; then   
    mkdir $INIT_LOG_DIR
fi

case "$1" in
    start)
        echo -n "Starting $DESC: "
        start_rabbitmq
        echo "$NAME."
        ;;
    stop)
        echo -n "Stopping $DESC: "
        stop_rabbitmq
        echo "$NAME."
        ;;
    status)
        status_rabbitmq
        ;;
    rotate-logs)
        echo -n "Rotating log files for $DESC: "
        rotate_logs_rabbitmq
        ;;
    force-reload|reload|restart)
        echo -n "Restarting $DESC: "
        restart_rabbitmq
        echo "$NAME."
        ;;
    try-restart)
        echo -n "Restarting $DESC: "
        restart_running_rabbitmq
        echo "$NAME."
        ;;
    *)
        echo "Usage: $0 {start|stop|status|rotate-logs|restart|condrestart|try-restart|reload|force-reload}" >&2
        RETVAL=1
        ;;
esac

exit $RETVAL

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

赋予权限
chmod -R 777 /etc/rc.d/init.d/rabbitmq

添加开机启动服务
chkconfig --add rabbitmq
chkconfig rabbitmq on  
 
启动rabbitmq服务
service rabbitmq start

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
开启rabbitmq web监控
rabbitmq-plugins enable rabbitmq_management

查看集群的状态
rabbitmqctl status

创建账号
rabbitmqctl add_user admin admin123

赋予其administrator
rabbitmqctl set_user_tags  admin administrator

创建和赋角色完成后查看并确认
rabbitmqctl list_users

访问任意管理端界面查看http://ip:15672 可查看


192.168.31.2 192.168.31.3 192.168.31.4 配置相同的cookie,如果不相同的话,无法搭建集群.
echo PSNXAHYXQRPBKUVNYQCU > /root/.erlang.cookie

在192.168.31.3和192.168.31.4执行命令,加入到集群,其中--ram代表是内存节点,如果希望是磁盘节点则不用加--ram,在rabbitmq集群中,至少需要一个磁盘节点
rabbitmqctl stop_app
rabbitmqctl join_cluster --ram rabbit@node1
rabbitmqctl start_app

查看集群的状态
rabbitmqctl cluster_status

设置成镜像队列 //意思表示以ha.开头的queue都会复制到各个节点 ["^"匹配所有]
rabbitmqctl set_policy ha-all "^ha\." '{"ha-mode":"all"}'

192.168.31.252+192.168.31.253-Haproxy配置
vi  /usr/local/haproxy/conf/haproxy.cfg
listen rabbitmq_local_cluster 0.0.0.0:5672
#配置TCP模式
mode tcp
option tcplog
#简单的轮询
balance roundrobin
#rabbitmq集群节点配置
server node1 192.168.31.2:5672 check inter 5000 rise 2 fall 2
server node2 192.168.31.3:5672 check inter 5000 rise 2 fall 2
server node3 192.168.31.4:5672 check inter 5000 rise 2 fall 2

#配置haproxy web监控,查看统计信息
listen private_monitoring :8100
mode http
option httplog
stats enable
#设置haproxy监控地址为http://localhost:8100/stats
stats uri /stats
stats refresh 30s
#添加用户名密码认证
stats auth admin:1234

启动haproxy
service haproxy  start

你可能感兴趣的:(Centos,RabbitMQ,haproxy)