##########配置keepalived##########
‘这个时候实现了故障排除,也就是健康检查’
‘但是如果调度器宕机,整个集群就无法访问,所以需要高可用’

#再开一台虚拟机,用来做高可用,配置好yum源

1.源码编译keepalived

tar zxf keepalived-2.0.6.tar.gz
yum install openssl-devel -y
yum install libnl -y
yum install libnl-devel -y
yum install libnfnetlink-devel-1.0.0-1.el6.x86_64.rpm -y
make && make install
./configure --help##可以看到–with-init=(upstart|systemd|SYSV|SUSE|openrc)specify init type,systemd为rhel7版本

./configure --with-init=SYSV##编译前安装gcc,openssl-devel
./configure --prefix=/usr/local/keepalived --with-init=SYSV
#编译完成后看到下面选项为yes,表示成功
Use IPVS Framework : Yes

make && make install

2.把编译好的目录scp到server4

3.配置启动脚本及配置文件#调度器(server1和server4)都做
chmod +x /usr/local/keepalived/etc/rc.d/init.d/keepalived

ln -s /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/##配置文件,脚本都做成软链接

ln -s /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/

ln -s /usr/local/keepalived/etc/keepalived/ /etc/

ln -s /usr/local/keepalived/sbin/keepalived /sbin/

4.配置keepalived

#先关闭ldirectord,因为keepalived也有健康检查
#/etc/init.d/ldirectord stop
#chkconfig ldirectord off

1)先删除调度节点(server1,server4)上的vip,因为keepalived会自己加上

ip addr del 172.25.0.100/24 dev eth0

2)编辑server1(主节点)的keepalived配置文件
global_defs {
notification_email {
root@localhost##节点宕机给谁发送邮件
}
notification_email_from keepalived@localhost##发送人名称
smtp_server 127.0.0.1##发送服务器(本机)
smtp_connect_timeout 30
router_id LVS_DEVEL
vrrp_skip_check_adv_addr
#vrrp_strict##注释掉,不然会有问题
vrrp_garp_interval 0
vrrp_gna_interval 0
}

vrrp_instance VI_1 {
state MASTER##主节点
interface eth0
virtual_router_id 51##做实验时候,让学生修改此ID,每个人的都不能一样,不然会出问题
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.25.0.100##VIP
}
}

virtual_server 172.25.0.100 80 {
delay_loop 3##当rs报错时,尝试多少次之后才邮件告知
lb_algo rr
lb_kind DR##DR模式
#persistence_timeout 50
protocol TCP

real_server 172.25.0.2 80 {
TCP_CHECK {
    weight 1
        connect_timeout 3
        retry 3
        delay_before_retry 3
    }
}
real_server 172.25.0.3 80 {
TCP_CHECK {
    weight 1
        connect_timeout 3
        retry 3
        delay_before_retry 3
    }
}

}

yum install mailx -y
3)编辑备节点(server4)的配置文件
state BACKUP
priority 50##修改这两个选项

4)启动keepalived
/etc/init.d/keepalived start

5)测试
在物理机上访问:curl 172.25.0.100
可以看到轮询

在server1上查看日志:
vim /var/log/messages
Dec 8 16:44:15 server1 Keepalived_vrrp[7656]: (VI_1) Entering MASTER STATE
Dec 8 16:44:15 server1 Keepalived_vrrp[7656]: (VI_1) setting VIPs.

在server4上查看日志:
vim /var/log/messages
Dec 8 16:50:26 server4 Keepalived_vrrp[1090]: (VI_1) removing VIPs.
Dec 8 16:50:26 server4 Keepalived_vrrp[1090]: (VI_1) Entering BACKUP STATE (init)

关闭一台rs的http服务,再次在物理机上访问(可能稍有延迟),也不会报错
ipvsadm策略里也会把down掉的那台rs自动剔除
#vim /var/log/messages 可以看到,server2在down掉后,调度器会检测三次,三次都失败就会剔除
#也就是配置文件中delay_loop 3的作用

mail可以看到会有邮件告知哪台的down了#(没有mail命令的话,yum install -y mailx)

#测试VIP漂移
down掉server(主节点)的keepalived服务
可以看到VIP会自动去掉,然后会漂移到server4上
服务正常访问,看日志可以看到VIP和主备切换的信息

server1再次开启keepalived,会自动接管VIP,并进入MASTER状态

07-12 20:44