负载均衡调度器Haproxy

本文介绍了Haproxy作为Web集群调度器的使用,对比了LVS的局限性,强调了Haproxy在高可用和负载均衡方面的优势。详细阐述了Haproxy的RR、LC和SH三种调度算法,并提供了Haproxy的部署步骤。同时提到了Nginx作为Web服务器的部署,展示了如何搭建和配置多台Nginx服务器。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Web集群调度器

目前常见的Web集群调度器为软件和硬件,软件通常使用开源的LVS、Haproxy、Nginx,硬件一般使用比较多的是F5,也有很多人使用国内的一些铲平,比如绿盟等。

Haproxy

LVS在企业中抗负载能力很强,但是存在不足,它不支持正则处理,不能实现动静分离并且对于大型网站而言,LVS的实施配置很复杂,维护成本比较高,但是Haproxy可提供高可用、负载均衡以及基于TCP和HTTP应用的代理软件,适用于负载打的Web站点,运行在硬件上可支持大量的连接请求。

Haproxy调度算法

  • RR(Round Robin):轮询调度,轮询分配各节点用户访问,可以实现负载均衡
  • LC(Least Connections):最小连接数算法,更具后端的节点连接数大小动态分配前端请求
  • SH(Source Hashing):基于访问调度算法,用于一些在服务器端由Session会话记录时,可以基于来源的ip、Cookie等做集群调度,可以实现会话保持,但当IP访问量非常大时会引起负载不均衡,部分节点访问量大,影响业务

Haproxy部署

  • Haproxy
[root@haproxy opt]# yum install -y pcre-devel bzip2-devel gcc gcc-c++ make   //安装编译环境
[root@haproxy opt]# cd haproxy-1.5.19/
[root@haproxy haproxy-1.5.19]# make TARGET=linux2628 ARCH=x86_64    
[root@haproxy haproxy-1.5.19]# make install
install -d "/usr/local/sbin"
install haproxy  "/usr/local/sbin"
install -d "/usr/local/share/man"/man1
install -m 644 doc/haproxy.1 "/usr/local/share/man"/man1
install -d "/usr/local/doc/haproxy"
for x in configuration architecture haproxy-en haproxy-fr; do \
	install -m 644 doc/$x.txt "/usr/local/doc/haproxy" ; \
done
[root@haproxy haproxy-1.5.19]# mkdir /etc/haproxy
[root@haproxy haproxy-1.5.19]# cp examples/haproxy.cfg /etc/haproxy/
[root@haproxy haproxy-1.5.19]# cd /etc/haproxy/
[root@haproxy haproxy]# vim haproxy.cfg
# this config needs haproxy-1.1.28 or haproxy-1.2.1

global
        log 127.0.0.1   local0 info
        log 127.0.0.1   local1 notice
        #log loghost    local0 info
        maxconn 4096                              //最大连接数
        #chroot /usr/share/haproxy
        uid 99                 //用户UID
        gid 99                 //用户GID
        daemon                 //守护进程模式
        #debug
        #quiet

defaults
        log     global        //定义日志为global配置中的日志
        mode    http            //模式为http
        option  httplog              //采用http日志格式记录
        option  dontlognull               //不记录健康检查日志信息
        retries 3                    //检查节点服务器失败次数,连续失败了三次,则认为不可用
        redispatch             //服务器负载很高时,自动结束当前队列处理比较久的连接
        maxconn 2000             //最大连接数
        contimeout      5000          //连接超时时间
        clitimeout      50000            //客户端超时时间
        srvtimeout      50000             //服务器超时时间

listen  webcluster 0.0.0.0:80
		option httpchk GET /test.html                //检查服务器html文件 
        balance roundrobin             //负载均衡调度算法使用轮询
        server  inst1 192.168.20.22:80 cookie app1inst1 check inter 2000 fall 3   //定义节点
        server  inst1 192.168.20.33:80 cookie app1inst2 check inter 2000 fall 3

[root@haproxy haproxy]# cp /opt/haproxy-1.5.19/examples/haproxy.init /etc/init.d/haproxy
[root@haproxy haproxy]# cd /etc/init.d/
[root@haproxy init.d]# chmod +x haproxy 
[root@haproxy init.d]# chkconfig --add /etc/init.d/haproxy
[root@haproxy init.d]# ln -s /usr/local/sbin/haproxy /usr/sbin/haproxy
[root@haproxy ~]# service haproxy start      //启动
Starting haproxy (via systemctl):                          [  确定  ]

  • Nginx1
[root@Nginx-1 opt]# yum install -y pcre-devel zlib-devel gcc gcc-c++ make 
[root@Nginx-1 opt]# useradd -M -s /sbin/nologin nginx
[root@Nginx-1 opt]# tar zxvf nginx-1.12.2.tar.gz -C /opt/
[root@Nginx-1 opt]# cd nginx-1.12.2/
[root@Nginx-1 nginx-1.12.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx
[root@Nginx-1 nginx-1.12.2]# make && make install
[root@Nginx-1 nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@Nginx-1 nginx-1.12.2]# nginx
[root@Nginx-1 nginx-1.12.2]# echo "this is tom web" > /usr/local/nginx/html/test.html
  • Nginx2
[root@Nginx-2 opt]# yum install -y pcre-devel zlib-devel gcc gcc-c++ make 
[root@Nginx-2 opt]# useradd -M -s /sbin/nologin nginx
[root@Nginx-2 opt]# tar zxvf nginx-1.12.2.tar.gz -C /opt/
[root@Nginx-2 opt]# cd nginx-1.12.2/
[root@Nginx-2 nginx-1.12.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx
[root@Nginx-2 nginx-1.12.2]# make && make install
[root@Nginx-2 nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@Nginx-2 nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@Nginx-2 nginx-1.12.2]# nginx
[root@Nginx-2 nginx-1.12.2]# echo "this is jerry web" > /usr/local/nginx/html/test.html

在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值