Linux+Keepalived 双机互备

http://www.ideawu.net/blog/archives/398.html
 

大规模Web应用有两个要解决的问题: 高可用性和负载均衡. Keepalived可以提供IP层的高可用性, 一旦某一台机器的网络出现问题, 另一台服务器会立即(几秒或者更少的时间)使用出故障的服务器的IP进行工作. 具体的工作原理会在我学习之后, 发布在后期的文章中.

现在有两台虚拟机ServerA和ServerB. 两个对外提供Web服务器的虚IP(VIP)192.168.200.100和192.168.200.200, 虚IP用在keepalived的配置中, 网卡接口配置有内网IP.

 

ServerA:
eth0: 192.168.200.128
VIP: 192.168.200.100

ServerB:
eth0: 192.168.200.129
VIP: 192.168.200.200

如果两台服务器都正常地提供网络服务, 那么, 发往192.168.200.100的服务请求会被ServerA处理, 发往192.168.200.200的服务请求会被ServerB处理. 假设只有ServerB出现故障, 那么, 所有的请求都由ServerA进行处理. 当只有ServerA出现故障时, 也是同理.

安装Ubuntu Linux Server

本文使用的Linux为Ubuntu 9.04 服务器版, 请到http://www.ubuntu.com.cn/getubuntu/download-server/下载. 需要安装gcc, make开发环境.

 

安装Keepalived

然后到keepalived网站下载源码, http://www.keepalived.org/download.html, 本文使用1.1.16版本.

 

tar zxf keepalived-1.1.16.tar.gz
cd keepalived-1.1.16
./configure --prefix=/usr --sysconf=/etc
make
make intall

安装后的keepalived的启动脚本在/etc/rc.d/init.d/keepalived, 配置文件在/etc/keepalived/keepalived.conf, 还有一个配置文件/etc/sysconfig/keepalived, 一般不用到.

如果你希望keepalived开机自动启动,

 

[email protected]:~# ln -s /etc/rc.d/init.d/keepalived /etc/rc2.d/S99keepalived

启动脚本可能有问题, 编辑/etc/rc.d/init.d/keepalived, 将/var/lock/subsys/$prog修改为/var/lock/$prog.

配置Keepalived

接下来, 是最重要的修改配置文件/etc/keepalived/keepalived.conf.

 

!This is keepalived config file, ideawu.net
global_defs {
notification_email {
[email protected]
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
lvs_id LVS_DEVEL_1
}

vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
smtp_alert

authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.200.100
}
}

vrrp_instance VI_2 {
state BACKUP
interface eth0
virtual_router_id 52
priority 10
advert_int 1
smtp_alert

authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.200.200
}
}

执行 /etc/rc.d/init.d/keepalived start 启动keepalived后, 执行ip a, 你将看到类似的信息:

 

[email protected]:~# ip a
2: eth0: 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 192.168.200.128/24 brd 192.168.200.255 scope global eth0
inet 192.168.200.100/32 scope global eth0
inet 192.168.200.200/32 scope global eth0
inet6 fe80::20c:29ff:fe01:112a/64 scope link

可以看到, ServerA的网卡绑定了两个虚IP 192.168.200.100和192.168.200.200, 这时在第3台机器上ping这两个IP, 是可以通的.

然后, 按上面的方法安装ServerB. ServerB的keepalived.conf配置和ServerA基本相同, 但是, 把state MASTER和state BACKUP调换, priority 100和priority 10调换. 启动ServerB后, 再到ServerA上运行ip a, 你将看到

 

[email protected]:~# ip a
2: eth0: 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 192.168.200.128/24 brd 192.168.200.255 scope global eth0
inet 192.168.200.100/32 scope global eth0
inet6 fe80::20c:29ff:fe01:112a/64 scope link

192.168.200.200已经不见了, 因为被ServerB使用了, ServerB是这个IP的MASTER, 它有优先使用权. 这样, 两个IP的网络数据分别被两台服务器处理. 如果设置好DNS, 就可以进行负载均衡了. 现在验证ServerB出在故障的情况, 请ServerB的网线拔掉, 然后在ServerA上执行ip a, 你将看到

 

[email protected]:~# ip a
2: eth0: 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 192.168.200.128/24 brd 192.168.200.255 scope global eth0
inet 192.168.200.100/32 scope global eth0
inet 192.168.200.200/32 scope global eth0
inet6 fe80::20c:29ff:fe01:112a/64 scope link

192.168.200.200又被ServerA使用了. 如果你安装了lighttpd, 各放两个内容不同的网页, 你将能更直观的看到这种效果.


 

你可能感兴趣的:(linux,keepalived,集群,休闲,双机互备)