HAProxy是一个使用C语言编写的自由及开放源代码软件[1],其提供高可用性、负载均衡,以及基于TCP和HTTP的应用程序代理。HAProxy特别适用于那些负载特大的web站点,这些站点通常又需要会话保持或七层处理。HAProxy运行在当前的硬件上,完全可以支持数以万计的并发连接。并且它的运行模式使得它可以很简单安全的整合进您当前的架构中,
同时可以保护你的web服务器不被暴露到网络上。HAProxy实现了一种事件驱动,
单一进程模型,此模型支持非常大的并发连接数。多进程或多线程模型受内存限制
、系统调度器限制以及无处不在的锁限制,很少能处理数千并发连接。事件驱动模型因为在有更好的资源和时间管理的用户空间(User-Space)
实现所有这些任务,所以没有这些问题。此模型的弊端是,在多核系统上,这些程序通常扩展性较差。这就是为什么他们必须进行优化以
使每个CPU时间片(Cycle)做更多的工作。包括 GitHub、Bitbucket[3]、Stack
Overflow[4]、Reddit、Tumblr、Twitter[5][6]和
Tuenti[7]在内的知名网站,及亚马逊网络服务系统都使用了HAProxy。
配置HAProxy Session亲缘性的三种方式
haproxy负载均衡保持客户端和服务器Session亲缘性的三种方式:
1 用户IP 识别
haproxy 将用户IP经过hash计算后 指定到固定的真实服务器上(类似于nginx 的IP hash 指令)
配置指令 balance source
2 cookie 识别
haproxy 将WEB服务端发送给客户端的cookie中插入(或添加前缀)haproxy定义的后端的服务器COOKIE ID。
配置指令例举 cookie SESSION_COOKIE insert indirect nocache
用firebug可以观察到用户的请求头的cookie里 有类似" Cookie jsessionid=0bc588656ca05ecf7588c65f9be214f5; SESSION_COOKIE=app1" SESSION_COOKIE=app1就是haproxy添加的内容
3 session 识别
配置举例:
#vi /usr/local/haproxy/haproxy.cfg
backend COOKIE_srv
mode http
cookie SESSION_COOKIE insert indirect nocache
server REALsrv_70 184.82.239.70:80 cookie 11 check inter 1500 rise 3 fall 3 weight 1
server REALsrv_120 220.162.237.120:80 cookie 12 check inter 1500 rise 3 fall 3 weight 1
backend SOURCE_srv
mode http
balance source
server REALsrv_70 184.82.239.70:80 cookie 11 check inter 1500 rise 3 fall 3 weight 1
server REALsrv_120 220.162.237.120:80 cookie 12 check inter 1500 rise 3 fall 3 weight 1
backend APPSESSION_srv
mode http
appsession JSESSIONID len 64 timeout 5h request-learn
server REALsrv_70 184.82.239.70:80 cookie 11 check inter 1500 rise 3 fall 3 weight 1
server REALsrv_120 220.162.237.120:80 cookie 12 check inter 1500 rise 3 fall 3 weight 1
一、Haproxy
1、Haproxy的作用和特点
1)Haproxy的作用
是一个故障转移群集调度器
支持静态网站配置故障转移群集
2)Haproxy的特点
支持动态分离
处理数据速度快
支持高并发
一般静态网站使用
2、常见的负载均衡调度器类型
1)LVS
一般动态网站使用
支持网络负载均衡
故障转移群集需要和keepalived结合使用
2)nginx
nginx可以是一个代理工具或者负载均衡调度器
支持动静分离
需要使用upstream实现故障转移
3)keepalived
是一个故障转移工具
依赖VRRP协议
可以和apache、ngin、tomcat结合工作
keepalived使用比较广泛
3、负载均衡调度器的类型
1)软件
处理速度慢稳定性差
LVS、nginx、haproxy
2)硬件调度器
处理数据速度快稳定性强
F5、梭子鱼、绿盟
4、常见的负载均衡调度器算法
1)RR
轮询调度算法
参与负载均衡的服务器各占50%负载
2)最小连接数
调度器选择负债量最少的服务器相应客户端请求
3)基于来源访问调度
将不同地理位置的客户端请求转发到不同的服务器相应客户端请求
需要基于源IP地址和cookie实现
5、http请求的方式和http代码
1)http请求的方式
GET:安全性差速度快
post:安全性强速度慢
2)http的状态代码
200、301都是正常访问
400、500都属于异常访问
二.两台服务器安装nginx,配置命令一样
(1)
安装nginx的依赖程序
[root@centos01 ~]# yum -y install
pcre-devel zlib-devel
(2) 创建管理nginx的用户
[root@centos01 ~]# useradd -M -s
/sbin/nologin nginx
切换linux光盘解压配置nginx,
[root@centos01 ~]# umount /mnt/
[root@centos01 ~]# tar zxf /mnt/nginx-1.6.0.tar.gz
-C /usr/src/
[root@centos01 ~]# cd /usr/src/nginx-1.6.0/
[root@centos01 nginx-1.6.0]# https://blog.csdn.net/ai_hanghang/article/details/configure
–prefix=/usr/local/nginx --user=nginx --group=nginx
–with-http_stub_status_module
(3)编译安装nginx
[root@centos01 nginx-1.6.0]# make && make install
3 优化nginx执行命令,创建测试文件
[root@centos01 ~]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin/
[root@centos01 ~]# echo
“www.benet.com” > /usr/local/nginx/html/index.html
三.安装配置haproxy
1.安装haproxy
(1)安装依赖程序
[root@centos03 ~]# yum -y install pcre-devel bzip2-devel
(2)解压编译haproxy支持64位系统
[root@centos03 ~]# tar zxvf haproxy-1.4.24.tar.gz -C /usr/src/
[root@centos03 ~]# cd /usr/src/haproxy-1.4.24/
[root@centos03 haproxy-1.4.24]# make TARGET=linux26
(3)安装haproxy
[root@centos03 haproxy-1.4.24]# make install
2.生成haproxy配置文件
(1)创建保存haproxy配置文件目录
[root@centos03 ~]# mkdir /etc/haproxy
(2)生成主配置文件
root@centos03 ~]# cd /usr/src/haproxy-1.4.24/
[root@centos03 haproxy-1.4.24]# cp examples/haproxy.cfg /etc/haproxy/
(3)创建haproxy服务器控制脚本设置执行权限
[root@centos03 haproxy-1.4.24]# cp examples/haproxy.init /etc/init.d/haproxy
[root@centos03 haproxy-1.4.24]# chmod +x /etc/init.d/haproxy
(4)添加系统服务器设置开机自动启动
[root@centos03 haproxy-1.4.24]# chkconfig --add haproxy
[root@centos03 haproxy-1.4.24]# chkconfig --level 35 haproxy on
(5)优化程序执行命令
[root@centos03 haproxy-1.4.24]# cp haproxy /usr/sbin/
(6)创建服务运行的临时目录
[root@centos03 haproxy-1.4.24]# mkdir -p /usr/share/haproxy
3.配置haproxy群集
修改主配置文件
[root@centos03 ~]# vim /etc/haproxy/haproxy.cfg
contimeout 10 连接超时时间
listen
nginx 192.168.100.30:80
启动haproxy服务
[root@centos03 ~]# /etc/init.d/haproxy start
客户端访问测试
本网信息来自于互联网,目的在于传递更多信息,并不代表本网赞同其观点。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,并请自行核实相关内容。本站不承担此类作品侵权行为的直接责任及连带责任。如若本网有任何内容侵犯您的权益,请及时联系我们,本站将会在24小时内处理完毕,E-mail:xinmeigg88@163.com
本文链接:http://www.bhha.com.cn/news/4221.html