Rabbitmq集群docker单机部署

Rabbitmq集群docker单机部署

服务器准备

系统版本 系统规格 IP地址
centos7.9 2c4g80G 192.168.117.151

本次使用单机docker集群部署,docker安装详情其他文档

集群部署

拉取镜像

1
docker pull rabbitmq:3.7-management

创建映射卷

1
2
3
4
docker volume create rabbitmq01
docker volume create rabbitmq02
docker volume create rabbitmq03
docker volume list

image-20231008095158772

启动容器

节点1

1
docker run -d --hostname rabbitmq01 --name rabbitmqCluster01 -v rabbitmq01:/var/lib/rabbitmq -p 15673:15672 -p 5673:5672 -e RABBITMQ_ERLANG_COOKIE='rabbitmqCookie' rabbitmq:3.7-management

节点2

1
docker run -d --hostname rabbitmq02 --name rabbitmqCluster02 -v rabbitmq02:/var/lib/rabbitmq -p 15674:15672 -p 5674:5672 -e RABBITMQ_ERLANG_COOKIE='rabbitmqCookie'  --link rabbitmqCluster01:rabbitmq01 rabbitmq:3.7-management

节点3

1
docker run -d --hostname rabbitmq03 --name rabbitmqCluster03 -v rabbitmq03:/var/lib/rabbitmq -p 15675:15672 -p 5675:5672 -e RABBITMQ_ERLANG_COOKIE='rabbitmqCookie'  --link rabbitmqCluster01:rabbitmq01 --link rabbitmqCluster02:rabbitmq02  rabbitmq:3.7-management

查询容器启动情况 up为启动成功

1
docker ps

image-20231008095404875

注: –hostname 设置容器的主机名RABBITMQ_ERLANG_COOKIE 节点认证作用,部署集成时 需要同步该值

启动容器成功后,可以访问

1
2
3
192.168.117.151:15673
192.168.117.151:15674
192.168.117.151:15675

账号/密码:guest / guest

登陆后,查看overview Tab页,可看到节点信息。

image-20231008095749670

容器节点加入集群

容器1

1
docker exec -it rabbitmqCluster01 bash
1
2
3
4
rabbitmqctl stop_app
rabbitmqctl reset
rabbitmqctl start_app
exit

image-20231008095958185

容器2

1
docker exec -it rabbitmqCluster02 bash
1
2
3
4
5
rabbitmqctl stop_app
rabbitmqctl reset
rabbitmqctl join_cluster --ram rabbit@rabbitmq01
rabbitmqctl start_app
exit

image-20231008100043401

容器3

1
docker exec -it rabbitmqCluster03 bash
1
2
3
4
5
rabbitmqctl stop_app
rabbitmqctl reset
rabbitmqctl join_cluster --ram rabbit@rabbitmq01
rabbitmqctl start_app
exit

image-20231008100123611

执行上述操作,这时候 再查看 http://192.168.117.151:15673/#/overview面板中的Nodes信息,可查看到节点信息。

image-20231008100227856

现在集群就已经部署完毕

负载均衡设置

有了集群之后 还要设置负载均衡,为了防止出现对单一节点造成高负载的情况。 本次测试用例 采用nginx为中间件,可根据自身需要进行选择。

拉取nginx镜像

1
docker pull nginx

创建映射目录及配置文件

1
2
mkdir /home/rabbitmqnginx
vim /home/rabbitmqnginx/rabbitmq.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
user  nginx;
worker_processes 2;

error_log /var/log/nginx/error.log warn;
pid       /var/run/nginx.pid;


events {
  worker_connections 10240;
}


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 192.168.117.151:15673;
      server 192.168.117.151:15674;
      server 192.168.117.151:15675;
  }
  server {
      listen       15672;
      server_name 192.168.117.151;
      location / {
          proxy_pass   http://rabbitManage;
      }

  }
}
# rabbitmq通信
stream{
  upstream rabbitTcp{
      server 192.168.117.151:5673;
      server 192.168.117.151:5674;
      server 192.168.117.151:5675;
  }

  server {
      listen 5672;
      proxy_pass rabbitTcp;
  }
}

image-20231008101459609

创建nginx容器

1
docker run  -d --name nginxRabbitmq -v /home/rabbitmqnginx/rabbitmq.conf:/etc/nginx/nginx.conf  --privileged --net=host nginx

image-20231008101234891

然后可以通过 192.168.117.151:15672 进行管理 以及通过 5672 端口 进行rabbitmq通信。

1
192.168.117.151:15672

image-20231008101434232