Haproxy搭建Web集群

常见负载均衡调度器

分硬件和软件
硬件F5裸金属用得较多,梭子鱼,绿盟。
软件lvs.haproxy,nginx
Haproxy是一款高可用,负载均衡,基于TCP和HTTP应用的代理软件
适用于负载大的web站点
运行在硬件上可支持数以万计的并发连接的连接请求

Haproxy调度算法

最常用的有三种,还有很多

  1. RR轮询
  2. LC最小连接数算法,根据后端i二点连接数大小动态分配前端请求。目前用的比较多
  3. SH基于来源访问调度算法,用于一些有Session会话记录在夫区其端的场景,可以基于来源IP,Cookie等做集群调度。

Haproxy的优点

  1. 在负载均衡速度和并发处理上由于nginx。
  2. Haproxy支持虚拟主机,可以工作在4.7层。
  3. 能够补充nginx不足的缺点,例如:session保持,cookie的引导。
  4. 支持以uurl的方式检测后端服务器的状态。
  5. 还可以对mysql进行负载均衡,对后端DB节点进行检测和负载均衡。

实验环境

// 
root@haproxy ~    192.168.142.142
root@nginx_01 ~   192.168.142.143
root@nginx_02 ~   192.168.142.144

三台都做
在这里插入图片描述

Haproxy搭建

// 
[root@haproxy opt]# yum install -y pcre-devel bzip2-devel gcc gcc-c++ make  ## 安装环境哦工具
[root@haproxy ~]# cd /opt
[root@haproxy opt]# rz -E
rz waiting to receive.
[root@haproxy opt]# tar zxvf haproxy-1.5.19.tar.gz
##解压
[root@haproxy haproxy-1.5.19]# uname -r
3.10.0-957.el7.x86_64
#使用uname -r 查看内核,如2.6.18-371.e15,此时该参数用TARGET=linux26;kernel大于2.6.28的用TARGET=linux2628
ARCH=x86_64
[root@haproxy haproxy-1.5.19]# make TARGET=linux2628 ARCH=x86_64
[root@haproxy haproxy-1.5.19]# make install

Haproxy服务器配置

// 
[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

配置日志记录,local0为日志设备,默认存放到系统日志
在这里插入图片描述
在这里插入图片描述

开启haproxy

// 
[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]# ls
functions  haproxy  netconsole  network  README
[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 init.d]# systemctl start haproxy#开启
[root@haproxy init.d]#  netstat -antp | grep haproxy
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      67070/haproxy

nginx搭建

// 
[root@nginx_01 ~]# yum install -y pcre-devel zlib-devel gcc gcc-c++ make
root@nginx_01 ~]# cd /opt
[root@nginx_01 opt]# rz -E
rz waiting to receive.
[root@nginx_01 opt]# tar zxvf nginx-1.12.2.tar.gz -C /opt/
[root@nginx_01 nginx-1.12.2]# ls
auto     CHANGES.ru  configure  html     man     src
CHANGES  conf        contrib    LICENSE  README
[root@nginx_01 nginx-1.12.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx  --with-http_stub_status_module
[root@nginx_01 nginx-1.12.2]# make && make install
[root@nginx_01 nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@nginx_01 nginx-1.12.2]# nginx
[root@nginx_01 nginx-1.12.2]# netstat -anpt | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      28691/nginx: master 

以上两台nginx都是一样的操作,不掩饰nginx_02

测试网页

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

结果

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

lvs再企业应用中康负载能力强,但是不支持正则处理,不能动静分离。对于大型网站,实施配置复杂,维护成本相对较高

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值