Haproxy 搭建Web群集

1.Haproxy概述

1.1概述

HAProxy是可提供高可用性、负载均衡以及基于TCP和HTTP应用的代理,是免费、快速并且可靠的一种解决方案。HAProxy非常适用于并发大(并发达1w以上)web站点,这些站点通常又需要会话保持或七层处理。HAProxy的运行模式使得它可以很简单安全的整合至当前的架构中,同时可以保护web服务器不被暴露到网络上。

1.2主要特性

  • 可靠性和稳定性非常好,可以与硬件级的F5负载均衡设备相媲美;
  • 最高可以同时维护40000-50000个并发连接,单位时间内处理的最大请求数为20000个,最大处理能力可达10Git/s;
  • 支持多达8种负载均衡算法
  • 支持Session会话保持,Cookie的引导;
  • 支持通过获取指定的url来检测后端服务器的状态;
  • 支持虚机主机功能,从而实现web负载均衡更加灵活;
  • 支持连接拒绝、全透明代理等独特的功能;
  • 拥有强大的ACL支持,用于访问控制;
  • 支持TCP和HTTP协议的负载均衡转发;
  • 支持客户端的keepalive功能,减少客户端与haproxy的多次三次握手导致资源浪费,让多个请求在一个tcp连接中完成

1.3常见的Web集群调度器 

目前常见的Web集群调度器分为软件和硬件

软件通常使用开源的LVS、Haproxy、Nginx

  • LVS 性能最好,但是搭建相对复杂
  • Nginx 的 upstream模块支持群集功能,但是对群集节点健康检查功能不强,高并发性能没有 Haproxy好

硬件一般使用比较多的是F5、Arry,也有很多人使用国内的一些产品,如梭子鱼、绿盟等

1.4HAProxy的8种调度算法

roundrobin轮询
static-rr  加权轮询
leastconn  最小连接
source    根据源地址做哈希
uri      根据请求的URI地址做哈希
url_param  根据请求的URL路径里传递的参数做哈希
hdr(NAME)      根据请求头的字段做哈希
rdp-cookie(NAME)根据cookie里的字段做哈希

1.5HAProxy提供了3种实现会话保持的方式

(1)源地址hash    balance source

(2)设置cookie    cookie HA_STICKY_dy insert indirect nocache
                 server tomcat.inst1 192.168.80.11:8080 cookie tomcat.inst1

(3)会话粘性表stick-table    stick-table type ip size 5k expire 1m
                            stick on src

1.6HAProxy 的配置文件共有 5 个域

  • global:用于配置全局参数
  • default:用于配置所有frontend和backend的默认属性
  • frontend:用于配置前端服务(即HAProxy自身提供的服务)实例
  • backend:用于配置后端服务(即HAProxy后面接的服务)实例组
  • listen:frontend + backend的组合配置,可以理解成更简洁的配置方法,frontend域和backend域中所有的配置都可以配置在listen域下

2.常见的应用分析

2.1 LVS 应用

  • LVS在企业应用中抗负载能力很强,但存在不足。
  • LVS不支持正则处理,不能实现动静分离。
  • 对于大型网站,LVS的实施配置复杂,维护成本相对较高。

2.2 Haproxy 应用

  • Haproxy是一款可提供高可用性、负载均衡、及基于TCP和HTTP应用的代理软件。
  • 适用于负载大的web站点。
  • 运行在硬件上可支持数以万计的并发连接的连接请求。

2.3 LVS、Nginx、Haproxy的区别

(1)负载均衡转发性能:

[ 硬件负载均衡 F5 > ] LVS 最好 > HAProxy 其次 > Nginx 弱于其它两款

(2)支持的代理类型:

  • LVS是基于Linux内核实现的软负载均衡,只支持四层代理转发,且不支持正则表达式处理,不能做动静分离           
  • Nginx、HAProxy都是基于应用层实现的软负载均衡,都支持四层和七层代理转发,且也支持正则表达式处理,能做动静分离

(3)配置维护:

  • LVS 实施配置复杂,维护成本相对较高
  • Nginx、HAProxy 配置简单,维护成本较低

(4)健康检查:

  • LVS可以配合Keepalived实现高可用,以及实现TCP端口或HTTP URL方式的健康检查
  • Nginx默认只支持被动方式的TCP端口健康检查,主动健康检查需要安装第三方模块nginx_upstream_check_module后才能支持
  • HAProxy默认就支持主动的TCP端口、HTTP URL、脚本等方式的健康检查
     

3.Haproxy搭建web群集

Haproxy服务器:192.168.80.105

nginx服务器1:192.168.80.106

nginx服务器2:192.168.80.107

3.1初始化操作

systemctl stop firewalld

systemctl disable --now firewalld

setenforce 0

vim /etc/sysconfig/selinux

3.2编译安装Haproxy

上传数据包至/opt/目录下 haproxy-2.8.9.tar.gz


yum install -y pcre-devel bzip2-devel gcc gcc-c++ make

cd /opt/

tar xf haproxy-2.8.9.tar.gz

cd haproxy-2.8.9

make TARGET=linux2628 ARCH=x86_64

make install PREFIX=/usr/local/haproxy

TARGET=linux26    #内核版本

使用uname -r查看内核,如:2.6.18-371.el5,此时该参数用TARGET=linux26;kernel大于2.6.28的用TARGET=linux2628

3.3Haproxy服务器配置

useradd -M -s /sbin/nologin haproxy

mkdir -p /etc/haproxy

cd /etc/haproxy

cp quick-test.cfg /etc/haproxy/haproxy.cfg

vim haproxy.init

["${NETWORKING}" = "no" ] && exit 0

cp haproxy.init /etc/init.d/haproxy

cd /etc/init.d/

chmod +x haproxy

vim /etc/init.d/haproxy

ln -s /usr/local/haproxy/sbin/haproxy /usr/local/sbin/

haproxy -v

cd /etc/init.d/

chkconfig --add haproxy

chkconfig --list haproxy

service haproxy start

netstat -lntp | grep 8000

3.4修改Haproxy 配置

cd /etc/haproxy

vim haproxy.cfg

haproxy -c -f haproxy.cfg
log 127.0.0.1 local0 info
   log 127.0.0.1 local1 warning
   maxconn 30000
   pidfile     /var/run/haproxy.pid
   user haproxy
   group haproxy
   daemon
   spread-checks 2
 
defaults
   log        global
   mode       http
   option     httplog
   option     http-server-close
   option     forwardfor
   retries    3
   timeout    http-request 2s
   timeout    queue 3s
   timeout    connect 1s
   timeout    client 10s
   timeout    server 2s
   timeout    http-keep-alive 10s
   timeout    check 2s
 
frontend http-xy101
   bind 192.168.80.105:80
   #maxconn 20000
   #acl url_static path beg -i /static
   acl url_jsp path_end -i .jsp
 
   #use_backend_static_backend if url_static
   log 127.0.0.1 local0 info
   log 127.0.0.1 local1 warning
   maxconn 30000
   pidfile     /var/run/haproxy.pid
   user haproxy
   group haproxy
   daemon
   spread-checks 2
 
defaults
   log        global
   mode       http
   option     httplog
   option     http-server-close
   option     forwardfor
   retries    3
   timeout    http-request 2s
   timeout    queue 3s
   timeout    connect 1s
   timeout    client 10s
   timeout    server 2s
   timeout    http-keep-alive 10s
   timeout    check 2s
 
frontend http-xy101
   bind 192.168.80.105:80
   #maxconn 20000
   #acl url_static path beg -i /static
   acl url_jsp path_end -i .jsp
 
   #use_backend_static_backend if url_static
   use_backend tomcat_backend if url_jsp
   default_backend nginx_backend
 
backend nginx_backend
   balance roundrobin
   option http-keep-alive
   option httpchk GET /test.html
   server nginx.inst1 192.168.80.106:80 maxconn 10000 check inter 2000 rise 2 fall 3
   server nginx.inst2 192.168.80.107:80 maxconn 10000 check inter 2000 rise 2 fall 3
 
backend tomcat_backend
   balance roundrobin
   option http-server-close
   #cookie HA_STICKY_dy insert indirect nocache
   server tomcat.inst1 192.168.80.106:8080 cookie tomcat.inst1 maxconn 10000 check inter 2000 rise 2 fall 3
   server tomcat.inst2 192.168.80.107:8080 cookie tomcat.inst2 maxconn 10000 check inter 2000 rise 2 fall 3
 
listen stats
   bind :8000
   stats enable
   stats refresh 30s
   stats uri /stats
   stats realm HAProxy\ Stats
   stats auth admin:admin123
   # this is the address and port we'll listen to, the ones to aim the
   # load generators at
   # create a certificate and uncomment this for SSL
   # bind :8443 ssl crt my-cert.pem alpn h2,http/1.1
   # Put the server's IP address and port below
listen stats
   bind :8000
   stats enable
   stats refresh 30s
   stats uri /stats
   stats realm HAProxy\ Stats
   stats auth admin:admin123
   server s1 172.31.32.33:8000

3.5部署nginx和Tomcat服务器

安装nginx

systemctl stop firewalld

systemctl disable --now firewalld

setenforce 0

vim /etc/sysconfig/selinux

上传数据包

rpm -ivh nginx -1.26.1-2.e17.ngx.x86_64.rpm

安装tomcat

上传数据包 apache-tomcat-9.0.16.tar.gz

tar xf apache-tomcat-9.0.16.tar.gz

mv apache-tomcat-9.0.16 /usr/local/tomcat

cd /usr/local/tomcat

./bin/startup.sh

cd /usr/local/tomcat/webapps/ROOT

vim test.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8%">
<html>
<body>
<% out.println("动态界面1"); %>
</body>
</html>

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8%">
<html>
<body>
<% out.println("动态界面2"); %>
</body>
</html>

3.6网页测试 

  • 16
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值