keepalived 主 172.16.101.194
keepalived 从 172.16.101.193

keepalived 主

global_defs {
notification_email {br/>[email protected]
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}

vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 88
priority 100
advert_int 1
# nopreempt #不进行抢占操作
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.16.101.201
}

}

virtual_server 172.16.101.201 3306 {
delay_loop 2
# lb_algo/lb_kind 这两项注释掉了,在不注释掉的情况下,通过vip访问数据库,会发现3306端口号状态为filtered,将会导致vip切换之后数据库失联

lb_algo rr

# lb_kind NAT
nat_mask 255.255.255.0
persistence_timeout 60
protocol TCP
real_server 172.16.101.194 3306{
weight 3
# 自检程序,当本服务器mysql进程宕掉之后执行的脚本(keepalived自杀脚本)
notify_down /etc/keepalived/scripts/keepalived_down.sh
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 3306
}
}
}

keepalived 从

global_defs {
notification_email {br/>[email protected]
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}

vrrp_sync_group VG_1 {
group {
VI_1
}
notify_master /etc/keepalived/sendmail.pl
}

vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 88
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}

virtual_ipaddress {
    172.16.101.201
}

}
virtual_server 172.16.101.201 3306 {
delay_loop 2
# lb_algo/lb_kind 这两项注释掉了,在不注释掉的情况下,通过vip访问数据库,会发现3306端口号状态为filtered,将会导致vip切换之后数据库失联

lb_algo rr

# lb_kind NAT
nat_mask 255.255.255.0
persistence_timeout 60
protocol TCP
real_server 172.16.101.193 3306{
weight 3
# 自检程序,当本服务器mysql进程宕掉之后执行的脚本(keepalived自杀脚本)
notify_down /etc/keepalived/scripts/keepalived_down.sh
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 3306
}

}

keepalived_down.sh脚本

#!/bin/bash
service keepalived stop