rabbit-mq集群docker搭建笔记
1、yum 包更新到最新
yum update
2、安装需要的软件包, yum-util 提供yum-config-manager功能,另外两个是devicemapper驱动依赖的
yum install -y yum-utils device-mapper-persistent-data lvm2
3、 设置yum源
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
4、 安装docker,出现输入的界面都按 y
yum install -y docker-ce
docker -v
6、 docker自启
启动:systemctl start docker
自启systemctl enable docker
2.安装mq
镜像拉取
docker pull rabbitmq:3.7-management
创建挂载目录
mkdir /opt/module/rabbitmq cd /opt/module/rabbitmq mkdir rabbitmq01 rabbitmq02 rabbitmq03
创建容器
docker run -d --hostname rabbitmq01 --name rabbitmq_node1 -v /opt/module/rabbitmq/rabbitmq01:/var/lib/rabbitmq -p 15672:15672 -p 5672:5672 -e RABBITMQ_ERLANG_COOKIE='rabbitmqCookie' rabbitmq:3.7-management
docker run -d --hostname rabbitmq02 --name rabbitmq_node2 -v /opt/module/rabbitmq/rabbitmq02:/var/lib/rabbitmq -p 15673:15672 -p 5673:5672 -e RABBITMQ_ERLANG_COOKIE='rabbitmqCookie' --link rabbitmq_node1:rabbitmq01 rabbitmq:3.7-management
docker run -d --hostname rabbitmq03 --name rabbitmq_node3 -v /opt/module/rabbitmq/rabbitmq03:/var/lib/rabbitmq -p 15674:15672 -p 5674:5672 -e RABBITMQ_ERLANG_COOKIE='rabbitmqCookie' --link rabbitmq_node1:rabbitmq01 --link rabbitmq_node2:rabbitmq02 rabbitmq:3.7-management
测试是否启动成功
主机名:15672/#/
主机名:15673/#/
主机名:15674/#/
账号密码:guest/guest
3.进入容器
节点1:
docker exec -it rabbitmq_node1 bash rabbitmqctl stop_app rabbitmqctl reset rabbitmqctl start_app exit
节点二:
docker exec -it rabbitmq_node2 bash rabbitmqctl stop_app rabbitmqctl reset rabbitmqctl join_cluster --ram rabbit@rabbitmq01 rabbitmqctl start_app exit
节点三:
docker exec -it rabbitmq_node3 bash rabbitmqctl stop_app rabbitmqctl reset rabbitmqctl join_cluster --ram rabbit@rabbitmq01 rabbitmqctl start_app exit
集群实现:
docker exec -it rabbitmq_node1 bash rabbitmqctl set_policy ha-all "^" '{"ha-mode":"all"}'
注意
在cluster中任意节点启用策略,策略会自动同步到集群节点
1 rabbitmqctl set_policy-p/ha-all"^"’{“ha-mode”:“all”}’
这行命令在vhost名称为hrsystem创建了一个策略,策略名称为ha-allqueue,策略模式为 all 即复制到所有节点,包含新增节点,策略正则表达式为 “^” 表示所有匹配所有队列名称。例如:
rabbitmqctl set_policy-p/ha-all"^message"'{"ha-mode":"all"}'
备注:“^message” 这个规则要根据自己修改,这个是指同步“message”开头的队列名称,我们配置时使用的应用于所有队列,所以表达式为“^”
4.测试集群:
主机名:15672/#/
5.常用命令:
启动rabbitmq
rabbitmqctl start_app
停止rabbitmq
rabbitmqctl stop_app
rabbitmq 查看所有队列信息
rabbitmqctl list_queues
还原 rabbitmq 清除所有队列
rabbitmqctl reset
rabbitmq 查看用户
rabbitmqctl list_users
rabbitmq添加用户
rabbitmqctl add_user root root
rabbitmq设置权限
rabbitmqctl set_permissions -p / root ".*" ".*" ".*"
rabbitmq查看集群状态信息
rabbitmqctl cluster_status
移除某个集群节点:一般情况当节点故障时,在正常的节点中,移除该故障节点
rabbitmqctl -n rabbit@rabbitmq01 forget_cluster_node rabbit@rabbitmq03
如果遇到nginx 502问题请关闭防火墙。
systemctl stop firewalld.service
5.资料:
https://www.processon.com/view/link/617be37ee0b34d7894fcf545
6.搭建负载均衡
nginx_rabbitmq_1.conf
user nginx; worker_processes 1; 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 192.168.3.161:15672; server 192.168.3.161:15673; server 192.168.3.161:15674; } server { listen 15675; server_name 192.168.3.161; location / { proxy_pass http://rabbitManage; index index.html index.htm; } } } # rabbitmq通信 stream{ upstream rabbitTcp{ server 192.168.3.161:5672; server 192.168.3.161:5673; server 192.168.3.161:5674; } server { listen 5675; proxy_pass rabbitTcp; } }
nginx_rabbitmq_2.conf
user nginx; worker_processes 1; 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 192.168.3.161:15672; server 192.168.3.161:15673; server 192.168.3.161:15674; } server { listen 15676; server_name 192.168.3.161; location / { proxy_pass http://rabbitManage; index index.html index.htm; } } } # rabbitmq通信 stream{ upstream rabbitTcp{ server 192.168.3.161:5672; server 192.168.3.161:5673; server 192.168.3.161:5674; } server { listen 5676; proxy_pass rabbitTcp; } }
nginx容器启动
docker run -it -d --name nginxRabbitmq -v /opt/module/nginx/nginx_rabbitmq.conf:/etc/nginx/nginx.conf --privileged --net=host nginx docker run -it -d --name nginxRabbitmq1 -v /opt/module/nginx/nginx_rabbitmq_1.conf:/etc/nginx/nginx.conf --privileged --net=host nginx docker run -it -d --name nginxRabbitmq2 -v /opt/module/nginx/nginx_rabbitmq_2.conf:/etc/nginx/nginx.conf --privileged --net=host nginx