keepalived+haproxy
做前说明:
1、MASTER和BACKUP主机各有2个ip,eth1为外网ip,但外网ip地址只用1个即可
2、先把BACKUP主机的haproxy服务启动(可以先把外网ip给它),启动后再把外网ip给MASTER主机
3、MASTER主机宕后又正常使用前应先把haproxy服务给启动,可以先不联网启动,启动后再联网
4、最好给BACKUP主机的eth1设个ip,随意设,不用在同一网段,瞎写都行,因为没有ip的话,MASTER宕后再启动后2台机子将都会那个外网ip,会产生混乱
先在主机上搭好keepalived,目的是建一个能访问的外网ip,然后再搭haproxy,借助这个外网ip来访问,就可以实现热备份或者说主、备切换同时负载均衡的效果
Keepalived的作用是检测web服务器的状态,如果有一台web服务器死机,或工作出现故障,Keepalived将检测到,并将有故障的web服务器从系统中剔除,让没问题的服务器顶上,当web服务器工作正常后再接过去原来的服务
HAProxy提供高可用性、负载均衡以及基于TCP和HTTP应用的代理,支持虚拟主机,它是免费、快速并且可靠的一种解决方案
http://www.keepalived.org/software/keepalived-1.1.19.tar.gz
http://haproxy.1wt.eu/download/1.3/src/haproxy-1.3.20.tar.gz
MASTER主机:
1、搭建keepalived服务
# tar zxf keepalived-1.1.19.tar.gz
# cd keepalived-1.1.19
# ./configure --prefix=/usr/local/keepalived --with-kernel-dir=/usr/src/kernels/2.6.18-164.el5-i686/
# make && make intall
# cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
# mkdir /etc/keepalived
# cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
# vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
yangzhiming0@126.com (当IP发生迁移等事件的时候发送邮件到指定邮箱)
}
notification_email_from root@yang.com (邮件发送方)
smtp_server 127.0.0.1 (邮件外发服务器地址)
smtp_connect_timeout 30 (邮件超时时间为30秒)
router_id LVS_DEVEL (一个实例的标识)
}
vrrp_instance VI_1 {
State BACKUP (设置服务器模式,注要大写)
interface eth1 (实例网卡,也就是提供服务的网卡)
virtual_router_id 51
priority 100 (设定权值为100 主备之间的权值差最好大于50 权值范围为0~255)
advert_int 1 (检查时间为1秒)
authentication { (认证方式为密码认证)
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
200.200.200.1 (外网ip)
}
}
后面的内容不用改
:wq
启动keepalived
# service keepalived start
开机自启动
# chkconfig --add keepalived
# chkconfig --level 35 keepalived on
# ip a
3: eth1: mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:01:11:2a brd ff:ff:ff:ff:ff:ff
inet 200.200.200.1/32 scope global eth0
inet6 fe80::20c:29ff:fe01:112a/64 scope link
其它主机ping200.200.200.1是可以ping通的,访问页面也是可以的
2、搭建haproxy服务
# tar zcvf haproxy-1.3.20.tar.gz
# cd haproxy-1.3.20
# vi Makefile
PREFIX = /usr/local/haproxy (原为PREFIX = /usr/local)
:wq
# make TARGET=linux26
# make install
haproxy应用时有2种方式,linsten方式和frontend+backend方式
方式一(listen):
# vi /usr/local/haproxy/haproxy.cfg
global
log 127.0.0.1 local0
maxconn 4096
chroot /usr/local/haproxy
uid 99
gid 99
daemon
nbproc 1
pidfile /usr/local/haproxy/haproxy.pid
defaults
log global
log 127.0.0.1 local3
option httplog
option httpclose
option dontlognull
option forwardfor
option redispatch
retries 2
maxconn 2000
mode http
stats uri /haproxy-stats
stats auth ming:123456
stats refresh 30s
monitor-uri /site_status
contimeout 5000
clitimeout 50000
srvtimeout 50000
listen web 200.200.200.1:1080
balance roundrobin
server s1 192.168.1.10:80 weight 3 check
server s2 192.168.1.12:80 weight 3 check
:wq
方式二(frontend+backend):
# vi /usr/local/haproxy/haproxy.cfg
global
log 127.0.0.1 local0
maxconn 4096
chroot /usr/local/haproxy
uid 99
gid 99
daemon
nbproc 1
pidfile /usr/local/haproxy/haproxy.pid
defaults
log global
log 127.0.0.1 local3
option httplog
option httpclose
option dontlognull
option forwardfor
option redispatch
retries 2
maxconn 2000
mode http
stats uri /haproxy-stats
stats auth ming:123456
stats refresh 30s
monitor-uri /site_status
contimeout 5000
clitimeout 50000
srvtimeout 50000
frontend web 200.200.200.1:1080
default_backend aa
backend aa
balance roundrobin
server yang 192.168.1.10:80 weight 5 check
server ming 192.168.1.12:80 weight 3 check
:wq
启动haproxy
# /usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/haproxy.cfg
开机自启动:
# vi /etc/rc.local
/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/haproxy.cfg
:wq
查看haproxy页面:
http://200.200.200.1:1080/haproxy-status
用户名:ming 密码:123456
验证效果:
在2主机上分别建2个页面1.html 内容写各自主机ip
http://200.200.200.1:1080/1.html
第一次会显示192.168.1.10
第二次会显示192.168.1.12
注:
1、1080是haproxy的端口号,是随意写的,可改
2、电脑重启后/etc/init.d/httpd 会被改为/etc/init.d/httpd.rpmorig ,要自己改回来,chkconfig开机启动命令也要再重写一遍,原因不详
BACKUP主机:
其步骤和MASTER主机步骤一样,只需把MASTER改成BACKUP 及 priority 100 改为50即可,haproxy步骤和MASTER主机一样,一点不用变
验证:
1、用http://200.200.200.1:1080/1.html来访问,能实现负载均衡
2、在MASTER主机宕后,BACKUP主机会自动接过来haproxy服务,并且保证了客户访问不会受影响(还是用外网ip200.200.200.1:1080访问)
3、当MASTER主机正常后,200.200.200.1又会回到MASTER主机上,haproxy服务也就自然接了回去
本文转自linux博客51CTO博客,原文链接http://blog.51cto.com/yangzhiming/871697如需转载请自行联系原作者
yangzhimingg