Haproxy

Haproxy

Haproxy是一个使用C语言编写的自由及开放源代码软件,其提供高可用性高可用性、负载均衡,以及基于TCP和HTTP的应用程序代理。

Haproxy特别适用于那些负载特大的web站点,这些站点通常又需要会话保持或七层处理。Haproxy运行在当前的硬件上,完全可以支持数以万计的并发连接。并且它的运行模式使得它可以很简单安全的整合进您当前的架构中, 同时可以保护你的web服务器不被暴露到网络上。

HAProxy实现了一种事件驱动,单一进程模型,此模型支持非常大的并发连接数。多进程或多线程模型受内存限制 、系统调度器限制以及无处不在的锁限制,很少能处理数千并发连接。事件驱动模型因为在有更好的资源和时间管理的用户空间(User-Space) 实现所有这些任务,所以没有这些问题。此模型的弊端是,在多核系统上,这些程序通常扩展性较差。这就是为什么他们必须进行优化以 使每个CPU时间片(Cycle)做更多的工作。

包括 GitHub、Bitbucket、Stack Overflow、Reddit、Tumblr、Twitter和Tuenti在内的知名网站,及亚马逊网络服务系统都使用了Haproxy。


在这里插入图片描述

Haproxy特点

  • Haproxy相对LVS,Nginx等负载均衡软件的优点
  • 支持tcp / http 两种协议层的负载均衡,使得其负载均衡功能非常丰富。
  • 支持8种左右的负载均衡算法,尤其是在http模式时,有许多非常实在的负载均衡算法,适用各种需求。
  • 性能非常优秀,基于单进程处理模式(和Nginx类似)让其性能卓越。
  • 拥有一个功能出色的监控页面,实时了解系统的当前状况。
  • 功能强大的ACL支持,给用户极大的方便。

Haproxy算法

  • roundrobin

基于权重进行轮询,在服务器的处理时间保持均匀分布时,这是最平衡,最公平的算法.此算法是动态的,这表示其权重可以在运行时进行调整.

  • static-rr

基于权重进行轮询,与roundrobin类似,但是为静态方法,在运行时调整其服务器权重不会生效.不过,其在后端服务器连接数上没有限制

  • leastconn

新的连接请求被派发至具有最少连接数目的后端服务器.

  • random

基于随机数作为一致性hash的key随机调度,支持加权

LVS,Nginx和Haproxy的区别

LVS: 是基于四层的转发

HAproxy: 是基于四层和七层的转发,是专业的代理服务器

Nginx: 是WEB服务器,缓存服务器,又是反向代理服务器,可以做七层的转发

区别: LVS由于是基于四层的转发所以只能做端口的转发

而基于URL的、基于目录的这种转发LVS就做不了

工作选择

HAproxy和Nginx由于可以做七层的转发,所以URL和目录的转发都可以做

在很大并发量的时候我们就要选择LVS,像中小型公司的话并发量没那么大

选择HAproxy或者Nginx足已,由于HAproxy由是专业的代理服务器

配置简单,所以中小型企业推荐使用HAproxy

Haproxy部署

Haproxy服务器:192.168.237.129

web服务器1:192.168.237.138

web服务器2:192.168.237.148

客户端:192.168.237.160

前提准备:haproxy-1.5.19.tar
web1服务器

[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0
[root@localhost ~]# yum install -y pcre-devel zlib-devel gcc gcc-c++ make 
[root@localhost ~]# useradd -M -s /sbin/nologin nginx
[root@localhost ~]# cd /opt
[root@localhost opt]# tar zxvf nginx-1.12.0.tar.gz -C /opt/
[root@localhost opt]# cd nginx-1.12.0/
[root@localhost opt]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx
[root@localhost opt]# make && make install
[root@localhost opt]# echo "this is wpc" > /usr/local/nginx/html/test.html
[root@localhost opt]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@localhost opt]# nginx      										#启动nginx 服务

web2服务器

[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0
[root@localhost ~]# yum install -y pcre-devel zlib-devel gcc gcc-c++ make 
[root@localhost ~]# useradd -M -s /sbin/nologin nginx
[root@localhost ~]# cd /opt
[root@localhost opt]# tar zxvf nginx-1.12.0.tar.gz -C /opt/
[root@localhost opt]# cd nginx-1.12.0/
[root@localhost opt]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx
[root@localhost opt]# make && make install
[root@localhost opt]# echo "this is zc" > /usr/local/nginx/html/test.html
[root@localhost opt]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@localhost opt]# nginx      										#启动nginx 服务

Haproxy服务器

[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0
[root@localhost ~]# yum install -y pcre-devel bzip2-devel gcc gcc-c++ make		#安装依赖环境
[root@localhost ~]# tar zxvf haproxy-1.5.19.tar.gz -C /opt						#解压到opt
[root@localhost ~]# cd /opt/haproxy-1.5.19/
[root@localhost haproxy-1.5.19]# make TARGET=linux2628 ARCH=x86_64				#编译安装(TARGET=linux26 #内核版本,使用uname -r查看内核,如:2.6.18-371.el5,此时该参数用TARGET=linux26;kernel大于2.6.28的用TARGET=linux2628
ARCH=x86_64 系统位数,64位系统)
[root@localhost 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@localhost haproxy-1.5.19]# mkdir /etc/haproxy								#创建目录
[root@localhost haproxy-1.5.19]# cp haproxy.cfg	/etc/haproxy
[root@localhost haproxy-1.5.19]# cd /etc/haproxy/						
[root@localhost haproxy-1.5.19]# vim haproxy.cfg
# this config needs haproxy-1.1.28 or haproxy-1.2.1

global
        log /dev/log    local0						#修改,配置日志记录,local0为日志设备,默认存放到系统日志
        log /dev/log    local1 notice				#修改,配置日志记录,local0为日志设备,默认存放到系统日志
        #log loghost    local0 info
        maxconn 4096
        #chroot /usr/share/haproxy					#注释,chroot运行路径,为该服务自设置的根目录,需注释掉
        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			#定义一个名为webcluster的应用
        option httpchk GET /test.html	#检查服务器的test.html文件
        balance roundrobin				#负载均衡调度算法使用轮询算法roundrobin
        server inst1 192.168.237.138:80 check inter 2000 fall 3			#定义在线节点
        server inst2 192.168.237.148:80 check inter 2000 fall 3
:wq
[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]# chmod +x haproxy					#赋予权限
[root@localhost init.d]# chkconfig --add /etc/init.d/haproxy			#添加haproxy 系统服务
[root@localhost init.d]# ln -s /usr/local/sbin/haproxy /usr/sbin/haproxy	#软链接
[root@localhost init.d]# service haproxy start							#开启服务
Starting haproxy (via systemctl):                          [  确定  ]

测试
在客户端使用浏览器打开 http://192.168.237.129/test.html ,刷新浏览器测试负载均衡效果

Haproxy集群的日志重新定义

[root@localhost haproxy-1.5.19]# vim haproxy.cfg
# this config needs haproxy-1.1.28 or haproxy-1.2.1

global
        log /dev/log    local0						#修改,配置日志记录,local0为日志设备,默认存放到系统日志
        log /dev/log    local1 notice				#修改,配置日志记录,local0为日志设备,默认存放到系统日志

[root@localhost init.d]# vim /etc/rsyslog.d/haproxy.conf
#将haproxy的info日志记录到/var/log/haproxy/haproxy-info.log下,将notice日志记录到/var/log/haproxy/haproxy-notice.log下。“&~”表示当日志写入到日志文件后,rsyslog停止处理这个信息。

if ($programname == 'haproxy' and $syslogseverity-text == 'info')
then -/var/log/haproxy/haproxy-info.log
&~
if ($programname == 'haproxy' and $syslogseverity-text == 'notice')
then -/var/log/haproxy/haproxy-notice.log
&~
:wq
				
[root@localhost init.d]# mkdir /var/log/haproxy/
[root@localhost init.d]# systemctl restart rsyslog.service

重新访问http://192.168.237.129/test.html ,刷新浏览器测试

[root@localhost init.d]# tail -f /var/log/haproxy/haproxy-info.log			#查看日志
Jun  7 16:47:43 localhost haproxy[41248]: 192.168.237.160:50029 [07/Jun/2022:16:47:43.485] webcluster webcluster/inst1 0/0/0/1/1 200 247 - - ---- 1/1/0/1/0 0/0 "GET /test.html HTTP/1.1"
Jun  7 16:47:43 localhost haproxy[41248]: 192.168.237.160:50030 [07/Jun/2022:16:47:43.533] webcluster webcluster/inst2 0/0/0/1/1 404 726 - - ---- 2/2/0/1/0 0/0 "GET /favicon.ico HTTP/1.1"
Jun  7 16:47:45 localhost haproxy[41248]: 192.168.237.160:50029 [07/Jun/2022:16:47:43.487] webcluster webcluster/inst1 2173/0/0/1/2174 304 178 - - ---- 2/2/0/1/0 0/0 "GET /test.html HTTP/1.1"
Jun  7 16:47:45 localhost haproxy[41248]: 192.168.237.160:50030 [07/Jun/2022:16:47:43.534] webcluster webcluster/inst2 2136/0/0/0/2136 404 726 - - ---- 2/2/0/1/0 0/0 "GET /favicon.ico HTTP/1.1"
Jun  7 16:47:46 localhost haproxy[41248]: 192.168.237.160:50029 [07/Jun/2022:16:47:45.661] webcluster webcluster/inst1 800/0/0/0/800 304 178 - - ---- 2/2/0/1/0 0/0 "GET /test.html HTTP/1.1"
Jun  7 16:47:46 localhost haproxy[41248]: 192.168.237.160:50029 [07/Jun/2022:16:47:46.461] webcluster webcluster/inst2 122/0/0/1/123 200 246 - - ---- 2/2/0/1/0 0/0 "GET /test.html HTTP/1.1"
Jun  7 16:47:46 localhost haproxy[41248]: 192.168.237.160:50029 [07/Jun/2022:16:47:46.584] webcluster webcluster/inst1 148/0/1/0/150 200 247 - - ---- 2/2/0/1/0 0/0 "GET /test.html HTTP/1.1"
Jun  7 16:47:47 localhost haproxy[41248]: 192.168.237.160:50029 [07/Jun/2022:16:47:46.734] webcluster webcluster/inst2 633/0/0/1/634 200 246 - - ---- 2/2/0/1/0 0/0 "GET /test.html HTTP/1.1"
Jun  7 16:47:48 localhost haproxy[41248]: 192.168.237.160:50029 [07/Jun/2022:16:47:47.368] webcluster webcluster/inst1 758/0/1/0/759 200 247 - - ---- 2/2/0/1/0 0/0 "GET /test.html HTTP/1.1"

[root@localhost init.d]# cd /var/log/haproxy/
[root@localhost haproxy]# ll
总用量 4
-rw-------. 1 root root 1710 6月   7 16:47 haproxy-info.log

总结:总体来说很方便,现实生存环境用的也不少,建议一定牢记如何部署,并掌握

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值