Skip to content

Instantly share code, notes, and snippets.

@ilaydadastan
Created April 25, 2024 15:20
Show Gist options
  • Save ilaydadastan/94e6da5118493a03236ce59e22fa8061 to your computer and use it in GitHub Desktop.
Save ilaydadastan/94e6da5118493a03236ce59e22fa8061 to your computer and use it in GitHub Desktop.
This file can be used to create RabbitMQ server using docker on AWS EC2.
#!/bin/bash
# OS: AmazonLinux
# Purpose: Rabbitmq Cluster Setup on AWS EC2 in docker
# Maintainer: cloudgeeks.ca
# https://www.rabbitmq.com/cluster-formation.html#peer-discovery-classic-config
# https://www.rabbitmq.com/cluster-formation.html#peer-discovery-aws
# https://hub.docker.com/_/rabbitmq
# https://github.com/docker-library/rabbitmq/issues/61
# https://www.rabbitmq.com/clustering.html
# Note: Make sure to TAG the EC2 Auto-Scaling Gourp EC2 with ---> service rabbitmq <--- cluster_formation.aws.instance_tags.service = rabbitmq
# Docker Installation
yum install -y docker
systemctl start docker
systemctl enable docker
# NETCAT installation
yum install -y nc
# USERADD
useradd rabbitmq
mkdir -p /root/rabbitmq
#CREDENTIALS
RABBIT_USERNAME="test"
RABBIT_PASSWORD="test"
# variables section
TOKEN=`curl -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600"`
REGION=$(curl -s http://169.254.169.254/latest/dynamic/instance-identity/document -H "X-aws-ec2-metadata-token: $TOKEN" | grep -oP '\"region\"[[:space:]]*:[[:space:]]*\"\K[^\"]+')
export AWS_DEFAULT_REGION=$REGION
IPV4=$(curl http://169.254.169.254/latest/meta-data/local-ipv4 -H "X-aws-ec2-metadata-token: $TOKEN")
# Rabbitmq Configurations
echo "
cluster_formation.peer_discovery_backend = aws
cluster_formation.aws.region = "$REGION"
cluster_formation.aws.use_autoscaling_group = true
cluster_formation.discovery_retry_limit = 10
cluster_formation.discovery_retry_interval = 10000
cluster_formation.aws.instance_tags.service = rabbitmq
cluster_formation.aws.use_private_ip = false
cluster_name = ilayda_rabbitmq
log.file.level = debug
vm_memory_high_watermark.relative = 0.8
" > /root/rabbitmq/rabbitmq.conf
echo "
NODENAME=rabbit@"$HOSTNAME"
NODE_IP_ADDRESS="$IPV4"
USE_LONGNAME=true
" > /root/rabbitmq/rabbitmq-env.conf
cat > /root/rabbitmq/enabled_plugins <<'EOF'
[rabbitmq_management,rabbitmq_peer_discovery_aws,rabbitmq_federation,rabbitmq_prometheus].
EOF
chown -R rabbitmq:rabbitmq rabbitmq
chmod 777 -R /root/rabbitmq
docker run --restart unless-stopped \
--hostname $HOSTNAME \
--volume /root/rabbitmq:/etc/rabbitmq \
--name rabbit \
--publish 15672:15672 \
--publish 5672:5672 \
--network="host" \
-e RABBITMQ_NODENAME=rabbit@"$HOSTNAME" \
-e NODE_IP_ADDRESS="$IPV4" \
-e RABBITMQ_USE_LONGNAME=true \
-e RABBITMQ_DEFAULT_USER=${RABBIT_USERNAME} \
-e RABBITMQ_DEFAULT_PASS=${RABBIT_PASSWORD} \
--log-opt max-size=1m \
--log-opt max-file=1 \
rabbitmq:3-management
while ! nc -vz 127.0.0.1 5672;do echo "Waiting for port" && sleep 5;done
sleep 30
sleep 30
# https://www.rabbitmq.com/vhosts.html
docker exec -it rabbit bash <<'EOF'
rabbitmqctl set_policy -p ilayda_rabbitmq ha-fed ".*" '{"federation-upstream-set":"all", "ha-sync-mode":"automatic","ha-mode":"all"}' --priority 1 --apply-to queues
EOF
#END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment