- 单个nginx代理+镜像队列
- nginx+keepalived实现高可用
拉取镜像
# 拉取镜像 docker pull rabbitmq:3.8.7-management # 创建挂载目录 mkdir -p /data/rabbitmqcluster/{rabbitmq01,rabbitmq02,rabbitmq03}
创建容器
# 第一个 docker run -d --hostname rabbitmq01 --restart=always --name rabbitmqCluster01 -v /data/rabbitmqcluster/rabbitmq01:/var/lib/rabbitmq -p 15672:15672 -p 5672:5672 -e RABBITMQ_ERLANG_cookie='rabbitmqcookie' rabbitmq:3.8.7-management # 第二个 docker run -d --hostname rabbitmq02 --restart=always --name rabbitmqCluster02 -v /data/rabbitmqcluster/rabbitmq02:/var/lib/rabbitmq -p 15673:15672 -p 5673:5672 -e RABBITMQ_ERLANG_cookie='rabbitmqcookie' --link rabbitmqCluster01:rabbitmq01 rabbitmq:3.8.7-management # 第三个 docker run -d --hostname rabbitmq03 --restart=always --name rabbitmqCluster03 -v /data/rabbitmqcluster/rabbitmq03:/var/lib/rabbitmq -p 15674:15672 -p 5674:5672 -e RABBITMQ_ERLANG_cookie='rabbitmqcookie' --link rabbitmqCluster01:rabbitmq01 --link rabbitmqCluster02:rabbitmq02 rabbitmq:3.8.7-management
–name 镜像名
-d 后台运行
-v /宿主机目录:/容器目录 镜像名
–restart=always 自动重启
-p 端口映射
-e RABBITMQ_ERLANG_cookie 配置相同的Erlang cookie; 因为RabbitMQ使用Erlang实现的,Erlang cookie相当于不同节点之间相互通讯的秘钥,Erlang节点通过交换Erlang cookie获得认证
–link 多个容器之间用–link连接
启动成功后可以通过 IP:15672、IP:15673、IP:15674 访问 账号/密码:guest / guest。
进入容器,加入集群
# 第一个节点 docker exec -it rabbitmqCluster01 /bin/bash rabbitmqctl stop_app rabbitmqctl reset rabbitmqctl start_app exit
这里如果出现这种错误,需要将所有容器移除,把 -e RABBITMQ_ERLANG_cookie=‘rabbitmqcookie’ 这个去掉再运行一遍。
# 第二个节点 docker exec -it rabbitmqCluster02 /bin/bash rabbitmqctl stop_app rabbitmqctl reset rabbitmqctl join_cluster --ram rabbit@rabbitmq01 rabbitmqctl start_app exit
# 第三个节点 docker exec -it rabbitmqCluster03 /bin/bash rabbitmqctl stop_app rabbitmqctl reset rabbitmqctl join_cluster --ram rabbit@rabbitmq01 rabbitmqctl start_app exit
所有节点都加入集群后,实现镜像集群
docker exec -it rabbitmqCluster01 /bin/bash # 设置同步策略 rabbitmqctl set_policy ha-all "^" '{"ha-mode":"all","ha-sync-mode":"automatic"}'
rabbitmqctl set_policy ha-all “^” ‘{“ha-mode”:“all”,“ha-sync-mode”:“automatic”}’
“^” 队列名字的通配符 ^ 表示所有匹配所有队列名称
ha-all 策略名称,可以自定义
ha-mode 镜像队列提供了三种模式:
- all:全部的节点队列都做镜像
- exactly:指定镜像队列的节点最高镜像数量
- nodes:只为指定具体节点配置镜像队列
ha-mode 是同步方式
搭建nginx代理
# 创建挂载目录 mkdir -p /data/nginx/conf # 创建nginx.conf配置文件 vim nginx.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 172.26.160.253:15672; # 这里的 IP 替换成自己宿主机的IP server 172.26.160.253:15673; server 172.26.160.253:15674; } server { listen 15675; server_name 114.215.186.185; # 这里是服务器公网IP location / { proxy_pass http://rabbitManage; index index.html index.htm; } } } # rabbitmq通信 stream{ upstream rabbitTcp{ server 172.26.160.253:5672; server 172.26.160.253:5673; server 172.26.160.253:5674; } server { listen 5675; proxy_pass rabbitTcp; } }
查看IP的方法
ip addr ifconfig hostname -i
创建nginx容器
docker run -it -d --name=nginx -v /data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf --privileged=true --net=host nginx
–privileged 是否让docker 应用容器 获取宿主机root权限(特殊权限-)
–net=host 不需要再做端口映射了
Nginx 启动完成后,可以通过 IP:15675 访问管理界面,IP:5675连接mq了。
通过Admin > Policies 可以看到我们创建的策略,再页面使用Add也可以创建策略。
集群搭建步骤和上面单个nginx一样,这里我们加入keepalived实现高可用容错。
上面已经搭建了一个nginx我们还需要搭建一个nginx。
搭建nginx
cd /data/nginx/conf # 创建配置文件 vim nginx2.conf
这里我们把公网的IP都换成了宿主机的IP。然后使用 IP:15678 进行管理。
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 172.26.160.253:15672; # 这里的 IP 替换成自己宿主机的IP server 172.26.160.253:15673; server 172.26.160.253:15674; } server { listen 15676; server_name 172.26.160.253; # 这里我们使用 keepalived 来管理就不使用公网IP了 location / { proxy_pass http://rabbitManage; index index.html index.htm; } } } # rabbitmq通信 stream{ upstream rabbitTcp{ server 172.26.160.253:5672; server 172.26.160.253:5673; server 172.26.160.253:5674; } server { listen 5675; proxy_pass rabbitTcp; } }
创建第二个nginx容器
docker run -it -d --name=nginx2 -v /data/nginx/conf/nginx2.conf:/etc/nginx/nginx.conf --privileged=true --net=host nginx
在第一个nginx容器上安装keepalived 实现HA
docker exec -it nginx /bin/bash # 更新数据源列表 apt-get -y update # 安装 vim 编辑器 apt-get -y install vim # 安装keepalived apt-get -y install keepalived
安装完成后,编写配置文件
vim /etc/keepalived/keepalived.conf
keepalived.conf 配置详细说明请看keepalived.conf详解
! Configuration File for keepalived global_defs { notification_email { ddz@163.com } notification_email_from 1584@qq.com smtp_server 192.168.200.1 smtp_connect_timeout 30 router_id keepalivep1 # 唯一 vrrp_skip_check_adv_addr vrrp_strict vrrp_garp_interval 0 vrrp_gna_interval 0 } vrrp_instance VI_1 { state MASTER # 主节点 interface eth0 # 网卡 virtual_router_id 51 # 唯一 1~255 两节点保持一致 priority 100 # 权重 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 172.26.160.222 # 虚拟IP } } # 通过 ip addr 查看当前服务的ip virtual_server 172.26.160.235 15678 { delay_loop 3 lb_algo rr lb_kind NAT persistence_timeout 50 protocol TCP real_server 172.26.160.235 15675 { weight 1 } } virtual_server 172.26.160.235 5678 { delay_loop 3 lb_algo rr lb_kind NAT persistence_timeout 50 protocol TCP real_server 172.26.160.235 5675 { weight 1 } }
保存配置,启动keepalived。
service keepalived start # 启动 service keepalived restart # 重启
在第一个nginx容器上安装keepalived 实现HA
docker exec -it nginx2 /bin/bash # 更新数据源列表 apt-get -y update # 安装 vim 编辑器 apt-get -y install vim # 安装keepalived apt-get -y install keepalived
安装完成后,编写配置文件
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived global_defs { notification_email { ddz@163.com } notification_email_from 1584@qq.com smtp_server 192.168.200.1 smtp_connect_timeout 30 router_id keepalivep2 # 唯一 vrrp_skip_check_adv_addr vrrp_strict vrrp_garp_interval 0 vrrp_gna_interval 0 } vrrp_instance VI_1 { state BACKUP # 备份节点 interface eth0 # 网卡 virtual_router_id 51 # 唯一 1~255 两节点保持一致 priority 100 # 权重 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 172.26.160.222 # 虚拟IP } } # 通过 ip addr 查看当前服务的ip virtual_server 172.26.160.235 15678 { delay_loop 3 lb_algo rr lb_kind NAT persistence_timeout 50 protocol TCP real_server 172.26.160.235 15676 { weight 1 } } virtual_server 172.26.160.235 5678 { delay_loop 3 lb_algo rr lb_kind NAT persistence_timeout 50 protocol TCP real_server 172.26.160.235 5676 { weight 1 } }
保存配置,启动keepalived。
service keepalived start # 启动 service keepalived restart # 重启
主机上安装keepaplived进行转发
- 安装keepalived
yum -y install keepalived
- 安装完成后,编写配置文件keepalived.conf
! Configuration File for keepalived global_defs { notification_email { ddz@163.com } notification_email_from 1584@qq.com smtp_server 192.168.200.1 smtp_connect_timeout 30 router_id keepalivep vrrp_skip_check_adv_addr vrrp_strict vrrp_garp_interval 0 vrrp_gna_interval 0 } vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 114.215.186.111 # 这里是自己的公网IP } } virtual_server 114.215.186.111 15678 {# 这里是自己的公网IP delay_loop 3 lb_algo rr lb_kind NAT persistence_timeout 50 protocol TCP real_server 172.26.160.222 15676 { # 这里的IP是 在容器里配置的虚拟IP weight 1 } } virtual_server 114.215.186.111 5678 { delay_loop 3 lb_algo rr lb_kind NAT persistence_timeout 50 protocol TCP real_server 172.26.160.222 5676 { weight 1 } }
- 保存完配置后,启动keepalived。
service keepalived start # 启动 service keepalived restart # 重启
- 启动完成后,就可以通过 IP:15678 访问控制台了,通过 IP:5678 连接。
转载于:转载地址
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)