Keepalived+nginx+rabbitmq实现高可用负载均衡

  • Post author:
  • Post category:其他


环境搭建:

NGINX配置:

因为我用的是windows环境配置的docker导致无法从主机直接ping容器,所以Nginx使用的是Multipass虚拟机.

搭建好ubuntu环境,NginxMS,NginxS1

PS C:\Users\****> multipass.exe ls
Name                    State             IPv4             Image
primary                 Deleted           --               Not Available
nginxMS                 Running           172.23.249.108   Ubuntu 22.04 LTS
nginxS1                 Running           172.23.253.212   Ubuntu 22.04 LTS


nginx.conf 主机和从机

user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
        worker_connections 768;
        # multi_accept on;
}


stream {
    upstream rabbitmqserver {
        server 192.168.0.106:5672;
        server 192.168.0.106:5673;
        server 192.168.0.106:5674;
    }

    server {
        listen 5670;
        proxy_pass rabbitmqserver;
    }

}

http {

        ##
        # Basic Settings
        ##

        sendfile on;
        tcp_nopush on;
        types_hash_max_size 2048;
        # server_tokens off;

        # server_names_hash_bucket_size 64;
        # server_name_in_redirect off;

        include /etc/nginx/mime.types;
        default_type application/octet-stream;

        ##
        # SSL Settings
        ##

        ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
        ssl_prefer_server_ciphers on;

        ##
        # Logging Settings
        ##

        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;

        ##
        # Gzip Settings
        ##

        gzip on;

        # gzip_vary on;
        # gzip_proxied any;
        # gzip_comp_level 6;
        # gzip_buffers 16 8k;
        # gzip_http_version 1.1;
        # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
        upstream rabbitManage {
                server 192.168.0.106:15672;
                server 192.168.0.106:15673;
                server 192.168.0.106:15674;
        }
        server {
        listen       15675;
        server_name  172.23.253.200;
        location / {
                proxy_pass   http://rabbitManage;
                index  index.html index.htm;
        }
        }
        ##
        # Virtual Host Configs
        ##

        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
}


#mail {
#       # See sample authentication script at:
#       # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
#       # auth_http localhost/auth.php;
#       # pop3_capabilities "TOP" "USER";
#       # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
#       server {
#               listen     localhost:110;
#               protocol   pop3;
#               proxy      on;
#       }
#
#       server {
#               listen     localhost:143;
#               protocol   imap;
#               proxy      on;
#       }
#}

KEEPALIVED配置:

在上面安装好Nginx的虚拟机中安装Keepalived


KeepAlived(MASTER):

! Configuration File for keepalived

global_defs {
    router_id HA-M1
}

vrrp_script chk_nginx {
    script "/etc/keepalived/nginx_check.sh"
    interval 2
    fall 3
    rise 2
}

vrrp_instance VI_MYSQL {
    state MASTER
    interface eth0
    virtual_router_id 100
    priority 100
    advert_int 1

    authentication {
        auth_type PASS
        auth_pass 1111
    }

    virtual_ipaddress {
        172.23.253.200  dev  eth0  label  eth0:vip
    }

    track_script {
        chk_nginx
    }
}


KeepAlived(SALVE):

! Configuration File for keepalived

global_defs {
    router_id HA-M1
}

vrrp_script chk_nginx {
    script "/etc/keepalived/nginx_check.sh"
    interval 2
    fall 3
    rise 2
}

vrrp_instance VI_MYSQL {
    state BACKUP
    interface eth0
    virtual_router_id 100
    priority 99
    advert_int 1

    authentication {
        auth_type PASS
        auth_pass 1111
    }

    virtual_ipaddress {
        172.23.253.200  dev  eth0  label  eth0:vip
    }

    track_script {
        chk_nginx
    }
}


nginx_check Nginx存活检查文件

:

#!/bin/bash
A=`ps -C nginx --no-header |wc -l`
if [ $A -eq 0 ]
        then
        echo 'nginx server is died'
        sudo killall keepalived
fi

RABBITMQ集群搭建:

三台docker容器 用命令加入集群 具体命令百度吧 我用的时候我也百度

但是docker run命令一定要用link连接

docker run -d --hostname rabbitMS --name mqMS -p 5672:5672 -p 15672:15672 -e RABBITMQ_ERLANG_COOKIE="rabbitmq_cookie" rabbitmq:3.11-management
docker run -d --hostname rabbitS1 --name mqS1 --link mqMS:link01 -p 5673:5672 -p 15673:15672 -e RABBITMQ_ERLANG_COOKIE="rabbitmq_cookie" rabbitmq:3.11-management
docker run -d --hostname rabbitS2 --name mqS2 --link mqMS:link02 --link mqS1:link03 -p 5674:5672 -p 15674:15672 -e RABBITMQ_ERLANG_COOKIE="rabbitmq_cookie" rabbitmq:3.11-management
rabbitmqctl stop_app
rabbitmqctl reset
rabbitmqctl join_cluster --ram rabbit@rabbitMS
rabbitmqctl start_app

搭建好集群以后访问

刷新查看是否负载均衡


关闭主机nginx以后keepalived自动转移到从机 正常访问



版权声明:本文为qq_43096296原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。