简介:
keepalived是一个类似于layer3, 4 & 5交换机制的软件,也就是我们平时说的第3层、第4层和第5层交换.keepalived的作用是检测web服务器的状态,如果有一台web服务器死机,或工作出现故障,keepalived将检测到,并将有故障的web服务器从系统中剔除,当web服务器工作正常后keepalived自动将web服务器加入到服务器群中,这些工作全部自动完成,不需要人工干涉,需要人工做的只是修复故障的web服务器.
haproxy 反向代理服务器,支持双机热备支持虚拟主机,但其配置简单,拥有非常不错的服务器健康检查功能,当其代理的后端服务器出现故障, haproxy会自动将该服务器摘除,故障恢复后再自动将该服务器加入.新的1.3引入了frontend,backend,frontend根据任意http请求头内容做规则匹配,然后把请求定向到相关的backend.
实验环境以及服务器信息:
os: redhat as 5.1
软件列表:
keepalived-1.2.2.tar.gz
haproxy-1.4.13.tar.gz
服务器信息:
master server 10.10.0.99 (调度主服务器)
slave server 10.10.0.98 (从调度服务器)
vip: 10.10.0.97 (调度服务器的虚拟ip)
real server:
10.10.0.96
10.10.0.95
安装keepalived
# tar zxvf keepalived-1.2.2.tar.gz
# cd keepalived-1.2.2
# ./configure –-prefix=/usr/local/keepalived
# make&&make install
# cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
# mkdir /etc/keepalived
# cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
# chkconfig –add keepalived
# chkconfig keepalived on
# vi /etc/keepalived/keepalived.conf
global_defs {
notification_email {
xxxxxx@139.com
}
notification_email_from
smtp_server mail.139.com
smtp_connect_timeout 30
router_id lvs_devel
}
vrrp_instance vi_1 {
state master
interface eth1
virtual_router_id 51
priority 100
advert_int
authentication {
auth_type pass
auth_pass 1111
}
virtual_ipaddress {
10.10.0.97
}
}
virtual_server 10.10.0.97 80 {
delay_loop 6
lb_algo rr
lb_kind dr
persistence_timeout 50
protocol tcp
real_server 10.10.0.95 80 {
weight 1
tcp_check {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
}
}
real_server 10.10.0.96 80 {
weight 1
tcp_check {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
}
}
}
关于keepalived配置文件的选项解释可以去网上搜下 由于选项参数居多这里就不说明了
# service keepalived start
安装haproxy
# tar zxvf haproxy-1.4.13.tar.gz
# mv haproxy-1.4.13 haproxy
# cd haproxy
# make target=linux26
# vi /usr/local/haproxy/conf/haproxy.conf
# mkdir –p /var/chroot/haproxy
global
chroot /var/chroot/haproxy
daemon
gid 0
log 127.0.0.1 local3
nbproc 2
pidfile /var/run/haproxy-private.pid
ulimit -n 65535
user root
maxconn 32000
spread-checks 4
tune.maxaccept 8
tune.maxpollevents 100
defaults sxit
log global
mode http
option httplog
option dontlognull
log 127.0.0.1 local3
retries 3
option redispatch
maxconn 32000
contimeout 5000
clitimeout 50000
srvtimeout 50000
listen sxit 0.0.0.0:80
appsession jsessionid len 52 timeout 3h
cookie srv insert indirect nocache
mode http
stats enable
stats hide-version
stats uri /haproxy-stats
stats realm haproxy\ statistics
stats auth sxit:sxit
stats refresh 3s
monitor-uri /haproxy_test
balance roundrobin
option httpclose
option forwardfor
option httpchk head /index.html http/1.0
server s1 10.10.0.95:80 check inter 2000 weight 3
server s3 10.10.0.96:80 check inter 2000 weight 3
# /usr/local/haproxy/sbin/haproxy –f /usr/local/haproxy/config/haproxy.config
如果启动没有报什么错误的话,就在浏览器上输入如下地址
(查看服务器状态信息的页面,登录的时候输入上面设置的账号和密码sxit),页面状态如下:
测试负载均衡效果以及高可用性 负载均衡测试:
启动真实服务器的web服务,在2个真实服务器上创建2个凯发app官方网站首页文件,内容分别为test1和test2,如果在浏览器上访问web服务每次刷新既显示test1又显示tetst2就说明负载均衡已经生效了.
高可用性测试:
拔掉主调度服务器的网线或者关闭主调度服务器,看下vip是否顺利的切换到从调度器,假如切换正常,那就说明keepalived已经成功生效了.