Tối ưu hóa Cấu hình RabbitMQ Cluster với Docker, HAProxy và Keepalived

Các máy chủ cần thiết
  • node1: 192.168.0.191
  • node2: 192.168.0.110
  • node3: 192.168.0.122

Đảm bảo kết nối mạng giữa các máy chủ (ping thành công). Tắt tường lửa nếu cần:

systemctl stop firewalld

Cấu hình nguồn Docker tại /etc/docker/daemon.json:

{
  "registry-mirrors": [
    "https://docker.mirrors.ustc.edu.cn",
    "https://hub-mirror.c.163.com",
    "https://reg-mirror.qiniu.com"
  ]
}

Khởi động lại Docker:

systemctl restart docker

Thực hiện trên tất cả máy chủ với Docker Compose:

version: '3'
services:
  rabbitmq:
    container_name: rabbitmq-node
    image: rabbitmq:management
    restart: always
    ports:
      - 4369:4369
      - 5673:5672
      - 15673:15672
      - 25672:25672
    environment:
      - TZ=Asia/Ho_Chi_Minh
      - RABBITMQ_ERLANG_COOKIE=cluster_key
      - RABBITMQ_ADMIN_USER=admin
      - RABBITMQ_ADMIN_PASS=admin123
      - RABBITMQ_DEFAULT_VHOST=app_vhost
    hostname: node1
    extra_hosts:
      - node1:192.168.0.191
      - node2:192.168.0.110
      - node3:192.168.0.122
    volumes:
      - /opt/rabbitmq/data:/var/lib/rabbitmq/mnesia
      - /opt/rabbitmq/logs:/var/log/rabbitmq

Chạy lệnh:

docker-compose up -d

Trên node2 và node3, điều chỉnh hostname và extra_hosts tương ứng.

Tệp khởi tạo cluster (rabbitmq_init.sh):

rabbitmqctl stop_app
rabbitmqctl reset
rabbitmqctl join_cluster --ram rabbit@node1
rabbitmqctl start_app

Đối với HAProxy (trên node3):

version: '3'
services:
  haproxy:
    container_name: lb-cluster
    image: haproxy:2.3.10
    restart: always
    ports:
      - 5675:5675
      - 8100:8100
    volumes:
      - /opt/haproxy/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro
      - /opt/keepalived:/etc/keepalived

haporxy.cfg:

global
    log 127.0.0.1 local0
    maxconn 2096
    daemon
defaults
    mode tcp
    timeout connect 5s
    timeout client 120s
    timeout server 120s
listen stats
    bind *:8100
    mode http
    stats enable
    stats uri /
    stats auth admin:admin123
listen rabbitmq
    bind *:5675
    balance roundrobin
    server node1 node1:5672
    server node2 node2:5672
    server node3 node3:5672

Keepalived trên node1 (MASTER):

global_defs {
  router_id master_node
}
vrrp_script chk_haproxy {
  script "/etc/keepalived/haproxy_check.sh"
  interval 5
  weight 10
}
vrrp_instance VI_1 {
  state MASTER
  interface ens33
  virtual_router_id 1
  priority 100
  advert_int 1
  authentication {
    auth_type PASS
    auth_pass 123456
  }
  track_script {
    chk_haproxy
  }
  virtual_ipaddress {
    192.168.0.201
  }
}

Keepalived trên node3 (BACKUP):

global_defs {
  router_id backup_node
}
vrrp_instance VI_1 {
  state BACKUP
  priority 90
  ... (các cấu hình còn lại giống MASTER)

Trong container HAProxy, cài đặt Keepalived và khởi động dịch vụ:

apt-get update
apt-get install -y keepalived net-tools
service keepalived start

Thẻ: rabbitmq-cluster docker-compose ha-proxy keepalived-failover high-availability

Đăng vào ngày 22 tháng 5 lúc 19:10