MySQL + keepalived 宕机自动切换

上一篇介绍了MySQL的双主备份,本次总结keepalived的安装与使用。

MySQL的双主备份:https://blog.csdn.net/u011144417/article/details/84614933

参考资料

mysql双机热备的实现 : https://blog.csdn.net/qq394829044/article/details/53203645

keepalived :http://www.cnblogs.com/guantou1992/p/9729465.html

windows+ubuntu双系统安装:https://www.linuxidc.com/Linux/2016-04/130520.htm

Ubuntu 安装keepalived:https://blog.csdn.net/lvshanhshan/article/details/80559437

环境

系统:Ubuntu -16.04.3  

A服务器:192.168.1.201

B服务器:192.168.1.201

keepalived IP:192.168.1.200

安装基本依赖库

apt-get install keepalived libpop-dev libssl-dev openssl daemon

安装keepalived

apt-get install keepalived

我的机器不知道为什么,/etc/keepalievd目录是空的,于是进行离线安装。

keepalived下载地址:www.keepalived.org/software  

我下载后放到了/usr/local/src目录下。

tar zxvf keepalived-1.2.18.tar.gz

cd keepalived-1.2.18

./configure --prefix=/usr/local/keepalived

make && make install

keepalived的配置

mkdir /etc/sysconfig

cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/

cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/

cp /usr/local/keepalived/sbin/keepalived /sbin/

把keepalived 的配置文件放到/etc/keepalived 下,keepalived默认会读取这个文件。

mkdir /etc/keepalived

cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/

编辑配置文件,修改以下的内容即可:

vi /etc/keepalived/keepalived.conf

参考如下:

! Configuration File for keepalived

global_defs {
   notification_email {
     jing.lin@turnipsmart.com  # 报警邮件接收人的地址
   }
   notification_email_from jing.lin@turnipsmart.com # 发送报警邮件发件人地址
   smtp_server 127.0.0.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_script monitor_nginx {

  script "/opt/chk_mysql.sh"  #根据自己的实际路径放置脚本文件
  interval 2                  # 脚本执行间隔
  Weight -5                   #脚本结果导致的优先级变更:5表示优先级加5;-5表示优先级减5
  fall 2                 
  rise 1 
}

vrrp_instance VI_1 {
    state MASTER           # 设置服务器模式master端 从服务器设置为BACKUP
    interface enp2s0       # 实例网卡,也就是提供服务的网卡,可通过ifconfig
    virtual_router_id 51   # 设置vrid 主从服务器设置一样
    priority 101           # 设置本节点的优先级,优先级高的为master
    advert_int 1           # 检查间隔,默认为1秒
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.1.200      # 设置vip,虚拟ip地址
    }
ck_script {               
   chk_mysql_port             
}
}

chk_mysql.sh内容如下:

#!/bin/bash
counter=$(netstat -na|grep "LISTEN"|grep "3306"|wc -l)
if [ "${counter}" -eq 0 ]; then
    service keepalived stop
fi

将keepalived作为系统启动服务

mkdir -p /etc/rc.d/init.d

cp /usr/share/initramfs-tools/scripts/functions /etc/rc.d/init.d/

vi /etc/init.d/keepalived +23

修改如下:

#daemon keepalived ${KEEPALIVED_OPTIONS}#注释当前行,添加如下内容

 daemon keepalived start

创建subsys目录

mkdir /var/lock/subsys 

(注意: 此目录在服务器重启后会消失,需要将其写到开机启动项中;如写到/etc/rc.local里)

开机自启动

vi /etc/rc.local 

修改为以下内容即可:

mkdir /var/lock/subsys

service keepalived start

exit 0

启动keepalived

service keepalived start

验证

ip addr show

从服务器的ip addr show 是不会有192.168.1.200 这个ip地址的,除非等master服务器宕机后,成为主节点后就会自动生成这个虚拟IP地址的,当master服务器正常后就会自动消失。

遇到的问题解决方案

-------------------------------------
1、启动错误 Unit keepalived.service is masked.
keepalived.service 拷贝覆盖/lib/systemd/system/keepalived.service
在/var/run/下新建一个文件touch keepalived.pid
修改之后 运行systemctl daemon-reload#重新载入 systemd

2、Failed to start LVS and VRRP High Availability Monitor
去/lib/systemd/system/keepalived.service配置文件中是否有目录或文件错误。
修改之后 运行systemctl daemon-reload#重新载入 systemd

配置systemctl常用命令
systemctl daemon-reload  重新加载
systemctl enable keepalived.service  设置开机自动启动
systemctl disable keepalived.service 取消开机自动启动
systemctl start keepalived.service 启动
systemctl stop keepalived.service停止

©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页