docker-compose部署RabbitMQ镜像集群
新建rabbitmq文件夹和其他文件如图:
编写docker-compose.yml
version: '3'
services:
rabbitmq01:
image: rabbitmq:3.8-management
container_name: rabbitmq01
ports:
- "15673:15672"
- "5673:5672"
hostname: rabbitmq01
environment:
- RABBITMQ_DEFAULT_USER=admin
- RABBITMQ_DEFAULT_PASS=admin
- RABBITMQ_ERLANG_COOKIE=rabbitcookie
volumes:
- /usr/local/rabbitmq/data1:/var/lib/rabbitmq
networks:
rabbitmq:
ipv4_address: "10.10.10.0"
rabbitmq02:
image: rabbitmq:3.8-management
container_name: rabbitmq02
ports:
- "15674:15672"
- "5674:5672"
hostname: rabbitmq02
environment:
- RABBITMQ_DEFAULT_USER=admin
- RABBITMQ_DEFAULT_PASS=admin
- RABBITMQ_ERLANG_COOKIE=rabbitcookie
volumes:
- /usr/local/rabbitmq/data2:/var/lib/rabbitmq
networks:
rabbitmq:
ipv4_address: "10.10.10.1"
rabbitmq03:
image: rabbitmq:3.8-management
container_name: rabbitmq03
ports:
- "15675:15672"
- "5675:5672"
hostname: rabbitmq03
environment:
- RABBITMQ_DEFAULT_USER=admin
- RABBITMQ_DEFAULT_PASS=admin
- RABBITMQ_ERLANG_COOKIE=rabbitcookie
volumes:
- /usr/local/rabbitmq/data3:/var/lib/rabbitmq
networks:
rabbitmq:
ipv4_address: "10.10.10.2"
nginxrabbitmq:
image: nginx:latest
container_name: nginxrabbitmq
network_mode: host
volumes:
- /usr/local/rabbitmq/nginx/nginx.conf:/etc/nginx/nginx.conf
privileged: true
networks:
rabbitmq:
driver: bridge
ipam:
config:
- subnet: "10.10.10.0/16"
编写nginx.conf
user nginx;
worker_processes 2;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 5s;
proxy_send_timeout 5s;
proxy_read_timeout 5s;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
#rabbitmq管理界面
upstream rabbitManage {
server localhost:15673;
server localhost:15674;
server localhost:15675;
}
server {
listen 15672;
server_name localhost;
location / {
proxy_pass http://rabbitManage;
index index.html index.htm;
}
}
}
# rabbitmq通信
stream {
upstream rabbitTcp{
server localhost:5673;
server localhost:5674;
server localhost:5675;
}
server {
listen 5672;
proxy_pass rabbitTcp;
}
}
编写配置shell脚本
#!/bin/bash
#reset first node
echo "Reset first rabbitmq node."
docker exec rabbitmq01 /bin/bash -c 'rabbitmqctl stop_app'
docker exec rabbitmq01 /bin/bash -c 'rabbitmqctl reset'
docker exec rabbitmq01 /bin/bash -c 'rabbitmqctl start_app'
#build cluster
echo "Starting to build rabbitmq cluster with two ram nodes."
docker exec rabbitmq02 /bin/bash -c 'rabbitmqctl stop_app'
docker exec rabbitmq02 /bin/bash -c 'rabbitmqctl reset'
docker exec rabbitmq02 /bin/bash -c 'rabbitmqctl join_cluster --ram rabbit@rabbitmq01'
docker exec rabbitmq02 /bin/bash -c 'rabbitmqctl start_app'
docker exec rabbitmq03 /bin/bash -c 'rabbitmqctl stop_app'
docker exec rabbitmq03 /bin/bash -c 'rabbitmqctl reset'
docker exec rabbitmq03 /bin/bash -c 'rabbitmqctl join_cluster --ram rabbit@rabbitmq01'
docker exec rabbitmq03 /bin/bash -c 'rabbitmqctl start_app'
#check cluster status
echo "Check cluster status:"
docker exec rabbitmq01 /bin/bash -c 'rabbitmqctl cluster_status'
docker exec rabbitmq02 /bin/bash -c 'rabbitmqctl cluster_status'
docker exec rabbitmq03 /bin/bash -c 'rabbitmqctl cluster_status'
启动集群docker-compose up -d
在执行配置脚本
记得给执行权限
chmod +x *.sh
已经配置好了
访问rabbitmq的管理页面