Docker - building rabbitmq cluster

Keywords: RabbitMQ Docker Nginx firewall

Article directory

Docker - building rabbitmq cluster

1. First, pull the image of rabbitmq

Let's take version 3.8.2 as an example

docker pull rabbitmq:3.8.2-management

2. Then run three instances of rabbitmq

# Running image, cluster mode
docker run \
  --name rabbitmq-1 \
  --hostname=rabbitmq-1 \
  --restart=always \
  -d \
  -e RABBITMQ_DEFAULT_USER=rabbitmq \
  -e RABBITMQ_DEFAULT_PASS=OnlinezuozuoCreated.rabbitmq.test.password \
  -e RABBITMQ_ERLANG_COOKIE='oNYE40jp2YbuXfWKd9vFu1VZiic8' \
  -v /etc/docker/volume/rabbitmqcluster/rabbitmq-1:/var/lib/rabbitmq \
  -p 52001:5672 \
  -p 52002:15672 \
  rabbitmq:3.8.2-management

docker run \
  --name rabbitmq-2 \
  --hostname=rabbitmq-2 \
  --restart=always \
  --link rabbitmq-1:rabbitmq-1 \
  -d \
  -e RABBITMQ_DEFAULT_USER=rabbitmq \
  -e RABBITMQ_DEFAULT_PASS=OnlinezuozuoCreated.rabbitmq.test.password \
  -e RABBITMQ_ERLANG_COOKIE='oNYE40jp2YbuXfWKd9vFu1VZiic8' \
  -v /etc/docker/volume/rabbitmqcluster/rabbitmq-2:/var/lib/rabbitmq \
  -p 52301:5672 \
  -p 52302:15672 \
  rabbitmq:3.8.2-management

docker run \
  --name rabbitmq-3 \
  --hostname=rabbitmq-3 \
  --restart=always \
  --link rabbitmq-1:rabbitmq-1 \
  --link rabbitmq-2:rabbitmq-2 \
  -d \
  -e RABBITMQ_DEFAULT_USER=rabbitmq \
  -e RABBITMQ_DEFAULT_PASS=OnlinezuozuoCreated.rabbitmq.test.password \
  -e RABBITMQ_ERLANG_COOKIE='oNYE40jp2YbuXfWKd9vFu1VZiic8' \
  -v /etc/docker/volume/rabbitmqcluster/rabbitmq-3:/var/lib/rabbitmq \
  -p 52501:5672 \
  -p 52502:15672 \
  rabbitmq:3.8.2-management


Rabbitmq ﹣ Erlang ﹣ cookie is the communication string of rabbitmq cluster, which is used for cluster connection, so it must be consistent

--link rabbitmq-1:rabbitmq-1 \ by connecting instance 2 and 3, we can make them communicate

3. Then we build the cluster and synchronize the time zone

Enter instance 1, and execute the following commands one by one

docker exec -it rabbitmq-1 bash
rabbitmqctl stop_app
rabbitmqctl reset
rabbitmqctl start_app
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime &&
  echo "Asia/Shanghai" >/etc/timezone
date "+%Y-%m-%d %H:%M:%S" ## After displaying the settings, the new system time
exit


Enter instance 2, and execute the following commands one by one

docker exec -it rabbitmq-2 bash
rabbitmqctl stop_app
rabbitmqctl reset
rabbitmqctl join_cluster --ram rabbit@rabbitmq-1
rabbitmqctl start_app
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime &&
  echo "Asia/Shanghai" >/etc/timezone
date "+%Y-%m-%d %H:%M:%S" ## After displaying the settings, the new system time
exit

Enter instance 3, and execute the following commands one by one

docker exec -it rabbitmq-3 bash
rabbitmqctl stop_app
rabbitmqctl reset
rabbitmqctl join_cluster --ram rabbit@rabbitmq-1
rabbitmqctl start_app
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime &&
  echo "Asia/Shanghai" >/etc/timezone
date "+%Y-%m-%d %H:%M:%S" ## After displaying the settings, the new system time
exit

4. How to delete a container

In case of startup failure or port occupation, we can stop the container and delete it for regeneration

docker container stop rabbitmq-1
docker container rm rabbitmq-1

docker container stop rabbitmq-2
docker container rm rabbitmq-2

docker container stop rabbitmq-3
docker container rm rabbitmq-3

5. Open the firewall or port controller for port release and check the operation status

By visiting

http://localhost:52101/

http://localhost:52301/

http://localhost:52501/

Verify that you can log in normally. The account is the account and password defined in the above script

And check whether the cluster is in good condition.

6. Load balancing through nginx

nginx increases load balancing of management pages

upstream rabbitmq {   
  	 server localhost:52002 weight=1 max_fails=2 fail_timeout=10s;   
	 server localhost:52302 weight=1 max_fails=2 fail_timeout=10s;
	 server localhost:52502 weight=1 max_fails=2 fail_timeout=10s;
}

location /
{
    proxy_pass http://rabbitmq;
    proxy_set_header Host rabbitmq;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header REMOTE-HOST $remote_addr;
    
    add_header X-Cache $upstream_cache_status;
    
    #Set Nginx Cache
    
    	add_header Cache-Control no-cache;
    expires 12h;
}

In this way, we can use nginx's reverse proxy to perform load balancing access to the console

nginx increases load balancing of connection ports

stream{

upstream rabbitmq {   
  	 server localhost:52001 weight=1 max_fails=2 fail_timeout=10s;   
	 server localhost:52301 weight=1 max_fails=2 fail_timeout=10s;
	 server localhost:52501 weight=1 max_fails=2 fail_timeout=10s;
}


server{
    listen 52701;
    proxy_connect_timeout 20s;
	proxy_pass rabbitmq;
}

}

In this way, we can use nginx's reverse proxy to load balance the data connection

446 original articles published, praised 1344, visited 2360000+
His message board follow

Posted by gabe33 on Thu, 27 Feb 2020 01:17:07 -0800