针对目前公司的数据库架构要做统计规划和调整,所以针对不同的业务环境要选择合适的架构就比较重要,同时作为运维人员又要考虑到维护的便捷性(不能做过多架构维护),最终停留在mha+vip/主从+vip/mycat集群/域名+主从这几种架构或组合,这里就分布对不同架构部署做下记录并对比起优劣,针对不同场景做出合适的选择。
本文主要针对主备自动切换,即VIP+主从方式,其中VIP通过keepalived控制。
一、环境描述
服务器IP
操作系统
数据库
硬件配置
备注
192.168.1.23
RHEL6.6_x86_64
MySQL 5.7.21
内存:16GB
CPU:4cores
硬盘:100GB
VIP:192.168.1.27
Keepalived:1.4.4
192.168.1.24
RHEL6.6_x86_64
MySQL 5.7.21
内存:16GB
CPU:4cores
硬盘:100GB
二、业务场景
1、优点
1)部署简单,管理方便。
2)可实现主备库VIP切换,对业务无感知。
2、缺点
1)存在脑裂风险
2)存在切换主备,状态无检测风险
3)跨VLAN不支持
4)看似部署简单,实则需要考虑的点非常多,如果考虑不完善建议慎用
3、业务场景
总而言之,此架构不推荐使用,使用时需谨慎。
1)双节点数据库服务器,仅仅为了实现主备VIP切换业务无感知
2)多个从节点,通过LVS,做负载均衡(现在不推荐这种架构实现负载均衡)
3)两节点的双向复制可以降低部分风险(来回切换VIP、脑裂等),单无法避免
三、数据库安装和主从部署
略
四、keepalived安装部署
1.keepalived下载
下载地址:
2.keepalived安装
主库:
[root@node1 tools]# tar -xzvf keepalived-1.4.4.tar.gz
[root@node1 tools]# cd keepalived-1.4.4
[root@node1 keepalived-1.4.4]# ./configure --prefix=/usr/local/keepalived
[root@node1 keepalived-1.4.4]# make -j 4
[root@node1 keepalived-1.4.4]# make install
[root@node1 keepalived-1.4.4]# cp -rp ./keepalived/etc/init.d/keepalived /etc/init.d/
[root@node1 keepalived-1.4.4]# chkconfig --add keepalived
[root@node1 keepalived-1.4.4]# mkdir /etc/keepalived
[root@node2 keepalived-1.4.4]# cp -rp ./bin/keepalived /usr/bin/
[root@node1 keepalived-1.4.4]# cp ./keepalived/etc/keepalived/keepalived.conf /etc/keepalived
[root@node1 keepalived-1.4.4]# cp -rp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
[root@node1 keepalived-1.4.4]# service keepalived status
keepalived is stopped
备库:
安装keepalived同上
###备注
*** WARNING - this build will not support IPVS with IPv6. Please install libnl/libnl-3 dev libraries to support IPv6 with IPVS.
###
3.keepalived配置
主库配置:
[root@node1 keepalived-1.4.4]# cp -rp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf_bak
[root@node1 keepalived-1.4.4]# >/etc/keepalived/keepalived.conf
[root@node1 keepalived-1.4.4]# vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
rangle@163.com
}
notification_email_from rangle@163.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id Node_Master
}
vrrp_instance VI_1 {
state BACKUP ##可配置master和backup模式,为了防止脑裂现象,主备均需要设置为backup模式,master模式会抢占VIP
interface eth1
virtual_router_id 43 ##VRRP组名,两个节点的设置必须一样,以指明各个节点属于同一VRRP组
priority 100
nopreempt ##配合backup,防止切换后,主库服务恢复正常后,IP漂移过来
advert_int 1 ###组播信息发送间隔,两个节点设置必须一样
authentication { ###设置验证信息,两个节点必须一致
auth_type PASS
auth_pass 1111
}
virtual_ipaddress { ###指定虚拟IP, 两个节点设置必须一样
192.168.1.27 label eth1:1
}
}
virtual_server 192.168.1.27 3306 {
delay_loop 6
lb_algo wrr #lvs调度算法rr|wrr|lc|wlc|lblc|sh|dh
lb_kind DR #负载均衡转发规则NAT|DR|RUN
persistence_timeout 50
protocol TCP