拉取rabbitmq management鏡像
docker pull rabbitmq:3.7-rc-management
若不使用Rabbitmq的management功能,可以拉取鏡像:rabbitmq:3.7-rc
參考: https://hub.docker.com/_/rabbitmq/
創(chuàng)建網(wǎng)絡
創(chuàng)建rabbitmq私有網(wǎng)絡
# docker network create rabbitmqnet
# docker network ls
NETWORK ID NAME DRIVER SCOPE
65b44ea8847c rabbitmqnet bridge local
...
創(chuàng)建節(jié)點
通過docker命令創(chuàng)建三個Rabbitmq nodes;
注意這里使用相同的 RABBITMQ_ERLANG_COOKIE 值
# docker run -d \
--name=rabbitmq1 \
-p 5672:5672 \
-p 15672:15672 \
-e RABBITMQ_NODENAME=rabbitmq1 \
-e RABBITMQ_ERLANG_COOKIE='YZSDHWMFSMKEMBDHSGGZ' \
-h rabbitmq1 \
--net=rabbitmqnet \
rabbitmq:3.7-rc-management
# docker run -d \
--name=rabbitmq2 \
-p 5673:5672 \
-p 15673:15672 \
-e RABBITMQ_NODENAME=rabbitmq2 \
-e RABBITMQ_ERLANG_COOKIE='YZSDHWMFSMKEMBDHSGGZ' \
-h rabbitmq2 \
--net=rabbitmqnet \
rabbitmq:3.7-rc-management
# docker run -d \
--name=rabbitmq3 \
-p 5674:5672 \
-p 15674:15672 \
-e RABBITMQ_NODENAME=rabbitmq3 \
-e RABBITMQ_ERLANG_COOKIE='YZSDHWMFSMKEMBDHSGGZ' \
-h rabbitmq3 \
--net=rabbitmqnet \
rabbitmq:3.7-rc-management
組建rabbitmq集群
登陸Rabbitmq的后兩個節(jié)點,執(zhí)行命令加入第一個Rabbitmq節(jié)點集群
### Disk Node
# docker exec rabbitmq2 bash -c \
"rabbitmqctl stop_app && \
rabbitmqctl reset && \
rabbitmqctl join_cluster rabbitmq1@rabbitmq1 && \
rabbitmqctl start_app"
### Ram Node
# docker exec rabbitmq3 bash -c \
"rabbitmqctl stop_app && \
rabbitmqctl reset && \
rabbitmqctl join_cluster --ram rabbitmq1@rabbitmq1 && \
rabbitmqctl start_app"
退出集群
# docker exec rabbitmq3 bash -c \
"rabbitmqctl stop_app && \
rabbitmqctl reset && \
rabbitmqctl start_app"
拉取haproxy鏡像
拉取haproxy鏡像
啟動haproxy
# cat haproxy-create.sh
#! /bin/bash
docker run -d \
--name rabbitmq-haproxy \
-p 1080:80 -p 5677:5677 -p 8001:8001 \
--net=rabbitmqnet \
-v /root/rabbitmq/haproxy-etc:/usr/local/etc/haproxy:ro \
haproxy:latest
haproxy的配置文件如下:
root@node0:~/rabbitmq# cat haproxy-etc/haproxy.cfg
# Simple configuration for an HTTP proxy listening on port 80 on all
# interfaces and forwarding requests to a single backend "servers" with a
# single server "server1" listening on 127.0.0.1:8000
global
daemon
maxconn 256
defaults
mode http
timeout connect 5000ms
timeout client 5000ms
timeout server 5000ms
listen rabbitmq_cluster
bind 0.0.0.0:5677
option tcplog
mode tcp
balance leastconn
server rabbit1 rabbitmq1:5672 check inter 2s rise 2 fall 3
server rabbit2 rabbitmq2:5672 check inter 2s rise 2 fall 3
server rabbit3 rabbitmq3:5672 check inter 2s rise 2 fall 3
listen http_front
bind 0.0.0.0:80
stats uri /haproxy?stats
listen rabbitmq_admin
bind 0.0.0.0:8001
server rabbit1 rabbitmq1:15672
server rabbit2 rabbitmq2:15672
server rabbit3 rabbitmq3:15672
啟動haproxy后,可以通過haproxy來訪問rabbitmq集群:http://external-ip:8001
獲取haproxy的狀態(tài):http://external-ip:1080/haproxy?stats
rabbitmq exporter部署
要收集rabbitmq的metrics給prometheus使用的話,可以使用開源的rabbitmq-exporter
參考如下:
https://github.com/kbudde/rabbitmq_exporter
https://hub.docker.com/r/kbudde/rabbitmq-exporter/
拉取鏡像
# docker pull kbudde/rabbitmq-exporter
啟動rabbitmq實例
復制代碼 代碼如下:
# docker run -d --name=rabbitmq1 -p 5672:5672 -p 15672:15672 -e RABBITMQ_NODENAME=rabbitmq1 -e RABBITMQ_ERLANG_COOKIE='YZSDHWMFSMKEMBDHSGGZ' -h rabbitmq1 --net=rabbitmqnet -p 9090:9090 rabbitmq:3.7-rc-management
開啟9090端口,這個是rabbitmq exporter的默認PUBLISH_PORT
啟動rabbitmq exporter實例
# docker run -d --net=container:rabbitmq1 kbudde/rabbitmq-exporter
獲取rabbitmq的metrics
# wget http://localhost:9090/metrics
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。