赞
踩
HAProxy是一个使用C语言编写的自由及开放源代码软件,其提供高可用性、负载均衡,以及基于TCP和HTTP的应用程序代理。
HAProxy特别适用于那些负载特大的web站点,这些站点通常又需要会话保持或七层处理。HAProxy运行在当前的硬件上,完全可以支持数以万计的并发连接。并且它的运行模式使得它可以很简单安全的整合进您当前的架构中, 同时可以保护你的web服务器不被暴露到网络上。
HAProxy实现了一种事件驱动, 单一进程模型,此模型支持非常大的并发连接数。多进程或多线程模型受内存限制 、系统调度器限制以及无处不在的锁限制,很少能处理数千并发连接。事件驱动模型因为在有更好的资源和时间管理的用户空间(User-Space) 实现所有这些任务,所以没有这些问题。此模型的弊端是,在多核系统上,这些程序通常扩展性较差。这就是为什么他们必须进行优化以 使每个CPU时间片(Cycle)做更多的工作。
包括 GitHub、Bitbucket、Stack Overflow、Reddit、Tumblr、Twitter和 Tuenti在内的知名网站,及亚马逊网络服务系统都使用了HAProxy。
准备三台centos7虚拟机
① Nginx节点1 IP地址:192.168.226.132
软件包
② Nginx节点2 IP地址:192.168.226.133
软件包
③ Haproxy调度器:IP地址:192.168.226.128
软件包
hostnamectl set-hostname haproxy
su
hostnamectl set-hostname nginx1
su
hostnamectl set-hostname nginx2
su
systemctl stop firewalld
setenforce 0
[root@nginx1 ~]# yum install -y gcc gcc-c++ pcre-devel zlib-devel make
#创建程序用户
[root@nginx1 ~]# useradd -M -s /sbin/nologin nginx
[root@nginx1 ~]# mount.cifs //192.168.226.1/LAMP-C7 /mnt
Password for root@//192.168.226.1/LAMP-C7:
[root@nginx1 ~]# cd /mnt
[root@nginx1 mnt]# tar zxvf nginx-1.12.2.tar.gz -C /opt
[root@nginx1 mnt]# cd /opt/nginx-1.12.2/
[root@nginx1 nginx-1.12.2]# ./configure \
> --prefix=/usr/local/nginx \
> --user=nginx \
> --group=nginx
[root@nginx1 nginx-1.12.2]# make && make install
.....省略部分内容
[root@nginx1 nginx-1.12.2]# cd /usr/local/nginx/html/
[root@nginx1 html]# ls
50x.html index.html xiaomao.jpg
[root@nginx1 html]# vim test.html
<h1>wellcome to nginx ~~~</h1>
<img height=300 src="xiaomao.jpg"/>
------>wq
[root@nginx1 html]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin
#检查语法
[root@nginx1 html]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@nginx1 html]# nginx
[root@nginx1 html]# netstat -natp | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 38436/nginx: master
[root@haproxy ~]# yum install -y pcre-devel bzip2-devel gcc gcc-c++ make
[root@haproxy ~]# mount.cifs //192.168.226.1/LAMP-C7 /mnt
Password for root@//192.168.226.1/LAMP-C7:
[root@haproxy ~]# cd /mnt
[root@haproxy mnt]# tar zxvf haproxy-1.5.19.tar.gz -C /opt
.....省略部分内容
[root@haproxy mnt]# cd /opthaproxy-1.5.19/
#64位系统
[root@haproxy haproxy-1.5.19]# make TARGET=linux26
[root@haproxy haproxy-1.5.19]# make install
......省略部分内容
#创建工作目录 [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 #27行以下删除,我们自己添加内容 # this config needs haproxy-1.1.28 or haproxy-1.2.1 global log 127.0.0.1 local0 log 127.0.0.1 local1 notice #log loghost local0 info maxconn 4096 #chroot /usr/share/haproxy uid 99 gid 99 daemon #debug #quiet defaults log global mode http option httplog option dontlognull retries 3 #redispatch maxconn 2000 contimeout 5000 clitimeout 50000 srvtimeout 50000 listen webcluster 0.0.0.0:80 #监听任意网段80端口的IP地址 option httpchk GET /test.html #使用展现test.html内容的提交方式 balance roundrobin #使用轮询机制(RR) server inst1 192.168.226.132:80 check inter 2000 fall 3 server inst2 192.168.226.133:80 check inter 2000 fall 3 #轮询调度对象为Nginx1和Nginx2两个服务器,设置2000毫秒一次健康检查,如过三次健康检查失败,调度服务器则不会再将请求发给该服务器 -----》wq
[root@haproxy haproxy]# cp /opt/haproxy-1.5.19/examples/haproxy.init /etc/init.d/haproxy
[root@haproxy haproxy]# chmod +x /etc/init.d/haproxy
root@haproxy haproxy]# chkconfig --add /etc/init.d/haproxy
[root@haproxy haproxy]# ln -s /usr/local/sbin/haproxy /usr/sbin/
[root@haproxy init.d]# service haproxy start
Starting haproxy (via systemctl): [ 确定 ]
[root@haproxy init.d]# netstat -natp | grep haproxy
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 127962/haproxy
haproxy配置文件详解:
haproxy配置文件一般分为三个部分
global 全局配置:作用范围为整个服务器端
defaults 默认配置:一般会被应用组件继承。如果在应用组件中没有特别声明,将安装默认配置参数设置
listen 应用组件配置:应用配置项目一般为配置应用模块参数
访问Haproxy服务器web页面,刷新后会以轮询的方式展现Nginx1和Nginx2的页面,如下:
Haproxy的日志默认是输出到syslog中的,在生产环境中一般单独定义出来
定义的过程:
① 修改Haproxy配置文件中关于日志配置的选项,加入配置
② 修改rsyslog配置,将Haproxy相关的配置独立定义到
保存配置文件并重启rsyslog服务,完成rsyslog配置,如下:
[root@haproxy haproxy]# vim haproxy.cfg #在全局设置中设置日志配置项 global log /dev/log local0 info log /dev/log local1 notice ------》wq [root@haproxy haproxy]# service haproxy restart Restarting haproxy (via systemctl): [ 确定 ] #创建新的haproxy配置文件 [root@haproxy haproxy]# touch /etc/rsyslog.d/haproxy.conf #编写haproxy配置文件脚本 [root@haproxy haproxy]# vim /etc/rsyslog.d/haproxy.conf if ($programname == 'haproxy' and $syslogserverity-text == 'info') then -/var/log/haproxy/haproxy-info.log &~ if ($programname == 'haproxy' and $syslogserverity-text == 'notice') then -/var/log/haproxy/haproxy-notice.log &~ ------>wq #重启日志服务 [root@haproxy haproxy]# systemctl restart rsyslog.service
#访问网页后才会产生haproxy文件
[root@haproxy haproxy]# cd /var/log
[root@haproxy log]# ls
haproxy
[root@haproxy log]# cd haproxy
[root@haproxy log]# cat haproxy-info.log
#可以看到来访的信息
haproxy可优化参数:
随着企业网站负载增加, haproxy参数优化相当重要
① maxconn:最大连接数,根据应用实际情况进行调整,推荐使用10 240
② daemon:守护进程模式, Haproxy可以使用非守护进程模式启动,建议使用守护进程模式启动
③ nbproc:负载均衡的并发进程数,建议与当前服务器CPU核数相等或为其2倍
④ option http-server-close:主动关闭http请求选项,建议在生产环境中使用此选项
⑤ timeout http-keep-alive:长连接超时时间,设置长连接超时时间,可以设置为10s
⑥ timeout http-request:http请求超时时间,建议将此时间设置为5~10s,增加http连接释放速度
⑦ timeout client:客户端超时时间,如果访问量过大,节点响应慢,可以将此时间设置短一些,建议设置为1min左右
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。