环境
系统:Centos 7.4 x64
服务:Mariadb 5.5 、Keepalived 1.3.5.6
结构
主1:192.168.1.108
主2:192.168.1.109
VIP:192.168.1.101
1.两台分别安装 Keepalived
yum -y install keepalived
2.修改Keepalived主配置文件(可提前备份源配置文件)
清空:echo > /etc/keepalived/keepalived.conf
vim /etc/keepalived/keepalived.conf
global_defs {
notification_email {
# 定义通知邮箱,有多个可以换行添加
xxxxxxx@126.com
}
global_defs {
notification_email {
# 定义发送邮件的邮箱
xxxxxx@126.com
}
# 定义邮件的邮件账户
xiangsikai@126.com
# 定义发件服务器
smtp_server 192.168.1.164
# 定义连接smtp服务器超时时间
smtp_connect_timeout 30
# id名称两端需要相同
router_id Mysql-HA
}
vrrp_instance VI_1 {
# 两端都设置BACKUP
state BACKUP
# 定义虚拟IP的网卡
interface ens32
# 定义id 两端需要相同
virtual_router_id 77
# 优先级 主2 应设置成90
priority 100
advert_int 1
# 不主动抢占资源,只在master这台优先级高的设置,backup不设置
nopreempt
authentication {
# 关联类型
auth_type PASS
# 两端需要相同的密码“1111”
auth_pass 1111
}
virtual_ipaddress {
# 定义虚拟VIP
192.168.1.101
}
}
# 填写虚拟VIP与端口
virtual_server 192.168.1.101 3307 {
delay_loop 2
# LVS算法,用不到,我们就关闭了
lb_algo rr
# LVS模式,如果不关闭,备用服务器不能通过VIP连接主MySQL
lb_kind DR
# 同一IP的连接60秒内被分配到同一台真实服务器
#persistence_timeout 50
protocol TCP
# 填写本地mysql端IP 及 端口
real_server 192.168.1.108 3307 {
# 权重值
weight 3
# 服务关闭后启动此端口
notify_down /DATA/shell/mysql.sh
TCP_CHECK {
#连接超时
connect_timeout 3
#重试次数
nb_get_retry 3
#重试间隔时间
delay_before_retry 3
}
}
}
主1 : 有备注说明
global_defs {
notification_email {
xiangsikai@126.com
}
global_defs {
notification_email {
xiangsikai@126.com
}
global_defs {
notification_email {
xiangsikai@126.com
}
xiangsikai@126.com
smtp_server 192.168.1.164
smtp_connect_timeout 30
router_id Mysql-HA
}
vrrp_instance VI_1 {
state BACKUP
interface ens32
virtual_router_id 77
priority 100
advert_int 1
nopreempt
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.101
}
}
virtual_server 192.168.1.101 3307 {
delay_loop 2
lb_algo rr
lb_kind DR
#persistence_timeout 50
protocol TCP
real_server 192.168.1.108 3307 {
weight 3
notify_down /DATA/shell/mysql.sh
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
主1:源配置文件
global_defs {
notification_email {
# 定义通知邮箱,有多个可以换行添加
xxxxxxx@126.com
}
global_defs {
notification_email {
# 定义发送邮件的邮箱
xxxxxx@126.com
}
# 定义邮件的邮件账户
xiangsikai@126.com
# 定义发件服务器
smtp_server 192.168.1.164
# 定义连接smtp服务器超时时间
smtp_connect_timeout 30
# id名称两端需要相同
router_id Mysql-HA
}
vrrp_instance VI_1 {
# 两端都设置BACKUP
state BACKUP
# 定义虚拟IP的网卡
interface ens32
# 定义id 两端需要相同
virtual_router_id 77
# 优先级 主1 应设置成100
priority 90
advert_int 1
# 不主动抢占资源,只在master这台优先级高的设置,backup不设置
#nopreempt
authentication {
# 关联类型
auth_type PASS
# 两端需要相同的密码“1111”
auth_pass 1111
}
virtual_ipaddress {
# 定义虚拟VIP
192.168.1.101
}
}
# 填写虚拟VIP与端口
virtual_server 192.168.1.101 3307 {
delay_loop 2
# LVS算法,用不到,我们就关闭了
lb_algo rr
# LVS模式,如果不关闭,备用服务器不能通过VIP连接主MySQL
lb_kind DR
# 同一IP的连接60秒内被分配到同一台真实服务器
#persistence_timeout 50
protocol TCP
# 填写本地mysql端IP 及 端口
real_server 192.168.1.109 3307 {
# 权重值
weight 3
# 服务关闭后启动此端口
notify_down /DATA/shell/mysql.sh
TCP_CHECK {
#连接超时
connect_timeout 3
#重试次数
nb_get_retry 3
#重试间隔时间
delay_before_retry 3
}
}
}
主2 : 有备注说明
global_defs {
notification_email {
xiangsikai@126.com
}
xiangsikai@126.com
smtp_server 192.168.1.164
smtp_connect_timeout 30
router_id Mysql-HA
}
vrrp_instance VI_1 {
state BACKUP
interface ens32
virtual_router_id 77
priority 50
advert_int 1
#nopreempt
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.101
}
}
virtual_server 192.168.1.101 3307 {
delay_loop 2
lb_algo rr
lb_kind DR
#persistence_timeout 50
protocol TCP
real_server 192.168.1.109 3307 {
weight 3
notify_down /DATA/shell/mysql.sh
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
主2:原配置文件
3.启动两端服务
# 1.启动服务
systemctl start keepalived
# 2.设置开机自启动
systemctl enable keepalived
4.添加脚本
# 1.创建脚本目录
mkdir -p /DATA/shell/
# 2.创建脚本文件
vim /DATA/shell/mysql.sh
#!/bin/bash
pkill keepalived
# 3.给脚本添加执行权限
chmod a+x /DATA/shell/mysql.sh
5.查看主VIP
[root@localhost ~]# ip addr | egrep "inet "
inet 127.0.0.1/8 scope host lo
inet 192.168.1.109/24 brd 192.168.1.255 scope global dynamic ens32
inet 192.168.1.101/32 scope global ens32
6.测试飘逸VIP,关闭服务 自行测试。