Skip to content

Instantly share code, notes, and snippets.

@ae6rt
Last active May 24, 2016 12:12
Show Gist options
  • Save ae6rt/c999dd686a27e204e403d46c7ba9191f to your computer and use it in GitHub Desktop.
Save ae6rt/c999dd686a27e204e403d46c7ba9191f to your computer and use it in GitHub Desktop.
Clustering RabbitMQ in a pair of Docker containers

Command basics

NET=${NET:-rabbit-net}

# Create a network
docker network create -d bridge --subnet 172.25.0.0/16 $NET 

# Start containers 
docker rm -f rabbit1 rabbit2 >/dev/null 2>&1

# Start rabbit1
docker run -d -p 5672:5672 -p 5671:5671 -p 15672:15672 -e HOSTNAME=rabbit1 --name=rabbit1 --net=$NET docker.example.com/rabbitmq:3.6.2

# Start rabbit2, which clusters with rabbit1
docker run -d -p 6672:5672 -p 6671:5671 -p 16672:15672 -e HOSTNAME=rabbit2 --name=rabbit2 --net=$NET -e CLUSTER_WITH=rabbit1 docker.example.com/rabbitmq:3.6.2

Discussion

The container images are identical, but rabbit2 is configured via the environment to cluster with rabbit1. Here is the entrypoint for both containers:

rabbitmq-server -detached
sleep 5

rabbitmq-plugins enable rabbitmq_management 
rabbitmqctl add_user remote sekrit 
rabbitmqctl set_user_tags remote administrator 
rabbitmqctl set_permissions -p / remote ".*" ".*" ".*" 

CLUSTER_WITH=${CLUSTER_WITH:-nobody}
if [ $CLUSTER_WITH != "nobody" ]; then
   until nmap -p5672 $CLUSTER_WITH | grep ^5672.*open 
   do
      echo "Waiting for $CLUSTER_WITH listener"
   done
   rabbitmqctl stop_app
   sleep 3
   rabbitmqctl join_cluster rabbit@${CLUSTER_WITH}
   sleep 3
   rabbitmqctl start_app 
fi

set +x
while true ; do sleep 5; done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment