【操作系统版本】CentOS 5.4(64位)
【Keepalived版本】1.1.19
1.2.0以上在此系统上有问题,所以选择较低版本。
使用keepadlived ,在数据库主机和备用机上同时开启该程序,通过虚拟VIP地址对外提供数据库访问接口。并抢占VIP地址,正常情况下数据库主机VIP地址具有优先权,并提供数据库访问服务。在数据库主机数据库无法访问时,keep自动关闭程序注销VIP地址,同时备用机抢占VIP地址,数据库备用机将自动接管并顶替主机数据库。
【编译】
- #./configure --prefix=/usr/local/keepalived --with-kernel-dir=/usr/src/kernels/2.6.18-164.el5-x86_64/
【安装】
- #make && make install
- #cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig
- #cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/rc.d/keepalived
- #vi /usr/local/MySQL/bin/MySQL.sh
- #!/bin/sh
- service keepalived stop
- #chmod +x /usr/local/MySQL/bin/MySQL.sh
- #mkdir /etc/keepalived/
- #vi /etc/keepalived.conf
##AB机均按以上步骤安装。
【主机A配置】
- ! Configuration File for keepalived
- global_defs {
- notification_email {
- xikder@foxmail.com
- }
- notification_email_from xikder@foxmail.com
- smtp_server 127.0.0.1
- smtp_connect_timeout 30
- router_id MySQL-ha
- }
- vrrp_instance VI_1 {
- state MASTER #两台配置此处均是BACKUP
- interface eth0
- virtual_router_id 51
- priority 100 #优先级,另一台改为90
- advert_int 1
- nopreempt #不抢占,只在优先级高的机器上设置即可,优先级低的机器不设置
- authentication {
- auth_type PASS
- auth_pass 1111
- }
- virtual_ipaddress {
- 192.168.0.250
- }
- }
- virtual_server 192.168.0.250 3306 {
- delay_loop 2 #每个2秒检查一次real_server状态
- lb_algo wrr #LVS算法
- lb_kind DR #LVS模式
- persistence_timeout 60 #会话保持时间
- protocol TCP
- real_server 192.168.0.245 3306 {
- weight 3
- notify_down "/usr/local/MySQL/bin/MySQL.sh" #检测到服务down后执行的脚本
- TCP_CHECK {
- connect_timeout 10 #连接超时时间
- nb_get_retry 3 #重连次数
- delay_before_retry 3 #重连间隔时间
- connect_port 3306 #健康检查端口
- }
- }
【主机B 配置】
- ! Configuration File for keepalived
- global_defs {
- notification_email {
- xikder@foxmail.com
- }
- notification_email_from xikder@foxmail.com
- smtp_server 127.0.0.1
- smtp_connect_timeout 30
- router_id MySQL-ha
- }
- vrrp_instance VI_1 {
- state BACKUP
- interface eth0
- virtual_router_id 51
- priority 90
- advert_int 1
- authentication {
- auth_type PASS
- auth_pass 1111
- }
- virtual_ipaddress {
- 192.168.0.250
- }
- }
- virtual_server 192.168.0.250 3306 {
- delay_loop 2
- lb_algo wrr
- lb_kind DR
- persistence_timeout 60
- protocol TCP
- real_server 192.168.0.246 3306 {
- weight 3
- notify_down /usr/local/MySQL/bin/MySQL.sh
- TCP_CHECK {
- connect_timeout 10
- nb_get_retry 3
- delay_before_retry 3
- connect_port 3306
- }
- }
【开启服务】
- /usr/local/keepalived/sbin/keepalived -D
|