Lvs+Keepalived+Mysql

环境

[root@node1 ~]# cat /etc/redhat-release
CentOS Linux release 7.1. (Core)
[root@node1 ~]# uname -a
Linux node1 3.10.-.el7.x86_64 # SMP Fri Mar :: UTC x86_64 x86_64 x86_64 GNU/Linux

主机

   IP         NAME      Describe
10.0.0.10 node1 主负载
10.0.0.11 node2 备负载
10.0.0.12 node3 节点
10.0.0.13 node4 节点
10.0.0.14 node5 节点
10.0.0.20 VIP

安装

[root@node1 ~]# yum install -y ipvsadm keepalived
[root@node2 ~]# yum install -y ipvsadm keepalived

主负载配置

[root@node1 ~]# vim /etc/keepalived/keepalived.conf 

修改配置文件

! Configuration File for keepalived

global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.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 eno16777736
virtual_router_id 51
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.20/24 dev eno16777736 label eno16777736:0
}
} virtual_server 10.0.0.20 3306 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP real_server 10.0.0.12 3306 {
weight 1
MISC_CHECK {
misc_path "/etc/keepalived/check.sh 10.0.0.12"
misc_dynamic
}
}
real_server 10.0.0.13 3306 {
weight 1
MISC_CHECK {
misc_path "/etc/keepalived/check.sh 10.0.0.13"
misc_dynamic
}
}
real_server 10.0.0.14 3306 {
weight 1
MISC_CHECK {
misc_path "/etc/keepalived/check.sh 10.0.0.14"
misc_dynamic
}
}
}

备负载配置

[root@node2 ~]# vim /etc/keepalived/keepalived.conf 

修改配置文件

! Configuration File for keepalived

global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL_BACKUP
vrrp_skip_check_adv_addr
vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
} vrrp_instance VI_1 {
state BACKUP
interface eno16777736
virtual_router_id 51
priority 50
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.20/24 dev eno16777736 label eno16777736:0
}
} virtual_server 10.0.0.20 3306 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP real_server 10.0.0.12 3306 {
weight 1
MISC_CHECK {
misc_path "/etc/keepalived/check.sh 10.0.0.12"
misc_dynamic
}
} real_server 10.0.0.13 3306 {
weight 1
MISC_CHECK {
misc_path "/etc/keepalived/check.sh 10.0.0.13"
misc_dynamic
}
}
real_server 10.0.0.14 3306 {
weight 1
MISC_CHECK {
misc_path "/etc/keepalived/check.sh 10.0.0.14"
misc_dynamic
}
}
}

自定义脚本做健康检查(node1和node2配置)

[root@node1 ~]# vim /etc/keepalived/check.sh

插入以下配置

#!/bin/bash

mysql -uwsrep -pnx111111 -h$ -e "select 1;" &>/dev/null
ret=$?
if [ $ret -eq ];then
exit
else
exit
fi

启动服务

[root@node1 ~]# systemctl start keepalived
[root@node2 ~]# systemctl start keepalived

开机启动

[root@node1 ~]#systemctl enable keepalived

测试

关闭主负载keepalived,查看VIP是否漂移到备负载

[root@node1 ~]# systemctl stop keepalived

启动主负载keepalived,查看VIP是否漂移回来

[root@node1 ~]# systemctl start keepalived
上一篇:如何在Java中调用Python代码


下一篇:OpenStack 对接 Ceph