HAProxy
它是免费,快速且可靠的一种解决方案没,适用于那些负载特大的web站点
这些站点通常又需要会话保持或七层处理
提供高可用性,负载均衡及基于tcp和http应用的代理
衡量负载均衡器性能的因素
Session rate 会话率:
每秒钟产生的会话数
Session concurrency 并发会话数:
服务器处理会话的时间越长,并发会话数越多
Data rate 数据速率:
以MB/s或Mbps衡量,大的对象导致并发会话数增加,
高会话数,高数据速率要求更多的内存
haproxy工作模式
mode http:
客户端请求被深度分析后再发往服务器
http事务模型:
hrrp close,客户端像服务器建立一个tcp连接,三次握手消耗相对较大的系统资源,延迟大
keep-alive,一次连接可以传输多个请求
开启keep-alive
[[email protected] ~]# vim /etc/httpd/conf/httpd.conf
KeepAlive On
[[email protected] ~]# vim /etc/httpd/conf/httpd.conf
KeepAlive On
pipelining,任然使用keep-alive,适用于有大量图片的页面,降低了多次请求之间的网络延迟
mode tcp:
客户端与服务器之间建立会话,不检查第七层信息
mode health:
仅做健康检查,已经不建议使用
实例:配置haproxy
[[email protected] ~]# yum -y install pcre pcre-devel(安装开发依赖包)
[[email protected] ~]# tar -xf haproxy-1.4.24.tar.gz
[[email protected] ~]# yum -y install gcc gcc-c++
[[email protected] haproxy-1.4.24]# make USE_PCRE=1 PREFIX=/usr/local/haproxy TARGET=linux2628 install(配置文件说明内核版本高于2.6以上要写上内核版本)
[[email protected] ~]# ln -s /usr/local/haproxy/sbin/haproxy/ /usr/local/sbin/(建立符号链接)
[[email protected] ~]# mkdir /usr/local/haproxy/conf
[[email protected] haproxy-1.4.24]# cp examples/haproxy.cfg /usr/local/haproxy/conf/(拷贝安装源目录下的示例文件)
haproxy配置文件说明
命令行:总是具有最高优先级
global部分:全局设置进程级别参数
代理声明部分:来自于default,listen,frontend和backend
default:为后续的其他部分设置缺省参数,缺省参数可以被后续部分重置
frontend:描述接受客户端侦听套接字(socket)集
backend:描述转发链接的服务器集
listen:把frontend和backend结合到一起的完整申明
[[email protected] ~]# vim /usr/local/haproxy/conf/haproxy.cfg
global
....
maxconn 4096(最大连接数)
chroot /usr/share/haproxy
pidfile /var/run/haproxy.pid(添加pid文件)
.......
defaults
....
option redispatch(改为option)
stats uri /ha_mon(添加uri,路径随便写)
...
listen appli1-rewrite 0.0.0.0:80(0.0.0.0 相当于虚拟ip)
cookie SERVERID rewrite
balance roundrobin
server web1 192.168.4.2:80 cookie app1inst1 check inter 2000 rise 2 fall 5(服务器名,ip地址)
server web2 192.168.4.3:80 cookie app1inst2 check inter 2000 rise 2 fall 5(把后面的不要的配置都删除)
[[email protected] conf]# haproxy -f /usr/local/haproxy/conf/haproxy.cfg(开启服务)
[[email protected] conf]# netstat -tlnp |grep :80(查看监听端口)
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 13810/haproxy
[[email protected] ~]# firefox http://192.168.4.4/ha_mon(测试访问查看监测)
[[email protected] ~]# kill $(pidof haproxy)(关闭进程)
[[email protected] ~]# pidof haproxy
13810
[[email protected] ~]# haproxy -f /usr/local/haproxy/conf/haproxy.cfg -st 13810(重启)
——————————————————————————————————————————————————————————————————————————————
Keepalived热备
keepalived实现了高可用集群,最初为lvs设计的,专门监控各服务器节点的状态
后来加入了vrrp功能,防止单点故障(VRRP 虚拟冗余路由协议)
Keepalived运行原理
keepalived检测每个服务器节点状态,服务器节点异常或工作出现故障,Keepalived将故障节点从集群系统中剔除,
故障节点恢复后,Keepalived再将其加入到集群系统中
所有工作自动完成,无需人工干预
实例:配置高可用web集群(两台web服务器做相同的操作)
[[email protected] ~]# yum -y install gcc gcc-c++ openssl-devel popt popt-devel(安装依赖)
[[email protected] ~]# tar -xf keepalived-1.3.6.tar.gz
[[email protected] keepalived-1.2.7]# ./configure --sysconf=/etc
[[email protected] keepalived-1.2.7]# make
[[email protected] keepalived-1.2.7]# make install
[[email protected] ~]# ln -s /usr/local/sbin/keepalived /usr/sbin/
[[email protected] ~]# vim /etc/keepalived/keepalived.conf
vrrp_instance VI_1 {
state MASTER(主服务器,另一个写副服务器)
interface eth0(正用的网卡)
virtual_router_id 51(虚拟路由器id号)
priority 100(优先级)
advert_int 1(通告间隔)
authentication {
auth_type PASS(密码认证方式)
auth_pass 1111(两台密码一样)
}
virtual_ipaddress {
192.168.4.200(对外提供的ip地址)
}
}(底下的配置全部dd)
[[email protected] ~]# vim /etc/keepalived/keepalived.conf
vrrp_instance VI_1 {
state SLAVE(副服务器)
interface eth0
virtual_router_id 51
priority 90(优先级比主服务器低)
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.4.200
}
}
[[email protected] ~]# service keepalived start
[[email protected] ~]# firefox http://192.168.4.200(访问测试)
实例:搭建高可用负载均衡的web集群
环境:把两台的keepalived stop,实现的是DR模式,所以web服务仍然要在lo上配置vip
仍然需要调整参数,把第一台lvs上的vip删除
把第一台调度器上的lvs规则清空
再准备一台调度器,主机名为lvs2,ip地址为192.168.4.4
在第二台调度器安装lvs(ipvsadm)
删除lvs上的vip,清空lvs规则
[[email protected] network-scripts]# rm -rf ifcfg-eth0:0
[[email protected] ~]# /etc/init.d/network restart
[[email protected] ~]# ipvsadm -D -t 192.168.4.100:80
安装lvs
[[email protected] ~]# vim /etc/yum.repos.d/rhel6.repo
[rhel]
name=Linux NSD
baseurl=file:///root/myiso/LoadBalancer(需要加上这个路径,yum默认找server这个库)
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release
[[email protected] ~]# yum -y install ipvsadm
安装依赖, 安装keepalived(两台lvs做相同的操作)
[[email protected] ~]# yum -y install gcc gcc-c++ openssl-devel popt-devel
[[email protected] ~]# tar -xf keepalived-1.2.7.tar.gz
[[email protected] keepalived-1.2.7]# ./configure --sysconf=/etc/
[[email protected] keepalived-1.2.7]# make
[[email protected] keepalived-1.2.7]# make install
[[email protected] keepalived-1.2.7]# ln -s /usr/local/sbin/keepalived /usr/sbin/
[[email protected] keepalived-1.2.7]# vim /etc/keepalived/keepalived.conf
global_defs {
notification_email {
[email protected](服务器有问题会向这个邮箱发邮件)
}
notification_email_from [email protected](谁发的邮件)
smtp_server 127.0.0.1(发邮件的地址)
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state MASTER(主服务器,另一个lvs要写副服务器)
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.4.100
}
}
virtual_server 192.168.4.100 80 {(真实机上的lo:0的虚拟ip)
delay_loop 6
lb_algo rr(rr算法)
lb_kind DR(DR模式)
nat_mask 255.255.255.0
persistence_timeout 50
protocol TCP
real_server 192.168.4.2 80 {(真实服务器地址)
weight 1
TCP_CHECK {(这里默认是ssl,改为tcp)
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.4.3 80 {(真实服务器地址)
weight 1
TCP_CHECK {(这里默认是ssl,改为tcp)
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}(下面的配置文件全部dd)
[[email protected] keepalived-1.2.7]# scp /etc/keepalived/keepalived.conf 192.168.4.5:/etc/keepalived/(把配置好的服务拷贝到lvs2的配置目录下)
[[email protected] keepalived-1.2.7]# vim /etc/keepalived/keepalived.conf
vrrp_instance VI_1 {
state SLAVE(改为副服务器)
interface eth0
virtual_router_id 51
priority 90(优先级)
[[email protected] ~]# service keepalived start; chkconfig keepalived on
[[email protected] ~]# ipvsadm -Ln(验证)
[[email protected] ~]# firefox http://192.168.4.100/bbs(访问测试)
————————————————————————————————————————————————————————————————————————————————————————————-