1 實驗環(huán)境
RabbitMQ 集群
server1.example.com IP: 10.10.10.11 Node: disk
server2.example.com IP: 10.10.10.12 Node: disk
server3.example.com IP: 10.10.10.13 Node: disk
RabbitMQ相關(guān)端口
4369 (epmd), 25672 (Erlang distribution)
5672, 5671 (AMQP 0-9-1 without and with TLS)
15672 (if management plugin is enabled)
61613, 61614 (if STOMP is enabled)
1883, 8883 (if MQTT is enabled)
2 YUM方式安裝
2.1 添加EPEL源
~># rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
2.2 添加Erlang源
~># yum install wget
~># rpm -Uvh http://packages.erlang-solutions.com/erlang-solutions-1.0-1.noarch.rpm
2.3 安裝RabbitMQ
~># wget http://www.rabbitmq.com/releases/rabbitmq-server/v3.6.1/rabbitmq-server-3.6.1-1.noarch.rpm
~># rpm --import https://www.rabbitmq.com/rabbitmq-signing-key-public.asc
~># yum install rabbitmq-server-3.6.1-1.noarch.rpm
3 手動RPM方式
3.1 安裝RabbitMQ
~># wget http://www.rabbitmq.com/releases/erlang/erlang-18.3-1.el7.centos.x86_64.rpm
~># wget http://www.rabbitmq.com/releases/rabbitmq-server/v3.6.1/rabbitmq-server-3.6.1-1.noarch.rpm
~># wget https://www.rabbitmq.com/rabbitmq-signing-key-public.asc
~># rpm --import rabbitmq-signing-key-public.asc
~># rpm -Uvh erlang-18.3-1.el7.centos.x86_64.rpm rabbitmq-server-3.6.1-1.noarch.rpm
注: 如果是RedHat6缕允,erlang下載地址為 http://www.rabbitmq.com/releases/erlang/erlang-18.3-1.el6.x86_64.rpm
4 RabbitMQ單機(jī)配置
4.1 調(diào)整可打開文件/文件描述符數(shù)目(高并發(fā)支持)
調(diào)整系統(tǒng)限制
~># vi /etc/sysctl.conf
fs.file-max = 100000
使設(shè)置生效
~># sysctl -p
查看系統(tǒng)限制
~># sysctl fs.file-max
調(diào)整用戶限制
~># vi /etc/security/limits.conf
* soft nofile 65536
* hard nofile 65536
重啟系統(tǒng)使之生效溅漾,檢查用戶限制是否生效
~># ulimit -n
4.2 添加/etc/hosts條目
~># echo "192.168.136.201 server1" >> /etc/hosts
4.3 開通防火墻上Web UI訪問端口(默認(rèn):15672/tcp)
~># firewall-cmd --permanent --add-port=15672/tcp
~># firewall-cmd –-reload
4.4 設(shè)置RabbitMQ服務(wù)自啟動叛拷,并啟動RabbbitMQ服務(wù)
~># chkconfig rabbitmq-server on
~># service rabbitmq-server start
4.5 啟用RabbitMQ監(jiān)控插件
~># rabbitmq-plugins enable rabbitmq_management
4.6 RabbitMQ用戶管理
添加用戶(用戶名root,密碼admin)
~># rabbitmqctl add_user admin admin
設(shè)置用戶角色(設(shè)置admin用戶為管理員角色)
~># rabbitmqctl set_user_tags admin administrator
設(shè)置用戶權(quán)限(設(shè)置admin用戶配置于未、寫、讀的權(quán)限)
~># rabbitmqctl set_permissions -p / admin "." "." ".*"
刪除用戶(刪除guest用戶)
~># rabbitmqctl delete_user guest
注意:rabbitmq從3.3.0開始禁止使用guest/guest權(quán)限通過除localhost外的訪問击困。
如果想使用guest/guest通過遠(yuǎn)程機(jī)器訪問吮蛹,需要在rabbitmq配置文件中(/etc/rabbitmq/rabbitmq.config)中設(shè)置loopback_users為[]。
[{rabbit, [{loopback_users, []}]}].
4.7 登陸Web UI
在瀏覽器中打開http://10.10.10.11:15672
輸入用戶名狭魂、密碼罚攀,登陸成功后的界面
4.8 檢查RabbitMQ狀態(tài)
查看進(jìn)程:
~># ps aux | grep -v grep | grep rabbitmq-server
查看端口:
~># netstat -tnlp | grep 5672
如果提示netstat命令沒找到,請先安裝net-tools
~># yum install net-tools
查看RabbitMQ狀態(tài):
~># rabbitmqctl status
4.9 RabbitMQ默認(rèn)配置
~># cat /usr/lib/rabbitmq/lib/rabbitmq_server-3.6.1/sbin/rabbitmq-defaults
### next line potentially updated in package install steps
SYS_PREFIX=
### next line will be updated when generating a standalone release
ERL_DIR=
CLEAN_BOOT_FILE=start_clean
SASL_BOOT_FILE=start_sasl
## Set default values
BOOT_MODULE="rabbit"
CONFIG_FILE=${SYS_PREFIX}/etc/rabbitmq/rabbitmq
LOG_BASE=${SYS_PREFIX}/var/log/rabbitmq
MNESIA_BASE=${SYS_PREFIX}/var/lib/rabbitmq/mnesia
ENABLED_PLUGINS_FILE=${SYS_PREFIX}/etc/rabbitmq/enabled_plugins
PLUGINS_DIR="${RABBITMQ_HOME}/plugins"
IO_THREAD_POOL_SIZE=64
CONF_ENV_FILE=${SYS_PREFIX}/etc/rabbitmq/rabbitmq-env.conf
從上面看出:
- 系統(tǒng)prefix是空
- 配置文件路徑是 /etc/rabbitmq/rabbitmq.config (erlang會自動加上.config后綴)
- 環(huán)境配置文件是 /etc/rabbitmq/rabbitmq-env.conf
- 日志文件目錄是 /var/log/rabbitmq
- 插件文件目錄是 安裝目錄下的plugins雌澄,這里RPM安裝方式下是 /usr/lib/rabbitmq/lib/rabbitmq_server-3.6.1/plugins
5 RabbitMQ集群配置
集群配置在單機(jī)配置完成的基礎(chǔ)上進(jìn)行
5.1 添加/etc/hosts條目
在server1斋泄、server2、server3里的/etc/hosts文件中分別添加:
10.10.10.11 server1
10.10.10.12 server2
10.10.10.13 server3
5.2 設(shè)置每個節(jié)點Cookie
Rabbitmq的集群是依賴于erlang的集群來工作的镐牺,所以必須先構(gòu)建起erlang的集群環(huán)境是己。Erlang的集群中各節(jié)點是通過一個magic cookie來實現(xiàn)的,這個cookie存放在 /var/lib/rabbitmq/.erlang.cookie 中任柜,文件是400的權(quán)限。所以必須保證各節(jié)點cookie保持一致粥谬,否則節(jié)點之間就無法通信
~># chmod 700 /var/lib/rabbitmq/.erlang.cookie
~># echo -n "AZVOCZYZZBVFLBPTBXU" > /var/lib/rabbitmq/.erlang.cookie
~># chmod 400 /var/lib/rabbitmq/.erlang.cookie
建議在RabbitMQ服務(wù)啟動前修改過cookie痴突,如果RabbitMQ服務(wù)已經(jīng)啟動,修改cookie值后逆皮,必須重啟RabbitMQ服務(wù),這步很關(guān)鍵
~># ps -ef | grep ^rabbitmq | awk '{print $2}' | xargs kill -9
~># service rabbitmq-server start
5.3 開通防火墻上集群通信端口
~># firewall-cmd --permanent --add-port={4369/tcp,25672/tcp}
~># firewall-cmd --reload
5.4 加入集群
將 server1参袱、server2 电谣、server3組成集群:
默認(rèn)是磁盤節(jié)點,如果是內(nèi)存節(jié)點的話抹蚀,需要加--ram參數(shù)
在server2剿牺、server3上分別運行:
~># rabbitmqctl stop_app
~># rabbitmqctl join_cluster rabbit@server1
~># rabbitmqctl start_app
5.5 設(shè)置鏡像策略
~># rabbitmqctl set_policy ha-all "^" '{"ha-mode":"all","ha-sync-mode":"automatic"}'
相關(guān)知識
rabbitmqctl (rabbitmq管理工具)
rabbitmq-plugins (rabbitmq插件管理工具)
rabbitmq-server (rabbitmq服務(wù))
主要配置文件
- enabled_plugins:設(shè)置允許的插件列表。
- rabbitmq.config:設(shè)置rabbitmq運行參數(shù)环壤,結(jié)構(gòu)為hash數(shù)組格式晒来。
- rabbitmq-env.conf:rabbitmq環(huán)境參數(shù)配置
rabbitmq-env.conf中的每項都以RABBITMQ_為前綴,常用參數(shù)如下:
RABBITMQ_NODENAME=FZTEC-240088 //節(jié)點名稱
RABBITMQ_NODE_IP_ADDRESS=127.0.0.1 //IP地址郑现,空串bind所有地址湃崩,指定地址bind指定網(wǎng)絡(luò)接口
RABBITMQ_NODE_PORT=5672 //TCP端口號,默認(rèn)是5672
RABBITMQ_LOG_BASE=/data/rabbitmq/log //日志所在路徑
RABBITMQ_PLUGINS_DIR=/data/rabbitmq/plugins //插件所在路徑
RABBITMQ_MNESIA_BASE=/data/rabbitmq/mnesia //mnesia所在路徑
rabbitmq-env.conf和rabbitmq.config默認(rèn)是不存在的接箫。
rabbitmq-env.conf需要在缺省位置手動創(chuàng)建一個攒读。
rabbitmq.config需要在RABBITMQ_CONFIG_FILE指定位置手動創(chuàng)建一個。
6 腳本安裝RabbitMQ的腳本
創(chuàng)建腳本文件
~># mkdir rabbitmq-script && cd rabbitmq-script
~># vi rabbitmq.config
[
{rabbit,
[
{cluster_partition_handling, pause_minority}
]
}
].
~># vi settings.conf
ERLANG_EL6_RPM=erlang-19.0.4-1.el6.x86_64.rpm
ERLANG_EL6_RPM_URL=http://www.rabbitmq.com/releases/erlang/erlang-19.0.4-1.el6.x86_64.rpm
ERLANG_EL7_RPM=erlang-19.0.4-1.el7.centos.x86_64.rpm
ERLANG_EL7_RPM_URL=http://www.rabbitmq.com/releases/erlang/erlang-19.0.4-1.el7.centos.x86_64.rpm
RABBITMQ_RPM=rabbitmq-server-3.6.9-1.el7.noarch.rpm
RABBITMQ_RPM_URL=http://www.rabbitmq.com/releases/rabbitmq-server/v3.6.9/rabbitmq-server-3.6.9-1.el7.noarch.rpm
RABBITMQ_KEY=rabbitmq-signing-key-public.asc
RABBITMQ_KEY_URL=https://www.rabbitmq.com/rabbitmq-signing-key-public.asc
RABBITMQ_CONFIG_FILE=rabbitmq.config
ERLANG_COOKIE=AZVOCZYZZBVFLBPTBXUD
RABBITMQ_HTTP_PORT=15672
ERL_EPMD_PORT=4369
RABBITMQ_DIST_PORT=25672
ADMIN_PWD=admin
~># vi install_rabbitmq.sh
#!/bin/bash
#====================================================================
# install_rabbitmq.sh
#
# Linux RabbitMQ Auto Install Script
#
# Copyright (c) 2015, Edward Guan <edward.guan@mkcorp.com>
# All rights reserved.
# Distributed under the GNU General Public License, version 3.0.
#
# Intro: http://www.cnblogs.com/edward2013/p/5061511.html
#
#====================================================================
echo "Note: This tiny script has been hardcoded specifically for RHEL/CentOS"
echo ""
if [ $(id -u) != "0" ]; then
echo "Error: You must be root to run this script!"
exit 1
fi
# defind functions
msg() {
printf '%b\n' "$1" >&2
}
success() {
msg "\33[32m[?]\33[0m ${1}${2}"
}
error() {
msg "\33[31m[?]\33[0m ${1}${2}"
exit 1
}
program_exists() {
command -v $1 >/dev/null 2>&1
}
function open_rabbitmq_ports() {
if program_exists firewall-cmd; then
firewall-cmd -q --permanent \
--add-port={$RABBITMQ_HTTP_PORT/tcp,$ERL_EPMD_PORT/tcp,$RABBITMQ_DIST_PORT/tcp}
firewall-cmd -q --reload
firewall-cmd -q --query-port $RABBITMQ_HTTP_PORT/tcp || return 1
firewall-cmd -q --query-port $ERL_EPMD_PORT/tcp || return 1
firewall-cmd -q --query-port $RABBITMQ_DIST_PORT/tcp || return 1
fi
}
function erase_rabbitmq() {
rpm -e rabbitmq-server
}
function update_rabbitmq_package() {
local TMP_FILE=/tmp/.$(basename "$0").tmp
# install required package
program_exists wget || yum -y install wget >/dev/null 2>&1
program_exists socat || yum -y install socat >/dev/null 2>&1
# install erlang rpm
if [ ! -f "$ERLANG_RPM_PATH" ]; then
msg "WARNING: $ERLANG_RPM_PATH not found."
msg "Try to download and install from $ERLANG_RPM_URL..."
wget $ERLANG_RPM_URL -P $SCRIPT_PATH || return 1
fi
rpm -U $ERLANG_RPM_PATH 2>$TMP_FILE && success "Installed $ERLANG_RPM_PATH" || {
sed 's/^[ |\t]*//' $TMP_FILE | grep "already installed" || return 1
}
# import rabbitmq key
if [ ! -f "$RABBITMQ_KEY_PATH" ]; then
msg "WARNING: $RABBITMQ_KEY_PATH not found."
msg "Try to download and import from $RABBITMQ_KEY_URL..."
wget $RABBITMQ_KEY_URL -P $SCRIPT_PATH || return 1
fi
rpm --import $RABBITMQ_KEY_PATH && success "Imported $RABBITMQ_KEY_PATH"
# install rabbitmq rpm
if [ ! -f "$RABBITMQ_RPM_PATH" ]; then
msg "WARNING: $RABBITMQ_RPM_PATH not found."
msg "Try to download and install from $RABBITMQ_RPM_URL..."
wget $RABBITMQ_RPM_URL -P $SCRIPT_PATH || return 1
fi
rpm -U $RABBITMQ_RPM_PATH 2>$TMP_FILE && success "Installed $RABBITMQ_RPM_PATH" || {
sed 's/^[ |\t]*//' $TMP_FILE | grep "already installed" || return 1
}
}
function set_rabbitmq_users() {
if program_exists rabbitmqctl; then
rabbitmqctl list_users | grep -q guest && \
rabbitmqctl delete_user guest >/dev/null && \
success "Deleted user [guest]"
rabbitmqctl list_users | grep -q admin || {
rabbitmqctl add_user admin $ADMIN_PWD >/dev/null && \
rabbitmqctl set_user_tags admin administrator >/dev/null && \
rabbitmqctl set_permissions -p / admin ".*" ".*" ".*" >/dev/null && \
success "Added user [admin]"
}
else
msg "Command not find: rabbitmqctl" && return 1
fi
}
function set_rabbitmq_policies() {
if program_exists rabbitmqctl; then
rabbitmqctl list_policies | grep -q ha-all || {
rabbitmqctl set_policy ha-all "^" '{"ha-mode":"all","ha-sync-mode":"automatic"}' >/dev/null && \
success "Added policy [ha-all]"
}
else
msg "Command not find: rabbitmqctl" && return 1
fi
}
function install_rabbitmq() {
rpm -q rabbitmq-server >/dev/null && \
error "$(rpm -q rabbitmq-server) is already installed"
update_rabbitmq_package || return 1
# enable rabbitmq plugin
rabbitmq-plugins enable rabbitmq_management >/dev/null && \
success "Enabled rabbitmq plugin [rabbitmq_management]" || return 1
# open firewall ports
# open_rabbitmq_ports && \
# success "Opened rabbitmq ports [$RABBITMQ_HTTP_PORT/tcp,$ERL_EPMD_PORT/tcp,$RABBITMQ_DIST_PORT/tcp]" || \
# return 1
# deploy rabbitmq.config
RABBITMQ_CONFIG_PATH="$SCRIPT_PATH/$RABBITMQ_CONFIG_FILE"
RABBITMQ_CONFIG_LOCATION=/etc/rabbitmq/rabbitmq.config
cp $RABBITMQ_CONFIG_PATH $RABBITMQ_CONFIG_LOCATION && \
success "Copyed $RABBITMQ_CONFIG_PATH to $RABBITMQ_CONFIG_LOCATION" || return 1
# add host entry into /etc/hosts
HOST_ENTRY=$(egrep "127.0.0.1 +$(hostname -s)" /etc/hosts)
if [ "x$HOST_ENTRY" == "x" ]; then
echo "127.0.0.1 $(hostname -s)" >> /etc/hosts && \
success "Added [127.0.0.1 $(hostname -s)] into /etc/hosts" || return 1
else
msg "[$HOST_ENTRY] is already added into /etc/hosts"
fi
# set erlang cookie
[ "x$COOKIE" == "x" ] && COOKIE="$ERLANG_COOKIE"
ERLANG_COOKIE_PATH=/var/lib/rabbitmq/.erlang.cookie
if [ ! -f "$ERLANG_COOKIE_PATH" ]; then
msg "WARNING: $ERLANG_COOKIE_PATH is not exist, create new one"
touch $ERLANG_COOKIE_PATH || return 1
fi
chmod 700 $ERLANG_COOKIE_PATH
echo -n "$COOKIE" >$ERLANG_COOKIE_PATH
chmod 400 $ERLANG_COOKIE_PATH
chown rabbitmq:rabbitmq $ERLANG_COOKIE_PATH
if [ $(cat $ERLANG_COOKIE_PATH) = "$COOKIE" ]; then
success "Set erlang cookie value to $COOKIE"
fi
}
function join_rabbitmq_cluster() {
local ret=0
if [ "x$SERVER" != "x" ] && [ "$SERVER" != $(hostname -s) ]; then
if program_exists rabbitmqctl; then
rabbitmqctl stop_app >/dev/null
rabbitmqctl reset >/dev/null
if [ "$NODE" == "ram" ]; then
rabbitmqctl join_cluster --ram rabbit@$SERVER >/dev/null || let ret++
else
rabbitmqctl join_cluster rabbit@$SERVER >/dev/null || let ret++
fi
rabbitmqctl start_app >/dev/null
else
msg "Command not find: rabbitmqctl" && return 1
fi
if [ "$ret" -eq 0 ]; then
success "Joined rabbit@$SERVER"
return 0
else
return 1
fi
fi
}
function restart_rabbitmq() {
# kill all rabbitmq server processes
local RABBITMQ_PIDS=$(ps -ef | grep ^rabbitmq | cut -c 9-16 | tr -s "\n" " ")
if [ "x$RABBITMQ_PIDS" != "x" ]; then
kill -9 $RABBITMQ_PIDS && success "Killed all rabbitmq server processes"
fi
# enable and start rabbitmq server
chkconfig rabbitmq-server on
service rabbitmq-server start
}
function print_usage() {
echo "Usage: $(basename "$0") [OPTIONS...]"
echo ""
echo "Options"
echo " [-h|--help] Prints a short help text and exists"
echo " [-i|--install] Install rabbitmq server"
echo " [-u|--update] Update rabbitmq server"
echo " [-e|--erase] Erase (uninstall) rabbitmq server"
echo " [-c|--cookie] <cookie> Set erlang cookie"
echo " [-j|--join] <server> Join rabbitmq cluster"
echo " [-n|--node] <disc|ram> Set cluster node type"
}
# read the options
TEMP=`getopt -o hiuec:j:n: --long help,install,update,erase,cookie:,join:,node: -n $(basename "$0") -- "$@"`
eval set -- "$TEMP"
# extract options and their arguments into variables.
while true; do
case "$1" in
-h|--help) print_usage ; exit 0 ;;
-i|--install) ACTION=install ; shift ;;
-u|--update) ACTION=update ; shift ;;
-e|--erase) ACTION=erase ; shift ;;
-c|--cookie) COOKIE=$2 ; shift 2 ;;
-j|--join) SERVER=$2 ; shift 2 ;;
-n|--node) NODE=$2 ; shift 2 ;;
--) shift ; break ;;
*) error "Internal error!" ;;
esac
done
# get script path
SCRIPT_PATH="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# load settings
source "$SCRIPT_PATH/settings.conf" || exit 1
# set erlang and rabbitmq rpm & key path
case $(uname -r) in
*el7*)
ERLANG_RPM_PATH="$SCRIPT_PATH/$ERLANG_EL7_RPM";
ERLANG_RPM_URL="$ERLANG_EL7_RPM_URL" ;;
*el6*)
ERLANG_RPM_PATH="$SCRIPT_PATH/$ERLANG_EL6_RPM";
ERLANG_RPM_URL="$ERLANG_EL6_RPM_URL" ;;
*) error "Your system is not RHEL/CentOS" ;;
esac
RABBITMQ_RPM_PATH="$SCRIPT_PATH/$RABBITMQ_RPM"
RABBITMQ_KEY_PATH="$SCRIPT_PATH/$RABBITMQ_KEY"
if [ "x$ACTION" == "x" ] && [ "x$SERVER" == "x" ]; then
error "$(basename "$0"): missing operand\n" \
"Try '$(basename "$0") --help' for more information."
fi
echo "$NODE" | grep -qE "^disk$|^ram$|^$" || {
error "$(basename "$0"): -n: must be disk or ram"
}
# if ACTION is erase, erase rabbitmq server
if [ "$ACTION" == "erase" ]; then
erase_rabbitmq && {
msg "\nThanks for erasing rabbitmq-server."
msg "? `date +%Y` marykay company"
} || error "Failed erase rabbitmq server"
exit 0
fi
# if ACTION is update, update rabbitmq server
if [ "$ACTION" == "update" ]; then
update_rabbitmq_package && restart_rabbitmq && {
msg "\nThanks for updating rabbitmq-server."
msg "? `date +%Y` marykay company"
} || error "Failed update rabbitmq server"
exit 0
fi
# if ACTION is install, install rabbitmq server
if [ "$ACTION" == "install" ]; then
install_rabbitmq && restart_rabbitmq && set_rabbitmq_users && set_rabbitmq_policies && join_rabbitmq_cluster && {
msg "\nThanks for installing rabbitmq-server."
msg "? `date +%Y` marykay company"
} || error "Failed install rabbitmq server"
else
if [ "x$SERVER" != "x" ]; then
join_rabbitmq_cluster && {
msg "\nThanks for joining rabbitmq-server."
msg "? `date +%Y` marykay company"
} || error "Failed join rabbitmq server"
fi
fi
在server1辛友,server2上分別運行腳本
~># chmod +x install_rabbitmq.sh
安裝RabbitMQ
~># ./install_rabbitmq.sh -i
升級RabbitMQ
~># ./install_rabbitmq.sh -u
卸載RabbitMQ
~># ./install_rabbitmq.sh -e
加入群集薄扁,不帶-n參數(shù)時,默認(rèn)節(jié)點類型為dsik废累,-n參數(shù)后只能接ram和disk
~># ./install_rabbitmq.sh -j server1 -n ram
安裝RabbitMQ并加入群集
~># ./install_rabbitmq.sh -ij server1