一:Keepalvied
Keepalived在这里主要用作RealServer的健康状态检查以及LoadBalance主机和BackUP主机之间failover的实现
网站负载均衡构架图
实验环境: rhel6.5
下载相关软件包:
keepalived.x86_64
修改其配置文件如下:
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost #接收警报的 email 地址,可以添加多个
}
notification_email_from keepalived@server4.example.com
smtp_server 127.0.0.1 #使用本机转发 email
smtp_connect_timeout 30
router_id LVS_DEVEL #load balancer 的标识 ID,用于 email 警报
}
vrrp_instance VI_1 {
state MASTER #备机改为 BACKUP,此状态是由 priority 的值来决定的,当前
priority 的值小于备机的值,那么将会失去 MASTER 状态
interface eth0 #HA 监测网络接口
virtual_router_id 51 #主、备机的 virtual_router_id 必须相同
priority 100 #主机的优先级,备份机改为 50
advert_int 1 #主备之间的通告间隔秒数
authentication { #主备切换时的验证
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.122.100 #HA 虚拟 ip,可加多个
}
}
virtual_server 192.168.122.100 80{
delay_loop 6 #每隔 6 秒查询 realserver状态
lb_algo rr #lvs 调度算法,这里使用轮叫
lb_kind DR #lvs 负载均衡机制,这里使用直连路由
persistence_timeout 50 #同一 IP 的连接 60 秒内被分配到同一台 realserver
protocol TCP #用 TCP 协议检查 realserver 状态
real_server 192.168.122.50 80{
weight 1
TCP_CHECK{
connect_timeout 3
nb_get_retry 3 #故障重试秒数
delay_before_retry 3 #重试延迟
connect_port 80
}
}
real_server 192.168.122.60 80{
weight 1
TCP_CHECK{
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
用scp命令将keepalived.conf文件拷贝到192.168.122.20上
启动keepalived服务
查看日志可以发现其状态为MASTER
用Ipvsadm -L 查看集群状态,-n不做解析
当主集群down掉后,LVS_DR_Backup会自动接管keepalived并转换为MASTER状态
二:haproxy
HAProxy 提供高可用性、负载均衡以及基于 TCP 和 HTTP 应用的代理,支持虚拟主机,
它是免费、快速并且可靠的一种解决方案。HAProxy 特别适用于那些负载特大的 web 站
点, 这些站点通常又需要会话保持或七层处理。HAProxy 运行在当前的硬件上,完全可
以支持数以万计的并发连接。并且它的运行模式使得它可以很简单安全的整 合进您当前
的架构中, 同时可以保护你的 web 服务器不被暴露到网络上。
所需安装包:
haproxy-1.4.24.tar.gz
rpm 包方式:
rpmbuild -tb haproxy-1.4.24.tar.gz
rpm -ivh /root/rpmbuild/RPMS/x86_64/haproxy-1.4.24-1.x86_64.rpm
配置:
# vim /etc/haproxy/haproxy.cfg
#---------------------------------------------------------------------
# Example configuration for a possible web application. See the
# full configuration options online.
#
# http://haproxy.1wt.eu/download/1.4/doc/configuration.txt
#
#---------------------------------------------------------------------
#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
# to have these messages end up in /var/log/haproxy.log you will
# need to:
#
# 1) configure syslog to accept network log events. This is done
# by adding the '-r' option to the SYSLOGD_OPTIONS in
# /etc/sysconfig/syslog
#
# 2) configure local2 events to go to the /var/log/haproxy.log
# file. A line like the following can be added to
# /etc/sysconfig/syslog
#
# local2.* /var/log/haproxy.log
#
log 127.0.0.1 local2 #指定日志设备
# chroot /var/lib/haproxy
chroot /usr/share/haproxy
pidfile /usr/share/haproxy.pid
# pidfile /var/run/haproxy.pid
maxconn 4000 #并发最大连接数量
user haproxy #用户
group haproxy #组
daemon #后台运行
# turn on stats unix socket
stats socket /var/lib/haproxy/stats
#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaults
mode http
log global
option httplog #http日志格式
option dontlognull
option http-server-close
option forwardfor except 127.0.0.0/8
option redispatch #当client连接到挂掉的机器时,重新分配到健康的主机
retries 3 #重试失败3次后认为服务器不可用
timeout http-request 10s
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeout http-keep-alive 10s
timeout check 10s
maxconn 3000
# stats uri /status
#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
#frontend main *:5000
# acl url_static path_beg -i /static /images /javascript /stylesheets
# acl url_static path_end -i .jpg .gif .png .css .js
#
# use_backend static if url_static
# default_backend app
#
#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
#backend static
# balance roundrobin
# server static 127.0.0.1:4331 check
#
##---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
#
#backend app
# balance roundrobin
# server app1 127.0.0.1:5001 check
# server app2 127.0.0.1:5002 check
# server app3 127.0.0.1:5003 check
# server app4 127.0.0.1:5004 check
stats uri /status #haproxy 监控页面
listen www.westos.org *:80
server web1 192.168.122.50:80 cookie app1inst1 check inter 2000 rise 2 fall 3
server web2 192.168.122.60:80 cookie app1inst2 check inter 2000 rise 3 fall 3
server backup 127.0.0.1:8081 backup
#cookie app1inst1:表示 serverid 为 app1inst1
#check inter 2000:检测心跳频率
#rise 2:表示 2 次正确认为服务器可用
#fall 3:表示 3次失败认为服务器不可用
访问 haproxy 监控页面:http://192.168.122.15/status
监控页面添加认证:
listen stats_auth 192.168.122.15:80
stats enable
stats uri /status #监控页面地址
stats auth admin:redhat #管理帐号和密码
stats refresh 5s #刷新频率
haproxy 日志:
# vim /etc/sysconfig/rsyslog
添加如下行
SYSLOGD_OPTIONS="-c 5
# vim /etc/rsyslog.conf
去掉如下行注释
$ModLoad imudp #接受 haproxy 日志
$UDPServerRun 514
并添加
local0.* /var/log/haproxy.log #日志文件位置
重启服务便可以看见了
haproxy+keepalived
MASTER:
! Configuration File for keepalived
vrrp_script check_haproxy {
script "/opt/haproxy_check"
interval 5
weight 2
}
global_defs {
notification_email {
root@localhost
}
notification_email_from keepalived@server5.example.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 108
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.122.100
}
track_script {
check_haproxy
}
}
BACKUP
! Configuration File for keepalived
vrrp_script check_haproxy {
script "/opt/haproxy_check"
interval 5
weight 2
}
global_defs {
notification_email {
root@localhost
}
notification_email_from keepalived@server5.example.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 108
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.122.100
}
track_script {
check_haproxy
}
}
# vim /opt/haproxy_check
#!/bin/bash
/etc/init.d/haproxy status &> /dev/null || /etc/init.d/haproxy restart &> /dev/null
if [ $? -ne 0 ];then
/etc/init.d/keepalived stop &> /dev/null
fi
~