一 keepalive介绍

1.1 keepalived 是什么

keepalived 是集群管理中保证集群高可用的一个服务软件,用来防止单点故障。

1.2 keepalived 工作原理

keepalived 是以VRRP 协议为实现基础的,VRRP 全称VirtualRouter Redundancy Protocol,即虚拟路由冗余协议。虚拟路由冗余协议,可以认为是实现路由器高可用的协议,即将N 台提供相同功能的路由器组成一个路由器组,这个组里面有一个master 和多个backup,master 上面有一个对外提供服务的vip(该路由器所在局域网内其他机器的默认路由为该vip),master 会发组播,当backup 收不到vrrp 包时就认为master 宕掉了,这时就需要根据VRRP 的优先级来选举一个backup 当master。这样的话就可以保证路由器的高可用了。

keepalived 主要有三个模块,分别是core、check 和vrrp。

core 模块为keepalived 的核心,负责主进程的启动、维护以及全局配置文件的加载和解析。

check 负责健康检查,包括常见的各种检查方式。

vrrp 模块是来实现VRRP 协议的。

1.3 实验环境

master1:192.168.132.121

master2:192.168.132.122

虚拟IP:192.168.132.120

双主环境

master1

master1
mysql> show slave status\G;
*************************** . row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.132.122
Master_User: replication
Master_Port:
Connect_Retry:
Master_Log_File: master-bin.
Read_Master_Log_Pos:
Relay_Log_File: relay-log.
Relay_Log_Pos:
Relay_Master_Log_File: master-bin.
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno:
Last_Error:
Skip_Counter:
Exec_Master_Log_Pos:
Relay_Log_Space:
Until_Condition: None
Until_Log_File:
Until_Log_Pos:
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master:
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno:
Last_IO_Error:
Last_SQL_Errno:
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id:
Master_UUID: 662134df-a196-11e9-b432-000c2963fd11
Master_Info_File: /data/mysql/master.info
SQL_Delay:
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count:
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set: 662134df-a196-11e9-b432-000c2963fd11:-
Executed_Gtid_Set: 63a7f26f-a196-11e9-a2b2-000c2991dd19:-,
662134df-a196-11e9-b432-000c2963fd11:-
Auto_Position:
Replicate_Rewrite_DB:
Channel_Name:

master2

mysql> show slave status\G;
*************************** . row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.132.121
Master_User: replication
Master_Port:
Connect_Retry:
Master_Log_File: master-bin.
Read_Master_Log_Pos:
Relay_Log_File: relay-log.
Relay_Log_Pos:
Relay_Master_Log_File: master-bin.
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno:
Last_Error:
Skip_Counter:
Exec_Master_Log_Pos:
Relay_Log_Space:
Until_Condition: None
Until_Log_File:
Until_Log_Pos:
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master:
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno:
Last_IO_Error:
Last_SQL_Errno:
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id:
Master_UUID: 63a7f26f-a196-11e9-a2b2-000c2991dd19
Master_Info_File: /data/mysql/master.info
SQL_Delay:
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count:
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set: 63a7f26f-a196-11e9-a2b2-000c2991dd19:-
Executed_Gtid_Set: 63a7f26f-a196-11e9-a2b2-000c2991dd19:-,
662134df-a196-11e9-b432-000c2963fd11:-
Auto_Position:
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:

二 配置keepalive

2.1 开始安装keepalived

[root@master1 ~]# cd /usr/local/src/

[root@master1 src]# wget https://www.keepalived.org/software/keepalived-2.0.17.tar.gz

[root@master1 src]# tar -xf keepalived-2.0.17.tar.gz

[root@master1 src]# cd keepalived-2.0.17

[root@master1 keepalived-2.0.17]# yum install openssl* libnl‐dev* gcc-c++

[root@master1 keepalived-2.0.17]# ./configure --prefix=/usr/local/keepalived

[root@master1 keepalived-2.0.17]# make

[root@master1 keepalived-2.0.17]# make install

[root@master1 keepalived-2.0.17]# cd /usr/local/keepalived/sbin/

[root@master1 sbin]# ll

-rwxr-xr-x.  root root  Jul  : keepalived

[root@master1 sbin]# cp keepalived /usr/sbin/

[root@master1 sbin]# ll /usr/sbin/keepalived

-rwxr-xr-x.  root root  Jul  : /usr/sbin/keepalived

[root@master1 sbin]# systemctl list-unit-files|grep keepalived

[root@master1 sbin]# systemctl enable keepalived

[root@master1 sbin]# systemctl list-unit-files|grep keepalived

[root@master1 sbin]# mkdir /etc/keepalived

[root@master1 sbin]# cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/

[root@master1 sbin]# ll /etc/keepalived/

-rw-r--r--.  root root  Jul  : keepalived.conf 

2.2 配置keepailve

root@master1 keepalived-2.0.17]# vim /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {
router_id db01
} vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id
priority
nopreempt
advert_int
authentication {
auth_type PASS
auth_pass
}
virtual_ipaddress {
192.168.132.120
}
}
virtual_server 192.168.132.120 {
delay_loop
persistence_timeout
protocol TCP real_server 192.168.132.121 {
notify_down /etc/keepalived/kill_keepalived.sh
TCP_CHECK {
connect_timeout
nb_get_retry
connect_port
delay_before_retry
}
}
}

配置简介

global_defs 区域主要是配置故障发生时的通知对象以及机器标识
notification_email_from Alexandre.Cassen@firewall.loc //通知邮件从哪个地址发出
smtp_server 192.168.200.1 //通知邮件的smtp 地址
smtp_connect_timeout //连接smtp 服务器的超时时间
router_id db01 // 标识本节点的字符串, 通常为hostname
vrrp_skip_check_adv_addr
vrrp_strict
vrrp_garp_interval
vrrp_gna_interval
}
vrrp_instance VI_1 {//VRRP 实例state MASTER // MASTER 或BACKUP,为了防止脑裂现象,主备均需要设置为backup 模式,master 模式会抢占VIP
interface eth0 //节点固有IP(非VIP)的网卡,用来发VRRP 包
virtual_router_id //取值在0-255 之间,用来区分多个instance 的VRRP 组播priority 100 //优先级,同一个vrrp_instance 的MASTER优先级必须比BACKUP 高nopreempt //非抢占,配合backup,防止切换后,主库服务恢复正常后,IP 漂移过来advert_int 1// MASTER 与BACKUP 负载均衡器之间同步检查的时间间隔,单位为秒。
authentication {
auth_type PASS
auth_pass
}
virtual_ipaddress { //虚拟ip 地址,可以有多个地址,每个地址占一行,不需要子网掩码
192.168.132.120
}
}
virtual_server 192.168.200.16 {
delay_loop //健康检查间隔,单位为秒
persistence_timeout // 会话保持时间,就是把用户请求转发给同一个服务器
protocol TCP //转发协议,有TCP 和UDP 两种,一般用TCP
real_server 192.168.132.121 { //真实服务器,包括IP和端口号
notify_down kill_keepalived.sh
TCP_CHECK { #通过tcpcheck 判断RealServer 的健康状态
connect_timeout #连接超时时间
nb_get_retry #重连次数
delay_before_retry #重连时间间隔
connect_port #检测端口
}
}
}

keepalived 服务配置

#设置不抢占模式(DB1 设置即可)

nopreempt

#虚拟路由标识,这个标识是一个数字(1‐255),在一个VRRP

实例中主备服务器ID 必须一样

virtual_router_id 68

#优先级,数字越大优先级越高,在一个实例中主服务器优

先级要高于备服务器

priority 100 #从服务器99

#设置主备之间同步检查的时间间隔单位秒

advert_int 1

[root@master1 keepalived-2.0.17]# vim /etc/keepalived/kill_keepalived.sh

[root@master1 keepalived-2.0.17]# chmod +x /etc/keepalived/kill_keepalived.sh

#!/bin/bash
kill - $(cat /var/run/keepalived.pid)

[root@master1 keepalived-2.0.17]# systemctl start  keepalived

[root@master1 keepalived-2.0.17]# cat /var/run/keepalived.pid
16287

master2相同做法
配置文件

[root@master2 sbin]# cat /etc/keepalived/keepalived.conf

[root@master2 sbin]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived bal_defs {
router_id db02
} vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id
priority
nopreempt
advert_int
authentication {
auth_type PASS
auth_pass
}
virtual_ipaddress {
192.168.132.120
}
}
virtual_server 192.168.132.120 {
delay_loop
persistence_timeout
protocol TCP real_server 192.168.132.122 {
notify_down /etc/keepalived/kill_keepalived.sh
TCP_CHECK {
connect_timeout
nb_get_retry
connect_port
delay_before_retry
}
}
}

2.3 检查状态

[root@master2 ~]# systemctl status keepalived

[root@master2 ~]# ps -ef|grep keepalived

[root@master1 ~]# ip addr

: lo: <LOOPBACK,UP,LOWER_UP> mtu  qdisc noqueue state UNKNOWN qlen
link/loopback ::::: brd :::::
inet 127.0.0.1/ scope host lo
valid_lft forever preferred_lft forever
inet6 ::/ scope host
valid_lft forever preferred_lft forever
: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu qdisc pfifo_fast state UP qlen
link/ether :0c:::dd: brd ff:ff:ff:ff:ff:ff
inet 192.168.132.121/ brd 192.168.132.255 scope global ens33
valid_lft forever preferred_lft forever
inet 192.168.132.120/ scope global ens33 #虚拟IP地址
valid_lft forever preferred_lft forever
inet6 fe80::b469:::a47c/ scope link
valid_lft forever preferred_lft forever
: ens34: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu qdisc pfifo_fast state UP qlen
link/ether :0c:::dd: brd ff:ff:ff:ff:ff:ff
inet 192.168.43.85/ brd 192.168.43.255 scope global dynamic ens34
valid_lft 2810sec preferred_lft 2810sec
inet 192.168.254.121/ brd 192.168.254.255 scope global ens34
valid_lft forever preferred_lft forever
inet6 fe80::4d8::407a:5c0a/ scope link
valid_lft forever preferred_lft forever

master2查看

: lo: <LOOPBACK,UP,LOWER_UP> mtu  qdisc noqueue state UNKNOWN qlen
link/loopback ::::: brd :::::
inet 127.0.0.1/ scope host lo
valid_lft forever preferred_lft forever
inet6 ::/ scope host
valid_lft forever preferred_lft forever
: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu qdisc pfifo_fast state UP qlen
link/ether :0c:::fd: brd ff:ff:ff:ff:ff:ff
inet 192.168.132.122/ brd 192.168.132.255 scope global ens33
valid_lft forever preferred_lft forever
inet6 fe80::5d18:db0b::de34/ scope link
valid_lft forever preferred_lft forever
: ens34: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu qdisc pfifo_fast state UP qlen
link/ether :0c:::fd:1b brd ff:ff:ff:ff:ff:ff
inet 192.168.254.122/ brd 192.168.254.255 scope global ens34
valid_lft forever preferred_lft forever
inet6 fe80::c3d5::468a:94f8/ scope link
valid_lft forever preferred_lft forever

三 验证

授权后尝试使用192.168.132.20这个IP连接数据库

mysql> grant all on *.*  to root@192.168.132.123 identified by '';
mysql> flush privileges;

[root@slave ~]# mysql -ureplication -h192.168.132.120 -p1234567

mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| master1 |
| master2 |
| mysql |
| performance_schema |
| sys |
+--------------------+

关闭192.168.132.121的mysqld

[root@master1 ~]# systemctl stop mysqld

[root@master1 ~]# ip addr

: lo: <LOOPBACK,UP,LOWER_UP> mtu  qdisc noqueue state UNKNOWN qlen
link/loopback ::::: brd :::::
inet 127.0.0.1/ scope host lo
valid_lft forever preferred_lft forever
inet6 ::/ scope host
valid_lft forever preferred_lft forever
: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu qdisc pfifo_fast state UP qlen
link/ether :0c:::dd: brd ff:ff:ff:ff:ff:ff
inet 192.168.132.121/ brd 192.168.132.255 scope global ens33
valid_lft forever preferred_lft forever
inet6 fe80::b469:::a47c/ scope link
valid_lft forever preferred_lft forever
: ens34: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu qdisc pfifo_fast state UP qlen
link/ether :0c:::dd: brd ff:ff:ff:ff:ff:ff
inet 192.168.43.85/ brd 192.168.43.255 scope global dynamic ens34
valid_lft 3348sec preferred_lft 3348sec
inet 192.168.254.121/ brd 192.168.254.255 scope global ens34
valid_lft forever preferred_lft forever
inet6 fe80::4d8::407a:5c0a/ scope link
valid_lft forever preferred_lft forever

查看master2

[root@master2 ~]# ip addr

: lo: <LOOPBACK,UP,LOWER_UP> mtu  qdisc noqueue state UNKNOWN qlen
link/loopback ::::: brd :::::
inet 127.0.0.1/ scope host lo
valid_lft forever preferred_lft forever
inet6 ::/ scope host
valid_lft forever preferred_lft forever
: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu qdisc pfifo_fast state UP qlen
link/ether :0c:::fd: brd ff:ff:ff:ff:ff:ff
inet 192.168.132.122/ brd 192.168.132.255 scope global ens33
valid_lft forever preferred_lft forever
inet 192.168.132.120/ scope global ens33 #虚拟IP已经转移过来
valid_lft forever preferred_lft forever
inet6 fe80::5d18:db0b::de34/ scope link
valid_lft forever preferred_lft forever
: ens34: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu qdisc pfifo_fast state UP qlen
link/ether :0c:::fd:1b brd ff:ff:ff:ff:ff:ff
inet 192.168.254.122/ brd 192.168.254.255 scope global ens34
valid_lft forever preferred_lft forever
inet6 fe80::c3d5::468a:94f8/ scope link
valid_lft forever preferred_lft forever

使用slave连接

[root@slave ~]# mysql -uroot -h192.168.132. -p1234567
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| master1 |
| master2 |
| mysql |
| performance_schema |
| sys |
+--------------------+

依然可以访问,双主的高可用配置完成

在master1开启MySQL和keepalive后,虚拟IP并不会抢占回来,是因为设置的是非抢占模式,防止虚拟IP跳动影响性能

mysql主从之keepalive+MySQL高可用的更多相关文章

  1. Mysql双主互备+keeplived高可用架构介绍

    一.Mysql双主互备+keeplived高可用架构介绍 Mysql主从复制架构可以在很大程度保证Mysql的高可用,在一主多从的架构中还可以利用读写分离将读操作分配到从库中,减轻主库压力.但是在这种 ...

  2. Mysql双主互备+keeplived高可用架构(部分)

    一.Mysql双主互备+keeplived高可用架构介绍 Mysql主从复制架构可以在很大程度保证Mysql的高可用,在一主多从的架构中还可以利用读写分离将读操作分配到从库中,减轻主库压力.但是在这种 ...

  3. 利用keepalive+mysql replication 实现数据库的高可用

    利用keepalive+mysql replication 实现数据库的高可用 http://www.xuchanggang.cn/archives/866.html

  4. Docker mysql主主互备和高可用

    一.测试环境 1.1 结构图 1.2 版本 操作系统:CentOS 7.3 MySQL版本:5.6.35 Docker版本:18.06.1-ce 使用root用户操作 IP地址说明 IP地址 用途 备 ...

  5. MySQL主主复制+MMM实现高可用

    架构图: 环境规划 角色 IP VIP 用途 DB1 192.168.56.101 192.168.56.111/113 主Master提供wirte+read DB2 192.168.56.102 ...

  6. mysql mha 主从自动切换 高可用

    mha(Master High Availability)目前在MySQL多服务器(超过二台),高可用方面是一个相对成熟的解决方案. 一,什么是mha,有什么特性 1. 主服务器的自动监控和故障转移 ...

  7. mysql主从之LVS+keepalived+双主MySQL 负载均衡

    LVS(Linux Virtual Server)即Linux 虚拟服务器,是一个的开源负载均衡项目,目前LVS 已经被集成到Linux 内核模块中.LVS 是四层负载均衡,也就是说建立在OSI 模型 ...

  8. MySQL MGR+ Consul之数据库高可用方案

    背景说明:     基于目前存在很多MySQL数据库单点故障,传统的MHA,PXC等方案用VIP或者DNS切换的方式可以实现.基于数据库的数据强一致性考虑,采用MGR集群,采用consul服务注册发现 ...

  9. 【mysql】Mgr实现数据库高可用架构

    转载:https://www.cnblogs.com/luoahong/articles/8043035.html MGR简介 MySQL Group Replication(下简称:MGR)是MyS ...

随机推荐

  1. 二 virtualenv与virtualenvwrapper

    https://www.cnblogs.com/pyyu/p/9015317.html 一  virtualenv 1.下载virtualenvpip3 install -i https://pypi ...

  2. C# 局部函数与事件

    本文告诉大家使用局部函数可能遇到的坑. 在以前,如果有一个事件public event EventHandler Foo和一个函数private void Program_Foo(object sen ...

  3. 关于6410板文件的dm9000的平台设备地址

    转自csdn #define CONFIG_DM9000_BASE 0x20000300#define DM9000_IO                      0x20000000#define ...

  4. day6_python之pickle、shelve序列化和反序列化

    pickle.shelve,python私有,支持所有python数据类型 一.pickle dic={'name':'egon','age':18} print(pickle.dumps(dic)) ...

  5. Select multiple多选上移、下移

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD ...

  6. C#循环语句练习(二)

    1.求1!+2!+...+n! 所有阶乘的和 2.求100以内的质数 (1)第一种做法 (2)另一种做法 3. 总数=幼兔+小兔+成兔成兔=上个月的小兔+上个月的成兔小兔=上个月的幼兔幼兔=这个月的成 ...

  7. jekyll 添加 Valine 评论

    本文告诉大家如何在自己搭建的静态博客添加 Valine 评论.在这前,我基本都是使用 多说,但是多说gg啦,所以就在找一个可以替换的评论 本来 Disqus是很好的,但是在国内很难打开,所以我就需要一 ...

  8. H3C DHCP简介

  9. PHP mysqli扩展整理,包括面向过程和面向对象的比较\事务控制\批量执行\预处理

    相关文章:PHP的mysql扩展整理,操作数据库的实现过程分析  PHP PDO扩展整理,包括环境配置\基本增删改查\事务\预处理 介绍 mysqli是PHP程序与mysql数据库进行数据交互的桥梁, ...

  10. H3C查看历史命令--用户以上

    [H3Cwang]display history-command   sys   sysname H3C   sysname H3Cwang   display history-command   q ...