1. 程式人生 > >Docker:搭建RabbitMQ集群

Docker:搭建RabbitMQ集群

n) png 介紹 let tcp nod comm pytho data-

RabbitMQ原理介紹(一)

RabbitMQ安裝使用(二)

RabbitMQ添加新用戶並支持遠程訪問(三)

RabbitMQ管理命令rabbitmqctl詳解(四)

RabbitMQ兩種集群模式配置管理(五)

RabbitMQ應用實例Python版-Hello World

RabbitMQ應用實例Python版-工作隊列

RabbitMQ應用實例Python版-消息確認和消息持久化

RabbitMQ應用實例Python版-發布/訂閱

RabbitMQ應用實例Python版-路由

Docker搭建RabbitMQ(單機版)

采用bijukunjummen提供的鏡像。

1 $ git clone https://github.com/bijukunjummen/docker-rabbitmq-cluster.git

啟動集群,別忘記安裝docker daemon和docker-compose

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 $ cd docker-rabbitmq-cluster/cluster $ docker-compose up -d Pulling rabbit1 (bijukunjummen/rabbitmq-server:latest)... latest: Pulling from bijukunjummen/rabbitmq-server 8d30e94188e7: Pull complete
4dcc5ee4075c: Pull complete 9554738b08fc: Pull complete efdc65d45fa8: Pull complete 8511de892111: Pull complete 370e6c0ab215: Pull complete 940500e823fe: Pull complete b6134aab1a2a: Pull complete b870c2ab7ca0: Pull complete 56ed51d99ae0: Pull complete adec15369e34: Pull complete 1f44e442c116: Pull complete 0dcd19841c9f: Pull complete
24b8906417b0: Pull complete Digest: sha256:a6bbb268de87d09d538152450b77835a27693039ab079785a7b72f595bef18a6 Status: Downloaded newer image for bijukunjummen/rabbitmq-server:latest Creating cluster_rabbit1_1 Creating cluster_rabbit2_1 Creating cluster_rabbit3_1

默認啟動了三個節點

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 rabbit1: image: bijukunjummen/rabbitmq-server hostname: rabbit1 ports: - "5672:5672" - "15672:15672" rabbit2: image: bijukunjummen/rabbitmq-server hostname: rabbit2 links: - rabbit1 environment: - CLUSTERED=true - CLUSTER_WITH=rabbit1 - RAM_NODE=true ports: - "5673:5672" - "15673:15672" rabbit3: image: bijukunjummen/rabbitmq-server hostname: rabbit3 links: - rabbit1 - rabbit2 environment: - CLUSTERED=true - CLUSTER_WITH=rabbit1 ports: - "5674:5672"

查看容器

1 2 3 4 5 $ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES ba5f665bb213 bijukunjummen/rabbitmq-server "/bin/sh -c /opt/rabb" 10 minutes ago Up 10 minutes 4369/tcp, 9100-9105/tcp, 15672/tcp, 25672/tcp, 0.0.0.0:5674->5672/tcp cluster_rabbit3_1 b9466e206b2b bijukunjummen/rabbitmq-server "/bin/sh -c /opt/rabb" 10 minutes ago Up 10 minutes 4369/tcp, 9100-9105/tcp, 25672/tcp, 0.0.0.0:5673->5672/tcp, 0.0.0.0:15673->15672/tcp cluster_rabbit2_1 b733201aeadf bijukunjummen/rabbitmq-server "/bin/sh -c /opt/rabb" 10 minutes ago Up 10 minutes 4369/tcp, 0.0.0.0:5672->5672/tcp, 9100-9105/tcp, 0.0.0.0:15672->15672/tcp, 25672/tcp cluster_rabbit1_1 daomonit

訪問
http://192.168.99.100:15672,彈出登陸界面。輸入guest/guest

技術分享圖片

然後就可以添加用戶了。

Docker搭建RabbitMQ集群(多機版)

上面我們使用Docker搭建出了一個RabbitMQ單機集群,這種模式只能用來測試玩玩,無法再生產環境中使用。在生產環境中,需要把3個節點的集群分布到各個主機上面去。這個時候docker-compose就需要做調整了,主要是對外端口和link方式,首先我們說一下RabbitMQ開啟後的對外端口。

SELinux和類似機制或許會通過綁定端口的方式阻止RabbitMQ。當這種情況發生時,RabbitMQ會啟動失敗。請確認以下的端口是可以被打開的:

4369 (epmd)

25672 (Erlang distribution)

5672, 5671 (啟用了或者未啟用TLS的AMQP 0-9-1)

15672 (如果管理插件被啟用)

61613, 61614 (如果STOMP被啟用)

1883, 8883 (如果MQTT被啟用)

說完端口,下面提供一個三節點的docker-compose文件。

rabbit1(10.106.136.7)

1 2 3 4 5 6 7 8 9 10 11 12 rabbit1: image: bijukunjummen/rabbitmq-server hostname: rabbit1 ports: - "5672:5672" - "4369:4369" - "1883:1883" - "15672:15672" - "25672:25672" environment: - RABBITMQ_DEFAULT_USER=myuser - RABBITMQ_DEFAULT_PASS=mypass

rabbit2(10.106.136.8)

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 rabbit2: image: bijukunjummen/rabbitmq-server hostname: rabbit2 extra_hosts: - "rabbit1:10.106.136.7" environment: - CLUSTERED=true - CLUSTER_WITH=rabbit1 - RAM_NODE=true ports: - "5672:5672" - "4369:4369" - "1883:1883" - "15672:15672" - "25672:25672"

rabbit3(10.106.136.9)

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 rabbit3: image: bijukunjummen/rabbitmq-server hostname: rabbit3 extra_hosts: - "rabbit1:10.106.136.7" - "rabbit2:10.106.136.8" environment: - CLUSTERED=true - CLUSTER_WITH=rabbit1 - RAM_NODE=true ports: - "5672:5672" - "4369:4369" - "1883:1883" - "15672:15672" - "25672:25672"

然後依序在三個節點上分別開啟rabbitmq,然後隨意找個節點打開IP:25672,輸入guest用戶名和guest密碼就可以查看集群了。

Docker:搭建RabbitMQ集群