以前一直用heartbeat或corosync+pacemaker构建高可用集群,现在发现keepalived实现起来更简单。
keepalived的master向backup发送广播,当backup一段时间收不到对方传来的VRRP广播时,backup会通过竞选一个master,master就会重新持有资源。具体的理论知识参见
实验目标:2台Nginx+Keepalived 2台Lamp构建高可用Web集群
规划:
ng1.laoguang.me 192.168.1.22 ng1
ng2.laoguang.me 192.168.1.23 ng2
lamp1.laoguang.me 192.168.1.24 lamp1
lamp2.laoguang.me 192.168.1.25 lamp2
拓扑:
一.基本环境准备
ng1,ng2上安装nginx
lamp1,lamp2上构建LAMP或只安装httpd,我只安装了Httpd,这里不给大家演示了,有需要请看我的其它博文,更改lamp1,lamp2的index.html的内容分别为lamp1和lamp2,以容易区分,实际集群中内容应该是一致的,由共享存储提供。
二.ng1,ng2上安装配置keepalived
下载地址:
2.1 安装keepalived
tar xvf keepalived-1.2.7.tar.gz
cd keepalived-1.2.7
./configure --prefix=/usr/local/keepalived
##可能会提示安装popt-devel包,yum即可
make && make install
2.2 整理配置文件与脚本
mkdir /etc/keepalived
##keepalived默认配置文件从/etc/keepalived下读取
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
##就一个二进制文件,直接拷贝过去即可,多的话就更改PATH吧
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
##脚本的额外配置文件读取位置
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
##启动脚本你懂得
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
##我们关键的keepalived配置文件
2.3 修改ng1的/etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
ibuler@qq.com ##出故障发送邮件给谁
}
notification_email_from keepalived@localhost ##故障用哪个邮箱发送邮件
smtp_server 127.0.0.1 ##SMTP_Server IP
smtp_connect_timeout 30 ##超时时间
router_id LVS_DEVEL ##服务器标识
}
vrrp_instance VI_1 {
state BACKUP
##状态,都为BACKUP,它们会推选Master,如果你写MASTER,它就会是Master,
##当Master故障时Backup会成为Master,当原来的Master恢复后,原来的Master会成为Master
interface eth0 ##发送VRRP的接口,仔细看你的是不是eth0
virtual_router_id 51 ##虚拟路由标识,同一个组应该用一个,即Master与Backup同一个
priority 100 ##重要的优先级哦
nopreempt ##不抢占,一个故障时,重启后恢复后不抢占意资源
advert_int 1 ##同步间隔时长
authentication { ##认证
auth_type PASS ##认证方式
auth_pass ##密钥
}
virtual_ipaddress {
192.168.1.18 ##VIP
}
}
##后面的删除吧,LVS上才有用
拷贝到ng2上一份,只修改priority 90 即可
scp /etc/keepalived/keepalived.conf 192.168.1.23:/etc/keepalived/
##Ng2上
vi /etc/keepalived/keepalived.conf priority 90 ##其它一致
2.4 ng1,ng2上启动keepalived
service keepalived start
查看日志
tail /var/log/messages
Nov 27 08:07:54 localhost Keepalived_vrrp[41871]: VRRP_Instance(VI_1) Entering BACKUP STATE
Nov 27 08:07:54 localhost Keepalived_vrrp[41871]: VRRP sockpool: [ifindex(2), proto(112), fd(11,12)]
Nov 27 08:07:54 localhost Keepalived_healthcheckers[41870]: Using LinkWatch kernel netlink reflector...
Nov 27 08:07:54 localhost Keepalived_vrrp[41871]: VRRP_Instance(VI_1) forcing a new MASTER election
Nov 27 08:07:55 localhost Keepalived_vrrp[41871]: VRRP_Instance(VI_1) Transition to MASTER STATE
Nov 27 08:07:56 localhost Keepalived_vrrp[41871]: VRRP_Instance(VI_1) Entering MASTER STATE
Nov 27 08:07:56 localhost Keepalived_vrrp[41871]: VRRP_Instance(VI_1) setting protocol VIPs.
Nov 27 08:07:56 localhost Keepalived_healthcheckers[41870]: Netlink reflector reports IP 192.168.1.18 added
Nov 27 08:07:56 localhost Keepalived_vrrp[41871]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.1.18
Nov 27 08:08:01 localhost Keepalived_vrrp[41871]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.1.18
查看vip绑定到哪台机器上了
ip addr ##ng1上
....省略
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:e8:90:0b brd ff:ff:ff:ff:ff:ff
inet 192.168.1.22/24 brd 192.168.1.255 scope global eth0
inet 192.168.1.18/32 scope global eth0
inet6 fe80::20c:29ff:fee8:900b/64 scope link
valid_lft forever preferred_lft forever
由此可知vip绑定到ng1上了