Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
A complete bash script to setup a RabbitMQ cluster from scratch. Takes user-defined servers at run time.
set -e
# This script is part of my blog post :
# It sets up a RabbitMQ cluster by connecting to user-provided master and slave servers
# and ringing them up to a cluster on the fly.
# RabbitMQ Clustering is described in detail here :
function getHostname()
local HOST=''
while test -z "$HOST"
read -p "$1 : " HOST
echo $HOST;
sudo rabbitmqctl stop_app;
sudo rabbitmqctl reset;
sudo rabbitmqctl start_app;
# Step 1 : Setup the Master and get the erlang cookie
echo "Setup RabbitMQ Master";
echo "=====================";
MASTER_HOSTNAME=$(getHostname "Enter the master server's hostname [hostname or user@hostname]");
echo "[$MASTER_HOSTNAME] Setting up master";
ssh -t $MASTER_HOSTNAME "bash -c '$SETUP_MASTER_SCRIPT sudo cat /var/lib/rabbitmq/.erlang.cookie;'" | tee $OUT;
COOKIE=$(cat $OUT | tail -n1)
rm $OUT;
echo "Master's Erlang Cookie : '$COOKIE'"
MASTER_IP=$(getHostname "Enter the master server's IP as seen from the slaves (Use a local IP if available)");
# Step 3 : Setup the slaves
sudo sed -i \"s/^$/$MASTER_IP $MASTER_HOSTNAME\n/\" /etc/hosts
sudo bash -c \"echo -n '$COOKIE' > /var/lib/rabbitmq/.erlang.cookie\";
sudo rabbitmqctl stop_app;
sudo rabbitmqctl reset;
sudo rabbitmqctl join_cluster --ram rabbit@$MASTER_HOSTNAME;
sudo rabbitmqctl start_app;
sudo rabbitmqctl cluster_status;
echo "Setup RabbitMQ Slaves";
echo "=====================";
S="Enter the server's hostname [hostname or user@hostname] or 'q' to quit : "
SERVER=$(getHostname $S);
while test "$SERVER" != "q"
echo "[SERVER] Setting up slave";
ssh -t $SERVER "bash -c '$SETUP_SLAVE_SCRIPT'";
SERVER=$(getHostname $S);
echo "Done";

This comment has been minimized.

Copy link

ypereirareis commented Apr 4, 2017

Hi !
Thx for sharing this 😃

I built a docker based project allowing to run a highly available RabbitMQ cluster with HAProxy.

You can find it there:

Bye !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.