Home Backend Development PHP Tutorial Understand the use of RabbitMQ+haProxy+keepalived

Understand the use of RabbitMQ+haProxy+keepalived

Sep 01, 2020 pm 05:49 PM
haproxy keepalived rabbitmq

Understand the use of RabbitMQ+haProxy+keepalived

前言

如有错误,随时斧正,非常感谢!

为什么要使用haProxy+keepalived呢?

为了AMQP服务的稳定性

首先先说下为什么要使用haProxy。

我在两台服务器上建了AMQP集群,分别是10.71.13.24和10.71.13.25,以后简称为24、25服务器。假设AMQP客户端直接连接24或25。如果24|25服务器宕机了,那么连接24|25的AMQP客户端就无法工作,消费者也无法进行正常消费(以下以24举例代表24|25单机的情况)。再者单机连接可能造成单机负载过高,而其他服务器空闲的状态。此时则可以通过haProxy实现负载均衡。

在本地的配置如:

global
    #log 127.0.0.1 local0 #[日志输出配置,所有日志都记录在本机,通过local0输出]
    log 127.0.0.1 local1 notice #定义haproxy 日志级别[error warringinfo debug]      
    daemon #以后台形式运行harpoxy
    nbproc 2 #设置进程数量
    maxconn 4096 #默认最大连接数,需考虑ulimit-n限制
    #user haproxy #运行haproxy的用户
    pidfile /var/run/haproxy.pid #haproxy 进程PID文件
    #group www #运行haproxy的用户所在的组
    #ulimit-n 65535 #ulimit 的数量限制
    #chroot /usr/share/haproxy #chroot运行路径
    #debug #haproxy 调试级别,建议只在开启单进程的时候调试
    #quiet

########默认配置############
defaults
    log global
    log 127.0.0.1 local0 info
    mode http #默认的模式mode { tcp|http|health },tcp是4层,http是7层,health只会返回OK
    option httplog #日志类别,采用httplog
    option dontlognull #不记录健康检查日志信息
    retries 3 #两次连接失败就认为是服务器不可用,也可以通过后面设置
    option forwardfor #如果后端服务器需要获得客户端真实ip需要配置的参数,可以从Http Header中获得客户端ip
    option httpclose #每次请求完毕后主动关闭http通道,haproxy不支持keep-alive,只能模拟这种模式的实现
    option redispatch #当serverId对应的服务器挂掉后,强制定向到其他健康的服务器,以后将不支持
    option abortonclose #当服务器负载很高的时候,自动结束掉当前队列处理比较久的链接
    maxconn 4096 #默认的最大连接数
    #timeout http-keep-alive10s #默认持久连接超时时间
    #timeout http-request 10s #默认http请求超时时间
    #timeout queue 1m #默认队列超时时间
    balance roundrobin #设置默认负载均衡方式,轮询方式
    #balance source #设置默认负载均衡方式,类似于nginx的ip_hash
    #balnace leastconn #设置默认负载均衡方式,最小连接数
    timeout connect 5s #连接超时
    timeout client 120s # 客户端超时
    timeout server 120s #服务端超时
    timeout check 2000 #心跳检测超时ms
#绑定配置
listen rabbitmq_cluster
    bind 0.0.0.0:5678
    mode http #配置TCP模式
    balance roundrobin
    #RabbitMQ集群节点配置
    server rmq_node_13_24 10.71.13.24:5672 check inter 5000 rise 2 fall 3 weight 1
    server rmq_node_13_25 10.71.13.25:5672 check inter 5000 rise 2 fall 3 weight 1
    # server <name> <ip>:<port> check inter <value> rise <value> fall <value> weight <value>
    # check inter <value>:每隔5000ms检测AMQP服务是否可用
    # rise <value>:检测到2次可用则可被确认再次可用。
    # fall <value>:检测到3次无法连接则认为服务不可用
    # weight <value>:权重
Copy after login

配置完haProxy.cfg之后,启动haProxy:sudo /etc/init.d/haproxy start。(haproxy启动脚本见最后)
配置解析:之前连接的socket是10.71.13.24:5672,现在连接的socket是10.71.13.24:5678。在25也可以按24的配置COPY一份,因此无论连接的是哪个服务器的5678端口最终都能实现负载均衡。然后还是有一个问题没有解决,如果某个服务器宕机了怎么办?这就是为什么要引入keepalived了。

再谈keepalived的使用

关于keepalived的原理我不熟,也就不过多的去解读。目前我只是想要解决我碰到的问题。为了解决AMQP最终能够稳定服务,于是申请了一个虚拟IP-VIP:10.71.13.254。通过这个VIP则可以开始配置keepalived.conf了。

! Configuration File for keepalived

global_defs {
   router_id Node_Master  # 路由ID,主备的不能相同}vrrp_script chk_haproxy {
    script "/etc/keepalived/check_haproxy.sh"
    interval 5
    weight 2}vrrp_instance VI_1 {
    state MASTER  #keepalived的角色。Master表示主服务器,从服务器设置为BACKUP
    interface eth1 #指定检测网卡,配置成eth0之后我无法连接到AMQP服务
    virtual_router_id 1
    priority 100 #优先级,BACKUP机器上的优先级至少小于50
    advert_int 1 #设置主备之间的检测时间,单位为s
    authentication {
        auth_type PASS
        auth_pass root123    }

    track_script {
        chk_haproxy    }

    virtual_ipaddress { #VIP地址,可以设置多个        10.71.13.254
    }}virtual_server 10.71.13.254 5672 { # 设置虚拟服务器
    delay_loop 6 #设置运行情况检查时间,单位s
    lb_algo wrr #设置负载调度算法,共有rr、wrr、lc、wlc、lblc、lblcr、dh、sh 这8种
    lb_kind DR #设置LVS实现的负载均衡机制方式 VS/DR
    persistence_timeout 50 #指定在一定时间内来自同一IP的连接将会被转发到同一RealServer中
    protocol TCP

    # 这个real_server 即LVS的三大部分之一的RealServer,这里特指RabbitMQ服务
    real_server 10.71.13.24 5678 { #配置服务节点
        weight 1  #配置权重        TCP_CHECK {
            nb_get retry 3
            connect_timeout 3
            delay_before_retry 3
            connect_port 5672
        }
    }

    real_server 10.71.13.25 5678 {
        weight 1
        TCP_CHECK {
            nb_get retry 3
            connect_timeout 3
            delay_before_retry 3
            connect_port 5672
        }
    }
  }virtual_server 10.71.13.254 15672 {
    delay_loop 6
    lb_algo wrr
    lb_kind DR
    persistence_timeout 50
    protocol TCP
    real_server 10.71.13.24 15672 {
        weight 1
        TCP_CHECK {
            nb_get retry 3
            connect_timeout 3
            delay_before_retry 3
            connect_port 15672
        }
    }
    real_server 10.71.13.25 15672 {
        weight 1
        TCP_CHECK {
            nb_get retry 3
            connect_timeout 3
            delay_before_retry 3
            connect_port 15672
        }
    }}
Copy after login

check_haproxy.sh脚本的目的是为了防止haProxy服务已经不可用而keepalived可用的情况,该脚本会尝试重新启动haProxy,如果启动不成功则关闭keepalived。这样keepalived的VIP就会漂移到BACKUP备份机,从而继续保证服务。脚本的配置如下:

#! /bin/bashif [ $(ps -C haproxy --no-header | wc -l) -eq 0 ]; then
    sudo service haproxy restartfisleep 2if [ $(ps -C haproxy --no-header | wc -l) -eq 0 ]; then
    sudo service keepalived stopfi
Copy after login

在25服务器上的配置和24的服务器基本一致,注意修改属性:route_id、state、priority。
然后启动keepalived:sudo /etc/init.d/keepalived start。(启动脚本见最后)

启动完keepalived之后,我们再次连接AMQP服务就变成了:10.71.13.254:5762,因为master是24服务器,所以实际真实响应的服务是10.71.13.24:567810.71.13.25:5678。因为采用的是加权轮询且权重都是1,24和25会轮流响应VIP的请求。在haProxy中监听了5678端口,并且采用了轮询机制分发到24和25的5672端口。会发现keepalived的和haProxy都使用了轮询机制,能不能把其中的某一处去掉呢?增加了VIP之后如果master宕机之后,就会选举一个从机成为主机,接管原主机的服务。当人工修复好了原主机之后,从机会把服务重新还给主机去接管,自己还原成原来的状态。

haProxy和keepalived的启动脚本

haProxy的启动脚本

#! /bin/shset -ePATH=/sbin:/bin:/usr/sbin:/usr/binPROGDIR=/opt/haproxy-1.7.11PROGNAME=haproxyDAEMON=$PROGDIR/$PROGNAMECONFIG=$PROGDIR/conf/$PROGNAME.cfg
# PIDFILE=$PROGDIR/conf/$PROGNAME.pidPIDFILE=/var/run/haproxy.pidDESC="HAProxy daemon"SCRIPTNAME=/etc/init.d/$PROGNAME# Gracefully exit if the package has been removed.test -x $DAEMON || exit 0start(){
       echo -e "Starting $DESC: $PROGNAME\n"
       $DAEMON -f $CONFIG
       echo "."}stop(){
       echo -e "Stopping $DESC: $PROGNAME\n"
       haproxy_pid="$(cat $PIDFILE)"
       kill $haproxy_pid
       echo "."}restart(){
       echo -e "Restarting $DESC: $PROGNAME\n"
       $DAEMON -f $CONFIG -p $PIDFILE -sf $(cat $PIDFILE)
       echo "."}case "$1" in
 start)
       start       ;;
 stop)
       stop       ;;
 restart)
       restart       ;;
 *)
       echo "Usage: $SCRIPTNAME {start|stop|restart}" >&2
       exit 1
       ;;esac

exit 0
Copy after login

keepalived启动脚本:

#!/bin/sh
#
# Startup script for the Keepalived daemon
#
# processname: keepalived
# pidfile: /var/run/keepalived.pid
# config: /etc/keepalived/keepalived.conf
# chkconfig: - 21 79# description: Start and stop Keepalived

# Source function library. /etc/rc.d/init.d/functions

# Source configuration file (we set KEEPALIVED_OPTIONS there). /etc/sysconfig/keepalivedRETVAL=0prog="keepalived"start() {
    echo -n $"Starting $prog: "
    daemon keepalived ${KEEPALIVED_OPTIONS}
    RETVAL=$?
    echo    [ $RETVAL -eq 0 ] && touch /var/lock/subsys/$prog}stop() {
    echo -n $"Stopping $prog: "
    killproc keepalived    RETVAL=$?
    echo    [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$prog}reload() {
    echo -n $"Reloading $prog: "
    killproc keepalived -1
    RETVAL=$?
    echo}# See how we were called.case "$1" in
    start)
        start        ;;
    stop)
        stop        ;;
    reload)
        reload        ;;
    restart)
        stop
        start        ;;
    condrestart)
        if [ -f /var/lock/subsys/$prog ]; then
            stop
            start
        fi        ;;
    status)
        status keepalived        RETVAL=$?
        ;;
    *)
        echo "Usage: $0 {start|stop|reload|restart|condrestart|status}"
        RETVAL=1esac

exit $RETVAL
Copy after login

相关学习推荐:php编程(视频)

The above is the detailed content of Understand the use of RabbitMQ+haProxy+keepalived. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to build a reliable messaging app with React and RabbitMQ How to build a reliable messaging app with React and RabbitMQ Sep 28, 2023 pm 08:24 PM

How to build a reliable messaging application with React and RabbitMQ Introduction: Modern applications need to support reliable messaging to achieve features such as real-time updates and data synchronization. React is a popular JavaScript library for building user interfaces, while RabbitMQ is a reliable messaging middleware. This article will introduce how to combine React and RabbitMQ to build a reliable messaging application, and provide specific code examples. RabbitMQ overview:

How to use RabbitMQ to implement distributed message processing in PHP How to use RabbitMQ to implement distributed message processing in PHP Jul 18, 2023 am 11:00 AM

How to use RabbitMQ to implement distributed message processing in PHP Introduction: In large-scale application development, distributed systems have become a common requirement. Distributed message processing is a pattern that improves the efficiency and reliability of the system by distributing tasks to multiple processing nodes. RabbitMQ is an open source, reliable message queuing system that uses the AMQP protocol to implement message delivery and processing. In this article we will cover how to use RabbitMQ in PHP for distribution

How to implement SSL passthrough in HAProxy How to implement SSL passthrough in HAProxy Mar 20, 2024 am 09:30 AM

Keeping web servers load balanced is one of the key measures to prevent downtime. Using a load balancer is a reliable approach, with HAProxy being a highly regarded choice. Using HAProxy, you can accurately configure the load balancing method and support SSL passthrough to ensure the security of communication between the client and the server. It starts by exploring the importance of implementing SSL passthrough in HAProxy, followed by a detailed discussion of the steps required to implement this feature and an example for better understanding. What is SSL passthrough? Why is it important? As a load balancer, HAProxy accepts and distributes the load flowing to your web servers across configured servers. Load distribution is targeted to client devices and

How SpringBoot integrates RabbitMQ to implement delay queue How SpringBoot integrates RabbitMQ to implement delay queue May 16, 2023 pm 08:31 PM

How to ensure that messages are not lost. The rabbitmq message delivery path producer->switch->queue->consumer is generally divided into three stages. 1. The producer ensures the reliability of message delivery. 2.MQ internal messages are not lost. 3. Consumer consumption is successful. What is message delivery reliability? Simply put, it means that messages are 100% sent to the message queue. We can turn on confirmCallback. After the producer delivers the message, mq will give the producer an ack. Based on the ack, the producer can confirm whether the message is sent to mq. Turn on confirmCallback and modify the configuration file #NONE: disable the release confirmation mode, which is the default value , CORRELATED:

Using RabbitMQ in Go: A Complete Guide Using RabbitMQ in Go: A Complete Guide Jun 19, 2023 am 08:10 AM

As modern applications increase in complexity, messaging has become a powerful tool. In this area, RabbitMQ has become a very popular message broker that can be used to deliver messages between different applications. In this article, we will explore how to use RabbitMQ in Go language. This guide will cover the following: Introduction to RabbitMQ RabbitMQ Installation RabbitMQ Basic Concepts Getting Started with RabbitMQ in Go RabbitMQ and Go

How to configure HAProxy for WebSocket connections How to configure HAProxy for WebSocket connections Mar 20, 2024 pm 03:51 PM

WebSocket applications enable real-time two-way communication between client and server. Even though WebSocket connections are used, effective traffic management mechanisms are still required to avoid overloading the server, resulting in service interruption and unavailability. HAProxy is a free and reliable load balancer that also works as a reverse proxy. By configuring HAProxy to support WebSocket connections, you can better utilize the real-time data transmission characteristics of WebSockets and reduce the server load. This article will describe the detailed steps to configure HAProxy to support WebSocket connections. Step-by-step guide on how to configure HAProxy for WebSocket connections via WebSoc

How to analyze Haproxy port reuse How to analyze Haproxy port reuse May 29, 2023 am 09:25 AM

Author of this article: Spark (Ms08067 intranet security team member) 1. Overview Haproxy is a high-performance load balancing proxy software developed in C language. It provides tcp and http application proxies. It is free, fast and reliable. Similar to frp, it can be run using a configuration file + a server. Advantages: Large-scale business field applications widely support four-layer proxies (transport layer) and seven-layer proxies (application layer). Support ACL (access control list), and can flexibly configure routing windows. It can be run after compiling with cygwin (can be cross-platform) access control list (AccessControlLists, ACL) is a list of commands applied to the router interface. These command lists

Solution for real-time data synchronization between Golang and RabbitMQ Solution for real-time data synchronization between Golang and RabbitMQ Sep 27, 2023 pm 10:41 PM

Introduction to the solution for real-time data synchronization between Golang and RabbitMQ: In today's era, with the popularity of the Internet and the explosive growth of data volume, real-time data synchronization has become more and more important. In order to solve the problems of asynchronous data transmission and data synchronization, many companies have begun to use message queues to achieve real-time synchronization of data. This article will introduce a real-time data synchronization solution based on Golang and RabbitMQ, and provide specific code examples. 1. What is RabbitMQ? Rabbi

See all articles