2021-07-27haproxy的集群搭建

理论

一,haproxy的基础
web集群调度分为软件和精简,之前lvs,hopoxy,nginx等,硬件是F5,等
lvs的不足之处
lvs不支持正则表达,不能实现动静分离,对大型网站,lvs配置复制,维护成本高
haproxy是可以提供高性能,负载均衡,以及tcp和http应用的代理软件
使用与负载特别大的we站点,运行在当前的硬件上课支持数以万计的并发连接请求

haproxy的调度算法

Haproxy支持多种调度算法,最常用的有三种:RR(Round Robin),LC(Least Connections),SH(Source Hashing)
1,RR
rr算法是最简单,最常用的算法,轮询调度,几个站点轮流分配
2,LC
LC算法是最小连接数算法,根据后端节点连接数动态分配请求
3,SH
SH基于来源范文调度算法,用于一些有session会话记录在服务器的场景,基于来源的ip。cookie等做集群调度

haproxy搭建

1,环境
需要两天nginx服务器
一台 haproxy服务器
三台服务器在同一个网段

[root@manager ~]# systemctl stop firewalld
[root@manager ~]# setenforce 0
[root@manager ~]# ntpdate ntp.aliyun.com
27 Jul 14:28:47 ntpdate[34361]: adjust time server 203.107.6.88 offset 0.001978 sec

2,haproxy的安装和启动

第一步,解压tar包`

[root@localhost ~]# cd /opt
[root@localhost opt]# rz -E
rz waiting to receive.
[root@localhost opt]# tar zxvf haproxy-1.5.19.tar.gz 

第二步,安装环境

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

第三步,
cd haproxy-1.5.19/
make TARGET=linux2628 ARCH=x86_64
make install
2021-07-27haproxy的集群搭建

第四步,配置文件

[root@localhost haproxy-1.5.19]# mkdir /etc/haproxy
[root@localhost haproxy-1.5.19]# ls
CHANGELOG  CONTRIBUTING  ebtree    haproxy                  include  Makefile  ROADMAP  SUBVERS  VERDATE
contrib    doc           examples  haproxy-systemd-wrapper  LICENSE  README    src      tests    VERSION
[root@localhost haproxy-1.5.19]# cp examples/haproxy.cfg /etc/haproxy/  配置文件
[root@localhost haproxy-1.5.19]# cd /etc/haproxy
[root@localhost haproxy]# ls
haproxy.cfg
[root@localhost haproxy]# vim haproxy.cfg 

2021-07-27haproxy的集群搭建
#chroot /usr/share/haproxy
uid 99 #用户UID
gid 99 #用户GID
daemon #守护进程模式

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

[root@localhost haproxy]# cp /opt/haproxy-1.5.19/examples/haproxy.init /etc/init.d/haproxy
[root@localhost haproxy]# cd /etc/init.d/
[root@localhost init.d]# ls
functions  haproxy  mysqld  netconsole  network  README
[root@localhost init.d]# chmod +x haproxy
[root@localhost init.d]# ls
functions  haproxy  mysqld  netconsole  network  README
[root@localhost init.d]# chkconfig --add /etc/init.d/haproxy
[root@localhost init.d]# ln -s /usr/local/sbin/haproxy /usr/sbin/haproxy
[root@localhost init.d]# service haproxy start

2021-07-27haproxy的集群搭建
三,在另外两天服务器上安装nginx

详情请见之前的nginx安装

2021-07-27haproxy的集群搭建

2021-07-27haproxy的集群搭建
2021-07-27haproxy的集群搭建

总结:

1,haproxy是一个可以通过搞可用性,搞负载均衡,以及基于tcp和http的代理的软件,主要有三种调度方式,rr,lc ,sh。haproxy是工作在七层上的,在负载均衡和出来并发的速度上由于nginx,能够补充nginx的一些缺点
2,lvs和haproxy的区别,lvs在企业应用中抗负载能力强,但是存在不足,lvs不支持正则表达,不能动静分离,复杂,维护成本高,而,haproxy是一块可提高可用性,负载均衡以及基于tcp和http的应用代理软件,支持高并发,基于端口的检查。
3,在配置安装中,make中指定诶和版本和系统的位数,其次在主配置文件配置日志记录和listen的内容,指定算法策略,和分配的服务器ip以及端口号。

上一篇:LVS负载均衡之DR模式部署


下一篇:【面试题】2021年PHP高频面试题汇总