keepalived+MySQL实现高可用

keepalived+MySQL实现高可用,第1张

概述(一)keepalived概述 Keepalived通过VRRP(虚拟路由冗余协议)协议实现虚拟IP的漂移。当master故障后,VIP会自动漂移到backup,这时通知下端主机刷新ARP表,如果业务

 

 

(一)keepalived概述

Keepalived通过VRRP(虚拟路由冗余协议)协议实现虚拟IP的漂移。当master故障后,VIP会自动漂移到backup,这时通知下端主机刷新ARP表,如果业务是通过VIP连接到服务器的,则此时依然能够连接到正常运行的主机,RedHat给出的VRRP工作原理如下图:

 

本来对VIP漂移有一定了解的我,看了上面的图后,越来越懵了。因此只能根据我的个人理解,来对keepalived的VIP漂移做一个解释了,假设我现在有一套这样的环境:
主机A的IP地址为:192.168.10.11
主机B的IP地址为:192.168.10.12

我们再单独定义一个keepalived使用的VIP:192.168.10.10

当2台主机安装了keepalive并正常运行时,keepalive会选择一个节点做为主节点(这里假设为主机A,IP为192.168.10.11),由于A是主节点,所以主机A上还会生成一个IP地址192.168.10.10,即虚拟IP(Virtual IP,也称VIP),此时我们使用192.168.10.10访问主机,访问到的主机是A;假如A主机上的keepalived由于某些原因(例如服务器宕机、用户主动关闭…)关闭了,keepalived备用节点会检查与主节点keepalived的通信是否正常,检测到不正常,则会提升一个备节点为主节点,相应的虚拟IP也会在对应的主机上生成,从而实现高可用的目的。

 

(二)MysqL是如何结合keepalived实现高可用的

在MysqL中,通过搭建MysqL双主复制,保持2台主机上的MysqL数据库一模一样,并在2台主机上安装keepalived软件,启用VIP,用户应用程序通过VIP访问数据库。当包含VIP的主机上的数据库发生故障时,关闭keepalived,从而将VIP漂移到另一个节点,用户依然可以正常访问数据库。 (这里需要注意,虽然MysqL架构双主复制,2个节点都可以写入数据,但是我们在使用的时候,是通过VIP访问其中一个实例,并没有2个数据库实例一起使用)。这里我简单画了一个流程图,来说明keepalive与MysqL实现高可用的过程:

 

(三)keepalived+MysqL实现高可用过程实现

基础环境规划:

 主机名IP地址备注
服务器Ahosta192.168.10.11keepalive主节点
服务器Bhostb192.168.10.12keepalive备节点
  192.168.10.10虚拟IP,会在keepalive启动后分配到上面2台机器的主节点上

 

(3.1)搭建MysqL双主复制环境

STEP1:安装MysqL过程见:https://www.cnblogs.com/lijiaman/p/10743102.html

STEP2:配置双主复制参数

服务器A服务器B

[MysqLd]
basedir=/usr/local/MysqL
datadir=/MysqL/data

server_ID = 1
binlog_format=ROW
log_bin=/MysqL/binlog/master-bin
auto-increment-increment = 2            #字段变化增量值
auto-increment-offset = 1               #初始字段ID为1
slave-skip-errors = all                 #忽略所有复制产生的错误
gtID_mode=ON
enforce-gtID-consistency=ON

 

[MysqLd]
basedir=/usr/local/MysqL
datadir=/MysqL/data

server_ID = 2
binlog_format=ROW
log_bin=/MysqL/binlog/master-bin
auto-increment-increment = 2            #字段变化增量值
auto-increment-offset = 2               #初始字段ID为2
slave-skip-errors = all                 #忽略所有复制产生的错误
gtID_mode=ON
enforce-gtID-consistency=ON

STEP3:创建复制用户,2个数据库上都要创建

grant replication slave on *.* to 'rep'@%' IDentifIEd by 123';

STEP4:将hosta的数据拷贝到hostb,并应用

[root@hostb ~]# MysqLdump -uroot -p123456 -h 192.168.10.11 --single-transaction --all-databases --master-data=2  > hosta.sql]# MysqL < hosta.sql

STEP5:hostb上开启复制,以下脚本在hostb上执行

配置复制MysqL> CHANGE MASTER TO -> master_host=192.168.10.11',-> master_port=3306-> master_user-> master_password-> MASTER_auto_position = 1;query OK,1); Font-weight: bold">0 rows affected,2 warnings (0.01 sec) 开启复制MysqL> start slave; query OK,1); Font-weight: bold">0 rows affected (0.00 sec) 查看复制状态MysqL show slave status \G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 10.11 Master_User: rep Master_Port: Connect_Retry: 60 Master_Log_file: master-bin.000001 Read_Master_Log_Pos: 322 Relay_Log_file: hostb-relay000002 Relay_Log_Pos: 417 Relay_Master_Log_file: master Slave_IO_Running: Yes Slave_sql_Running: Yes

STEP6:hosta上开启复制,以下脚本在hosta上执行

MysqLTO    ->    master_host192.168.10.12->    master_port->    master_user->    master_password->    MASTER_auto_position  sec)MysqL start slave;query OK,1)"> show slave status \G;               Slave_IO_State: Waiting  send event                  Master_Host: 10.12                  Master_User: rep                  Master_Port:                 Connect_Retry:               Master_Log_file: master          Read_Master_Log_Pos: 154               Relay_Log_file: hosta                Relay_Log_Pos: 369        Relay_Master_Log_file: master             Slave_IO_Running: Yes            Slave_sql_Running: Yes

STEP7:测试双主复制

在hosta上创建数据库testdb,到hostb服务器上查看数据库是否已经创建

hosta上创建数据库create database testdb;hostb上查看数据库,发现已经创建MysqL show databases; +------------------+ | Database | | information_schema | db1 | lijiamandb | MysqL | performance_schema | sbtest | sys | | testdb 8 rows in set (0.01 sec)

在hostb的testdb数据库上创建表t1,并插入数据,到hosta上查看是否复制过来

在hostb上创建表并插入数据MysqL> use testdbDatabase changedMysqLtable t1(ID int,name varchar(20));query OK,1)">insert into t1 values(1,a);query OK,1); Font-weight: bold">1 row affected ( 在hosta上查看数据,数据已经过来MysqLselect from testdb.t1;----+------+| ID | name | 1 | a 1 row 0.00 sec)

到这,双主复制已经搭建完成,接下来安装配置keepalived。

 

(3.2)安装配置keepalived

(3.2.1)keepalived的安装与管理

keepalived可以使用源码安装,也可以使用yum在线安装,这里直接使用yum在线安装:

root@hosta data]# yum install -y keepalived

使用如下命令查看安装路径:

[root@hosta data]# rpm -ql keepalived/etc/keepalived/etc/keepalived/keepalived.conf/etc/sysconfig/keepalived/usr/bin/genhash/usr/lib/systemd/system/keepalived.service/usr/libexec/keepalived/usr/sbin/keepalived/usr/share/doc/keepalived-1.3.5
… 略

 

使用如下命令管理keepalived

# 开启keepalivedsystemctl start keepalived 或者 service keepalived start # 关闭keepalivedsystemctl stop keepalived 或者 service keepalived stop # 查看keepalived运行状态systemctl status keepalived 或者 service keepalived status# 重新启动keepalivedsystemctl restart keepalived 或者 service keepalived restart

 

(3.2.2)keepalived的配置

keepalived的配置文件为:/etc/keepalived/keepalived.conf,我的配置文件如下:

【hosta主机的配置文件】

[root@hosta keepalived]# cat keepalived.conf! Configuration file for keepalived       global_defs {notification_email {[email protected]@wangshibo.cn}       notification_email_from [email protected]_server 127.0.0.1 smtp_connect_timeout 30router_ID MASTER-HA}       vrrp_script chk_MysqL_port {       #检测MysqL服务是否在运行。有很多方式,比如进程,用脚本检测等等    script "/MysqL/chk_MysqL.sh"   #这里通过脚本监测    interval 2                     #脚本执行间隔,每2s检测一次    weight –5                      
#脚本结果导致的优先级变更,检测失败(脚本返回非0)则优先级 -5
    fall 2                         
#检测连续2次失败才算确定是真失败。会用weight减少优先级(1-255之间)
    rise 1                         #检测1次成功就算成功。但不修改优先级}       vrrp_instance VI_1 {    state BACKUP                  #这里所有节点都定义为BACKUP    interface ens34               #指定虚拟ip的网卡接口    mcast_src_ip 192.168.10.11    #本地IP     virtual_router_ID 51          #路由器标识,MASTER和BACKUP必须是一致的    priority 101                  #定义优先级,数字越大,优先级越高,在同一个vrrp_instance下,MASTER的优先级必须大于BACKUP的优先级。     advert_int     nopreempt                     #不抢占模式,在优先级高的机器上设置即可,优先级低的机器可不设置             authentication {           auth_type PASS         auth_pass 1111         }    virtual_ipaddress {            10.10             #虚拟IP     }      track_script {                  chk_MysqL_port             }}

 

【hostb主机的配置文件】

[root@hostb keepalived]# HA}       vrrp_script chk_MysqL_port {    script     interval                 weight -5                     fall                      rise                }       vrrp_instance VI_1 {    state BACKUP    interface ens34     mcast_src_ip 10.12        priority 99              advert_int              authentication {           auth_type PASS         auth_pass 10.10    }      track_script {                  chk_MysqL_port             }}

需要特别注意:nopreempt这个参数只能用于state为BACKUP的情况,所以在配置的时候要把master和backup的state都设置成BACKUP,这样才会实现keepalived的非抢占模式!

 

【判断MysqL数据库允许状态的文件】

[root@hosta ~]# cat /MysqL/chk_MysqL.sh #!/bin/bashcounter=$(netstat -na|grep ListEN"|3306wc -l)if [ ${counter}" -eq 0 ]; then    systemctl stop keepalivedfi  

 

 

 

 

 

在配置完成之后,启动MysqL数据库和keepalive,需要注意,先启动MysqL,再启动keepalive,因为keepalive启动后会检测MysqL的运行状态,如果MysqL运行异常,keepalive会自动关闭。

 

(3.3)高可用测试

时间轴

(时间递增)

hosta *** 作hostb *** 作
时间1

# 数据库运行正常
[root@hosta ~]# service MysqLd status
  SUCCESS! MysqL running (8530)

#keepalived运行正常
[root@hosta ~]# service keepalived status
    Active: active (running) since Mon 2020-08-03 22:29:10 CST; 1min 32s ago

# 此时虚拟IP在hosta上
[root@hosta ~]# ip addr
3: ens34: <broADCAST,MulTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
     link/ether 00:0c:29:fb:62:0a brd ff:ff:ff:ff:ff:ff
     inet 192.168.10.11/24 brd 192.168.10.255 scope global ens34
        valID_lft forever preferred_lft forever
     inet 192.168.10.10/32 scope global ens34
        valID_lft forever preferred_lft forever
     inet6 fe80::20c:29ff:fefb:620a/64 scope link
        valID_lft forever preferred_lft forever

# 数据库运行正常

[root@hostb ~]# service MysqLd status
  SUCCESS! MysqL running (8022)
 

#keepalived运行正常

[root@hostb ~]# service keepalived status
    Active: active (running) since Mon 2020-08-03 22:31:09 CST; 8s ago

[root@hostb ~]# ip addr
3: ens34: <broADCAST,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
     link/ether 00:0c:29:92:3d:5a brd ff:ff:ff:ff:ff:ff
     inet 192.168.10.12/24 brd 192.168.10.255 scope global ens34
        valID_lft forever preferred_lft forever
     inet6 fe80::20c:29ff:fe92:3d5a/64 scope link
        valID_lft forever preferred_lft forever

时间2

# 通过VIP访问数据库,访问到的都是hosta上的实例
[root@hosta keepalived]# MysqL -uroot -p123456 -h192.168.10.10 -e "select @@hostname"
MysqL: [Warning] Using a password on the command line interface can be insecure.
+------------+
| @@hostname |
+------------+
| hosta      |
+------------+

# 通过VIP访问数据库,访问到的都是hosta上的实例
[root@hostb ~]# MysqL -uroot -p123456 -h192.168.10.10 -e "select @@hostname"
  MysqL: [Warning] Using a password on the command line interface can be insecure.
  +------------+
  | @@hostname |
  +------------+
  | hosta      |
  +------------+

 

时间3

# 主节点关闭MysqL
[root@hosta keepalived]# service MysqLd stop
Shutting down MysqL............ SUCCESS!

 

 
时间4

# keepalived检测到MysqL关闭后,会自动关闭
[root@hosta keepalived]# service keepalived status
    Active: inactive (dead)

# 此时VIP已经不存在
[root@hosta keepalived]# ip addr
3: ens34: <broADCAST,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
     link/ether 00:0c:29:fb:62:0a brd ff:ff:ff:ff:ff:ff
     inet 192.168.10.11/24 brd 192.168.10.255 scope global ens34
        valID_lft forever preferred_lft forever
     inet6 fe80::20c:29ff:fefb:620a/64 scope link
        valID_lft forever preferred_lft forever

# hostb上的keepalived运行正常
[root@hostb ~]# service keepalived status
    Active: active (running) since Mon 2020-08-03 22:31:09 CST; 6min ago

# 发现VIP已经切换到了hostb
[root@hostb ~]#  ip addr
3: ens34: <broADCAST,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
     link/ether 00:0c:29:92:3d:5a brd ff:ff:ff:ff:ff:ff
     inet 192.168.10.12/24 brd 192.168.10.255 scope global ens34
        valID_lft forever preferred_lft forever
     inet 192.168.10.10/32 scope global ens34
        valID_lft forever preferred_lft forever
     inet6 fe80::20c:29ff:fe92:3d5a/64 scope link
        valID_lft forever preferred_lft forever

时间5

hostb      |
+------------+

# 通过VIP访问数据库,访问到的都是hosta上的实例
[root@hostb ~]# MysqL -uroot -p123456 -h192.168.10.10 -e "select @@hostname"
MysqL: [Warning] Using a password on the command line interface can be insecure.
+------------+
| @@hostname |
+------------+
| hostb      |
+------------+

时间6

# 重启MysqL
[root@hosta keepalived]# service MysqLd start
Starting MysqL. SUCCESS!

# 重启keepalived
[root@hosta keepalived]# service keepalived start
Redirecting to /bin/systemctl start keepalived.service
[root@hosta keepalived]#
[root@hosta keepalived]# service keepalived status
Redirecting to /bin/systemctl status keepalived.service
    Active: active (running) since Mon 2020-08-03 22:40:08 CST; 6s ago

# 因为使用了非抢占模式,VIP不会漂回来
# 如果要把VIP漂回来,可以关闭hostb上的数据库或者直接关闭hostb服务器

[root@hosta keepalived]# ip addr
3: ens34: <broADCAST,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
     link/ether 00:0c:29:fb:62:0a brd ff:ff:ff:ff:ff:ff
     inet 192.168.10.11/24 brd 192.168.10.255 scope global ens34
        valID_lft forever preferred_lft forever
     inet6 fe80::20c:29ff:fefb:620a/64 scope link
        valID_lft forever preferred_lft forever

 

 

 

【完】

总结

以上是内存溢出为你收集整理的keepalived+MySQL实现高可用全部内容,希望文章能够帮你解决keepalived+MySQL实现高可用所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/1152602.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存