ip | 系统 | 安装的软件 |
---|---|---|
192.168.253.158 | CentOS7 | keepalived |
192.168.253.128 | CentOS7 | keepalived |
第一步:在192.168.253.128主机上安装keepalived和发邮件的工具
yum install mailx -y
yum install keepalived -y
第二步:在192.168.253.128主机上修改配置文件/etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from keepalived@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id 192.168.253.128
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 15
priority 95
advert_int 1
authentication {
auth_type PASS
auth_pass 1234
}
virtual_ipaddress {
192.168.253.190 dev ens33
}
notify_master "/etc/keepalived/notify.sh master" ##这个脚本稍后会创建
notify_backup "/etc/keepalived/notify.sh backup"
notify_fault "/etc/keepalived/nofify.sh fault"
}
第三步:创建keepalived状态发生变化时触发的脚本/etc/keepalived/nofify.sh
#!/bin/bash
#发邮件通知root,keepalived状态发生改变
contact='root@localhost'
notify() {
local mailsubject="$(hostname) to be $1, vip floating"
local mailbody="$(date +'%F %T'): vrrp transition, $(hostname) changed to be $1"
echo "$mailbody" | mail -s "$mailsubject" $contact
}
case $1 in
master)
notify master
;;
backup)
notify backup
;;
fault)
notify fault
;;
*)
echo "Usage: $(basename $0) {master|backup|fault}"
exit 1
;;
esac
第四步:在192.168.253.158上安装keepalived,将192.168.253.128的脚本与配置文件发送到192.168.253.158上
192.168.253.158端操作
yum install keepalived -y
yum install mailx -y
192.168.253.128端操作
scp /etc/keepalived/keepalived.conf root@192.168.253.158:/etc/keepalived/keepalived.conf
scp /etc/keepalived/notify.sh root@192.168.253.158:/etc/keepalived/notify.sh
第五步:在192.168.253.158上修改配置文件
[root@localhost ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from keepalived@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id 192.168.253.158 ##修改为当前主机的主机名,这里直接用ip了
}
vrrp_instance VI_1 {
state BACKUP ##设置为BACKUP节点
interface ens33
virtual_router_id 15
priority 90 ##优先级
advert_int 1
authentication {
auth_type PASS
auth_pass 1234
}
virtual_ipaddress {
192.168.253.190 dev ens33
}
notify_master "/etc/keepalived/notify.sh master"
notify_backup "/etc/keepalived/notify.sh backup"
notify_fault "/etc/keepalived/nofify.sh fault"
}
第六步:测试
我们先启动192.168.253.158备用节点的keepalived
systemctl start keepalived
这里可以看出192.168.253.158正在广播自己的心跳信息。
这时候我们吧192.168.253.128启动起来
systemctl start keepalived