Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A complete bash script to setup a RabbitMQ cluster from scratch. Takes user-defined servers at run time.
#!/bin/bash
set -e
# This script is part of my blog post :
# http://thoughtsimproved.wordpress.com/2015/01/03/tech-recipe-setup-a-rabbitmq-cluster-on-ubuntu/
# 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 :
# https://www.rabbitmq.com/clustering.html
function getHostname()
{
local HOST=''
while test -z "$HOST"
do
read -p "$1 : " HOST
done
echo $HOST;
}
SETUP_MASTER_SCRIPT='
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 "=====================";
OUT=/tmp/master.out
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
SETUP_SLAVE_SCRIPT="
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"
do
echo "[SERVER] Setting up slave";
ssh -t $SERVER "bash -c '$SETUP_SLAVE_SCRIPT'";
SERVER=$(getHostname $S);
done
echo "Done";
@ypereirareis

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: https://github.com/ypereirareis/docker-rabbitmq-ha-cluster

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.