如何使用 HAProxy 在 Redhat 上为(Web + App)设置负载平衡

如何使用 HAProxy 在 Redhat 上为(Web + App)设置负载平衡

我正在尝试在 Redhat Enterprise 7.7 上设置 HAproxy 负载均衡器和粘性会话。在使用 Apache 的双节点 Web 服务器中安装 HAproxy 作为“Web-Load-balance”,并使用 java+jboss 的双节点应用服务器作为“App-Load-Balance”。

笔记:Web 服务器(双节点)已在同一台服务器上安装 Apache 和 HAProxy 笔记:应用服务器(双节点)在同一台服务器上安装 Java+Jboss 和 HAProxy

下面我将与大家分享供应商得到的 4 个测试用例,结果如下:

案例 1:- 测试状态 (OK) 没有问题

  1. Web1 = 开启
  2. Web2 = 开启
  3. App1 = 开启
  4. App2 = 关闭

案例 2:测试状态(OK)没有问题

  1. Web1 = 开启
  2. Web2 = 开启
  3. App1 = 关闭
  4. App2 = 开启

案例 3:- 测试状态“App-Load-Balance”服务器无法维护会话 ID(JBOSS)

  1. Web1 = 开启
  2. Web2 = 开启
  3. App1 = 开启
  4. App2 = 开启

案例 4:- 测试状态“App-Load-Balance”服务器无法维护会话 ID(JBOSS)

  1. Web1 = 开启
  2. Web2 = 关闭
  3. App1 = 开启
  4. App2 = 开启

我将分享两者的 HAproxy 配置器Web 负载均衡器应用程序负载均衡器如果我将其编辑完整并从头开始重新输入代码配置,我不介意。

两个节点Web服务器的HAproxy代码如下:

#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global

    log         127.0.0.1 local2
    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    maxconn     4000
    user        haproxy
    group       haproxy
    daemon
    stats socket /var/lib/haproxy/stats

#---------------------------------------------------------------------
defaults
    mode                    http
    log                     global
   # option                  httplog
    option                  dontlognull
    #option http-server-close
    #option forwardfor       except 127.0.0.0/8
    option                  redispatch
    retries                 3
    timeout http-request    10s
    timeout queue           1m
    timeout connect         10s
    timeout client          1m
    timeout server          1m
    timeout http-keep-alive 10s
    timeout check           10s
    maxconn                 3000

#------------------- Haproxy LB  -----------------------
frontend ft_http
        bind *:880
        mode http
        default_backend bk_http
frontend ft_https
        bind *:8443
        mode tcp
        default_backend bk_https


backend bk_http
        mode http
        balance roundrobin
        server Web1_SRV 10.10.10.121:80 check 
        server Web2_SRV 10.10.10.122:80 check 
backend bk_https
        mode tcp
        balance roundrobin

        server Web1_SRV 10.10.10.121:443 check
        server Web2_SRV 10.10.10.122:443 check

peers Mypeer

        peer LB1 10.10.10.121:1024 
        peer LB2 10.10.10.122:1024 
#------------------ Haproxy LB --------------------------

#------------------- HAproxy Status Page -----------------

listen stats
bind *:1978
stats enable
stats hide-version
stats realm Haproxy\ Statistics
stats uri /showus
stats auth admin:admin

Keepalived 作为 web1 的代码:

#---------------------------------------------------------
# Start Keepalived1 - 
#----------------------------------------------------------
vrrp_script chk_haproxy {
                script "pidof haproxy"
                interval 2
}
vrrp_instance VI_1 {
                interface eth0
                state MASTER
                virtual_router_id 40
                priority 150
                vrrp_unicast_bind 10.10.10.121 #Internal IP of this node
                vrrp_unicast_peer 10.10.10.122 #Internal IP of Backup node
                virtual_ipaddress {
                                10.10.10.120
                }
                track_script {
                                chk_haproxy
                }
}
#----------------------------------------------------------
#  End Keepalived1  - WEB1
#---------------------------------------------------------

Keepalived 作为 web2 的代码:

#----------------------------------------------------------
# Start Keepalived2
#----------------------------------------------------------
vrrp_script chk_haproxy {
                script "pidof haproxy"
                interval 2
}
vrrp_instance VI_1 {
                interface eth0
                state MASTER
                virtual_router_id 40
                priority 100
                vrrp_unicast_bind 10.10.10.122 #Internal IP of this node
                vrrp_unicast_peer 10.10.10.121 #Internal IP of Backup node
                virtual_ipaddress {
                                10.10.10.120
                }
                track_script {
                                chk_haproxy
                }
}
#----------------------------------------------------------
#  End Keepalived2  -WEB2
#---------------------------------------------------------

双节点APP服务器的HAproxy代码如下:

#---------------------------------------------------------------------
global
    log         127.0.0.1 local2
    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    maxconn     4000
    user        haproxy
    group       haproxy
    daemon
    stats socket /var/lib/haproxy/stats
#-------------------------------------------------------------
defaults
    mode                    tcp
    log                     global
    option                  tcplog
    option                  dontlognull
    #option http-server-close
    #option forwardfor       except 127.0.0.0/8
    option                  redispatch
    retries                 3
    #timeout http-request    10s
    timeout queue           1m
    timeout connect         10s
    timeout client          1m
    timeout server          1m
    #timeout http-keep-alive 10s
    timeout check           10s
    maxconn                 3000

#--------------------   Start ----------------------------------
frontend haproxy_inbound
        bind *:8888
        unique-id-format %{+X}o\ %ci:%cp
        unique-id-header X-Unique-ID
        option tcp-smart-accept
        option splice-request
        option splice-response
        default_backend LB
backend LB
        option tcp-smart-connect
        option splice-request
        option splice-response
        balance hdr(X-Unique-ID)
        server APP1_SRV 10.10.24.111:8109 maxconn 10 weight 1 check
        server APP2_SRV 10.10.24.112:8109 maxconn 10 weight 1 check

peers Mypeer
                peer LB1 10.10.24.111:1024 
                peer LB2 10.10.24.112:1024 

#----------- Start HAPRoxy Status page -----------------------
listen stats
bind *:1978
mode http
stats enable
stats hide-version
stats realm Haproxy\ Statistics
stats uri /showus
stats auth admin:admin
#---------- End HAPRoxy Status page ----------------

APP1 的 Keepalived 代码为:

#----------------------------------------------------------
# Start Keepalived1 -
#----------------------------------------------------------
vrrp_script chk_haproxy {
                script "pidof haproxy"
                interval 2
}
vrrp_instance VI_1 {
                interface eth0
                state MASTER
                virtual_router_id 51
                priority 150
                vrrp_unicast_bind 10.10.24.111 #Internal IP of this node
                vrrp_unicast_peer 10.10.24.112 #Internal IP of Backup node
                virtual_ipaddress {
                                10.10.24.110
                }
                track_script {
                                chk_haproxy
                }
}
#----------------------------------------------------------
#  End Keepalived1  - APP1
#---------------------------------------------------------

APP2 的 Keepalived 代码为:

#----------------------------------------------------------
# Start Keepalived2 - 
#----------------------------------------------------------
vrrp_script chk_haproxy {
                script "pidof haproxy"
                interval 2
}
vrrp_instance VI_1 {
                interface eth0
                state MASTER
                virtual_router_id 51
                priority 100
                vrrp_unicast_bind 10.10.24.112 #Internal IP of this node
                vrrp_unicast_peer 10.10.24.111 #Internal IP of Backup node
                virtual_ipaddress {
                                10.10.24.110
                }
                track_script {
                                chk_haproxy
                }
}
#----------------------------------------------------------
#  End Keepalived2  - APP2
#----------------------------------------------------------

**

HAproxy 的目标是为我们提供负载平衡和粘性会话。

**

谢谢。

相关内容