How to Build a High‑Availability RabbitMQ Cluster on CentOS with Docker
This guide walks through analyzing RabbitMQ requirements, selecting self‑hosted servers, preparing CentOS nodes, installing Docker and Docker‑Compose, configuring RabbitMQ, deploying a three‑node high‑availability cluster, and validating its performance and memory usage.
Demand Analysis
During RabbitMQ deployment we analyzed existing connections (631) and queues (80,418). Cloud service could not meet the required queue count and cost was high, so a self‑built three‑node cluster was chosen, each node using 8 CPU, 16 GB RAM, 100 GB disk, 5 Mbps network.
Server Specification Comparison
Selected self‑built nodes (8 core/16 GB) cost ¥2,485 per month, compared with Tencent Cloud options where higher specs still fell short of queue requirements.
Cluster Construction Requirements
Future expansion must be supported; node specifications may be adjusted to 4 core/8 GB with 150 GB storage for new workloads. Memory usage threshold should be set to 70 %.
Environment Setup
All nodes run CentOS 7.9. Hostnames are set, and the init.sh script installs basic tools, disables firewalld and SELinux, increases file descriptor limits, applies kernel tuning, reduces swap usage, and installs performance tools.
Data Disk Formatting
mkfs.ext4 /dev/vdb
mount /dev/vdb /data
echo "/dev/vdb /data ext4 defaults 0 0" >> /etc/fstab
mount -a
mkdir -p /data/{apd,logs,prog,setup,backup,www}Docker and Docker‑Compose Installation
yum install -y yum-utils device-mapper-persistent-data lvm2 git htop
sudo yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
sudo sed -i 's+download.docker.com+mirrors.aliyun.com/docker-ce+' /etc/yum.repos.d/docker-ce.repo
yum makecache fast
yum -y install docker-ce-18.03.1.ce
systemctl enable --now docker
mkdir -p /etc/docker
cat > /etc/docker/daemon.json <<'EOF'
{
"registry-mirrors": ["https://rbmo5xql.mirror.aliyuncs.com"],
"log-driver":"json-file",
"bip": "192.168.1.5/24",
"log-opts": { "max-size": "50m", "max-file": "1" }
}
EOF
systemctl daemon-reload && systemctl restart docker
cd /data/setup
wget -O docker-compose https://github.com/docker/compose/releases/download/1.29.2/docker-compose-Linux-x86_64
chmod +x docker-compose
cp docker-compose /usr/local/bin/
ln -sf /usr/local/bin/docker-compose /usr/bin/docker-compose
docker-compose -vRabbitMQ Cluster Deployment
Use the rabbitmq:3.8-management image (avoid the Alpine variant due to known vulnerabilities). Pull the image on all three nodes.
# docker pull rabbitmq:3.8-managementCookie Generation
cat > rabbitmq-cookie.sh <<'eof'
docker run -d --name mq rabbitmq:3.8-management
sleep 10
docker exec -it mq cat /var/lib/rabbitmq/.erlang.cookie > .erlang.cookie
chmod 600 .erlang.cookie
docker rm -f mq
docker volume prune
eof
sh rabbitmq-cookie.shRabbitMQ Configuration
cat > rabbitmq.conf <<'eof'
loopback_users.guest = false
listeners.tcp.default = 5672
cluster_formation.peer_discovery_backend = rabbit_peer_discovery_classic_config
cluster_formation.classic_config.nodes.1 = rabbit@pos_rabbitmq_1
cluster_formation.classic_config.nodes.2 = rabbit@pos_rabbitmq_2
cluster_formation.classic_config.nodes.3 = rabbit@pos_rabbitmq_3
eofDocker‑Compose Files
Each node uses a similar docker-compose.yaml with host networking, extra hosts mapping, volume mounts, and environment variables. Memory high‑watermark is set to 0.7 (70 %).
version: "3.6"
services:
pos_rabbitmq_1:
image: rabbitmq:3.8-management
restart: always
container_name: pos_rabbitmq_1
network_mode: host
extra_hosts:
- "pos_rabbitmq_1:172.17.80.27"
- "pos_rabbitmq_2:172.17.80.32"
- "pos_rabbitmq_3:172.17.80.6"
volumes:
- /etc/localtime:/etc/localtime:ro
- /data/apd/rabbitmq:/var/lib/rabbitmq
- ./rabbitmq.conf:/etc/rabbitmq/rabbitmq.conf
- .erlang.cookie:/var/lib/rabbitmq/.erlang.cookie
- ../enabled_plugins:/etc/rabbitmq/enabled_plugins
- /data/logs/rabbitmq:/var/log/rabbitmq
environment:
- LANG=C.UTF-8
- RABBITMQ_DEFAULT_USER=root
- RABBITMQ_DEFAULT_PASS=xxxxxx
- RABBITMQ_VM_MEMORY_HIGH_WATERMARK=0.7Cluster Startup and HA Policy
# docker-compose up -d
# docker logs pos_rabbitmq_1 -f
# docker exec -it pos_rabbitmq_1 rabbitmqctl set_policy ha-all "^" '{"ha-mode":"all"}'Testing
The three nodes form a cluster, HA mirroring is enabled, and memory usage is limited to 70 % of the 8 GB RAM, leaving about 5.2 GB for applications.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
