##########设置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状态
!