Docker部署RabbitMQ集群

安装rabbitmq

下载Docker镜像

docker pull rabbitmq:3.7.12-management

安装RabbitMQ

mkdir rabbitmq
# node1 172.17.11.22 
cd rabbitmq
vim hosts
#输入
172.17.11.22 rabbit1 rabbit1
172.17.11.23 rabbit2 rabbit2

sudo docker run -d \
    --name rabbit1  \
    -h rabbit1\
    --log-opt max-size=10m \
    --log-opt max-file=3 \
    -v /root/rabbitmq:/var/lib/rabbitmq:z \
    -v /root/rabbitmq/hosts:/etc/hosts \
    -e RABBITMQ_DEFAULT_USER=rabbitmq   \
    -e RABBITMQ_DEFAULT_PASS=password \
    -e RABBITMQ_ERLANG_COOKIE='cookie' \
    -p 5656:5672 \
    -p 5657:15672 \
    -p 4369:4369 \
    -p 25672:25672 \
    -p 15671:15671 \
    -p 5671:5671 \
    rabbitmq:3.7.12-management

# node2 172.17.11.23
cd rabbitmq
vim hosts
#输入
172.17.11.22 rabbit1 rabbit1
172.17.11.23 rabbit2 rabbit2
sudo docker run -d \
    --name rabbit2  \
    -h rabbit2\
    --log-opt max-size=10m \
    --log-opt max-file=3 \
    -v /root/rabbitmq:/var/lib/rabbitmq:z \
    -v /root/rabbitmq/hosts:/etc/hosts \
    -e RABBITMQ_DEFAULT_USER=rabbitmq   \
    -e RABBITMQ_DEFAULT_PASS=password \
    -e RABBITMQ_ERLANG_COOKIE='cookie' \
    -p 5656:5672 \
    -p 5657:15672 \
    -p 4369:4369 \
    -p 25672:25672 \
    -p 15671:15671 \
    -p 5671:5671 \
    rabbitmq:3.7.12-management

加入集群

上述完成2个RabbitMQ节点的启动,后面是要将两个节点做成一个集群,现在讲rabbitmq1作为master,然后rabbitmq2加入到rabbitmq1中

# node2 处理
sudo docker exec -it rabbit2 /bin/bash
# 加入集群
rabbitmqctl stop_app
rabbitmqctl join_cluster rabbit@rabbit1
rabbitmqctl start_app

使用删除方式处理时出现过以下问题

rabbit@localhost:
      * connected to epmd (port 4369) on localhost
      * epmd reports node 'rabbit' running on port 25672
      * TCP connection succeeded but Erlang distribution failed

      * Hostname mismatch: node "rabbit@warp10" believes its host is different. Please ensure that hostnames resolve the same way locally and on "rabbit@warp10"


    current node details:
    - node name: 'rabbitmq-cli-30@warp10'
    - home dir: /Users/antares
    - cookie hash: Sg08R8+G85EYHZ3H/9NUfg==

官网中有问题显示,但是具体情况很多,经过后续的分析,是因为在docker run时hostname的问题,所以在run时指定容器的hostname。也就是通过-h来指定hostname,根本问题就是HOSTNAME

解决方法,在Docker创建时,指定了/etc/hosts文件,但是启动docker时,应用里的hostname与hosts中指定的hostname不相同

1. echo $HOSTNAME 查看下当前的hostname  然后export HOSTNAME=*** 修改为/etc/hosts希望的名字
2. /etc/hostname中科院设置相应的数据

使用docker部署rabbitmq集群时,因为hostname不匹配,导致的问题,建议在run时加入参数-h来指定参数,保证hostname的正确性

配置nginx,来保证集群的可用性

配置文件如下:

server {
    listen 443;
    server_name rabbitmq.example.cn;

    ssl on;
    ssl_certificate   cert/123.pem;
    ssl_certificate_key  cert/123.key;
    ssl_session_timeout 5m;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    
    # https 因为安全原因,需要禁用 gzip
    # 但是在一些场景下,不需要禁用
    # gzip off;
    gzip on;
    # 设置允许压缩的页面最小字节数
    gzip_min_length 1000;
    gzip_buffers 4 16k;
    gzip_http_version 1.1;
    # 1~9,默认为1,数值越大,压缩率越高,CPU占用越多,时间越久
    gzip_comp_level 3;
    gzip_vary on;
    # 禁用对 IE 6 使用 gzip 压缩
    gzip_disable "MSIE [1-6]\.";
    gzip_types text/plain text/css text/xml text/javascript application/x-javascript application/javascript "application/javascript; charset=utf-8" application/xml application/xml+rss application/json "application/json; charset=utf-8" fo
nt/ttf font/otf image/svg+xml;

    # 设置最大允许的POST数据量,如果提交的文件超过这个值,会出现413错误
    client_max_body_size 20m;
    keepalive_timeout 15;

    # 不显示 nginx 的版本号
    server_tokens off;

    # 设置请求头的长度大小
    client_header_buffer_size 32k;
    large_client_header_buffers 4 32k;


    ## Individual nginx logs
    access_log  /var/log/nginx/access.log main;
    error_log   /var/log/nginx/error.log;
    # 不保存 access_log
    # access_log off;

    location ^~ /favicon.ico {
        expires 30d;
        # add_header Content-Type text/plain;
        return 200 "";
    }

    location ^~ /robots.txt {
        expires 30d;
        # add_header Content-Type text/plain;
        return 200 "User-Agent:  *\nDisallow:  /";
    }

    location / {
        port_in_redirect on;
        proxy_pass   http://rabbitManage;
        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;
        #index  index.html index.htm;
    }
    
    # 主页返回 404
    # location = / {
    #     return 404;
    # }
}


server {
   listen 80;
   server_name  rabbitmq.example.cn;
   #告诉浏览器有效期内只准用 https 访问
   add_header Strict-Transport-Security max-age=15768000;
   #永久重定向到 https 站点
   return 301 https://$server_name$request_uri;
}

#rabbitmq管理界面
upstream rabbitManage {
    server 172.17.11.22:5657;
    server 172.17.11.23:5657;
}


#!!! stream 要和http一个层级
# rabbitmq Tcp监听
stream{
    upstream rabbitTcp{
        server 172.17.11.22:5656;
        server 172.17.11.23:5656;
    }

    server {
        listen 5656;
        proxy_pass rabbitTcp;
    }
}

注意:stream要和http一个层级

参考资料

Docker分布式部署RabbitMQ集群

你可能感兴趣的:(RabbitMQ,技术小栈)