keepalived高可用配置
young / / / 阅读量

环境准备

MySQL Server1:192.168.0.252
MySQL Server2:192.168.0.102
VIP:192.168.0.150
OS: CentOS 6.5

防火墙开放vrrp协议
-A INPUT -p vrrp -j ACCEPT

Keepalive安装

在线安装

#安装keepalived需要的依赖包
[root@localhost]# yum -y install  lm_sensors-libs net-snmp-libs 
#安装keepalived
[root@localhost]#yum -y install keepalived 

离线安装

首先官网下载安装包,官网地址:http://www.keepalived.org/download.html

安装好好依赖包和gcc环境

[root@localhost keepalived-1.3.5]# yum -y install libnl libnl-devel libnfnetlink-devel gcc gcc-c++ 

开始安装

[root@localhost local]# tar -zxvf /tmp/keepalived-1.3.5.tar.gz 
[root@localhost local]# cd keepalived-1.3.5/
[root@localhost keepalived-1.3.5]# ./configure --prefix=/usr/local/keepalived
[root@localhost keepalived-1.3.5]# make && make install

注册服务

[root@localhost keepalived-1.3.5]#  mkdir /etc/keepalived
[root@localhost keepalived-1.3.5]#  cp ./keepalived/etc/init.d/keepalived /etc/init.d/
[root@localhost keepalived-1.3.5]#  cp  /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
[root@localhost keepalived-1.3.5]#  cp ./keepalived/etc/sysconfig/keepalived /etc/sysconfig/keepalived
[root@localhost ]# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/

这样就可以执行service keepalived [start | stop | reload | restart ]命令。

高可用配置

此处以mysql+keepalived 为例

搭建MySQL主主复制

这里不再赘述,

修改keepalived配置文件

下面是Keepalived主节点上的Keepalived配置,LVS2类似(只需修改 priorityreal_server )

/etc/keepalived/keepalived.conf

###
! Configuration File for keepalived
global_defs {
   #双主相同 局域网有多个keepalived时,router_id不能相同 
   router_id bms_mysql 
}
vrrp_instance VI_1 {
    state BACKUP
    interface eth0 
    #相同VRID为一个组 主备相同
    virtual_router_id 21
    #优先级 backup节点不一样
    priority 90 
    #不主动抢占资源 优先级高的设置
    nopreempt            
    advert_int 1  
    authentication {
        auth_type PASS  
        auth_pass LvS2019  
    }
    #VIP
    virtual_ipaddress {
        192.168.0.150 
    }
}

virtual_server 192.168.0.150 3306 {
    delay_loop 2  
    persistence_timeout 50 
    protocol TCP  

     ## Real Server设置
    real_server 192.168.0.252 3306 {
        weight 3  
        #端口不通时执行此脚本
        notify_down /etc/keepalived/real_server.sh
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
            connect_port 3306
        }
    }
}

/etc/keepalived/real_server.sh 脚本内容

#!/bin/bash
service keepalived stop

启动

mysql启动后才能启动keepalived ,不然keepalived会检测到mysql不通会执行real_server脚本 停止keepalived

[root@localhost]# service keepalived start

注意此时网卡的变化,可以看到虚拟IP已经分配到了realserver上。
此时LVS+Keepalived+MySQL主主复制已经搭建完成。

测试验证

功能性验证

关闭MySQL Server1

查看vip 是否漂移至 slave服务器,通过VIP是否能正常连接MySQL Server2

重新新启动MySQL Server1后 是否自动将故障节点自动加入LVS集群

关闭MySQL Server2

查看vip 是否漂移至 slave服务器,通过VIP是否能正常连接MySQL Server1

重新新启动MySQL Server2后 是否自动将故障节点自动加入LVS集群

总结

MySQL主主复制是集群的基础,组成Server Array,其中每个节点作为Real Server。
Keepalived搭建主备,避免了服务器的单点故障,出现故障时可以自动切换到正常的节点。

支付宝捐赠
请使用支付宝扫一扫进行捐赠
微信捐赠
请使用微信扫一扫进行赞赏
有 0 篇文章