概述
Keepalived+Haproxy+Mysql(双主)高可用架构部署
一、背景
公司原部署的Mysql架构为keepalived+Mysql双主,但是这个架构有个缺陷是所有的读写请求都在一台机器上(VIP所在机器),造成另一台机器空闲,导致资源浪费,为了解决这个问题,所以需要往Keepalived和mysql之间增加一个haproxy负载均衡插件,将读写请求均衡分布到两台机器上。
二、安装keepalived心跳 + Mysql双主架构(原有架构)
参考:
1、快速编译安装mysql
2、构建主主(互为主从)同步关系
3、搭建keepalived双机热备
三、安装Haproxy(增加负载均衡插件)
Haproxy下载地址:Index of /repo/pkgs/haproxy
[root@CS-mysql02 soft]# cd /data/soft/
[root@CS-mysql02 soft]# ll
总用量 2744
-rw-r--r-- 1 root root 2057051 11月 11 15:55 haproxy-1.8.9.tar.gz
drwxrwxr-x 9 root root 4096 2月 21 2021 keepalived-1.4.3
-rw-rw-r-- 1 root root 744680 2月 21 2021 keepalived-1.4.3.tar.gz
[root@CS-mysql02 soft]# tar xf haproxy-1.8.9.tar.gz
[root@CS-mysql02 soft]# ll
总用量 2744
drwxrwxr-x 10 root root 295 5月 18 2018 haproxy-1.8.9
-rw-r--r-- 1 root root 2057051 11月 11 15:55 haproxy-1.8.9.tar.gz
drwxrwxr-x 9 root root 4096 2月 21 2021 keepalived-1.4.3
-rw-rw-r-- 1 root root 744680 2月 21 2021 keepalived-1.4.3.tar.gz
[root@CS-mysql02 soft]# cd haproxy-1.8.9/
[root@CS-mysql02 soft]# make TARGET=linux2628 PREFIX=/usr/local/haproxy
[root@CS-mysql02 soft]# make install PREFIX=/usr/local/haproxy
[root@CS-mysql02 soft]# /usr/local/haproxy/sbin/haproxy -v
[root@CS-mysql02 soft]# useradd -r -s /sbin/nologin haproxy
[root@CS-mysql02 soft]# mkdir /etc/haproxy
[root@CS-mysql02 soft]# cd /etc/haproxy/
[root@CS-mysql02 soft]# vim haproxy.cfg
global
log 127.0.0.1 local0 info
user haproxy
group haproxy
daemon
nbproc 2
pidfile /usr/local/haproxy/haproxy.pid
defaults
mode http
retries 3
option redispatch
timeout http-request 10s
timeout connect 10s
timeout client 20s
timeout server 30s
timeout check 5s
frontend mysql-front
bind *:3307
mode tcp
default_backend mysql-backup
backend mysql-backup
mode tcp
balance roundrobin
server db1 10.0.8.31:3306 weight 1 check inter 2000 rise 2 fall 3
server db2 10.0.8.32:3306 weight 1 check inter 2000 rise 2 fall 3
listen admin_stats
bind 0.0.0.0:9188
mode http
bind-process 2
log 127.0.0.1 local0 err
stats refresh 5s
stats uri /haproxy-status
stats realm welcome login Haproxy
stats auth haproxy:haproxy
[root@CS-mysql02 soft]# /usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg
[root@CS-mysql02 soft]# ps -ef | grep haproxy
haproxy 4509 1 0 16:01 ? 00:00:00 /usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg
haproxy 4510 1 0 16:01 ? 00:00:00 /usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg
root 4522 31394 0 16:01 pts/0 00:00:00 grep --color=auto haproxy
四、高可用设计
1、修改 keepalived.conf配置文件
两个keepalived配置文件都要修改,我这里只修改一台,另外一台除了
router_id、state、priority三个值不一样,其他全部相同,依照自己情况而定
[root@CS-mysql02 scripts]# vim /etc/keepalived/keepalived.conf
global_defs {
router_id lbs_MySQL_N1
}
vrrp_script chk_servers {
script "/scripts/keepalived_mysql.sh"
interval 5
weight 2
}
vrrp_instance VI_1 {
state BACKUP
nopreempt
interface eth0
virtual_router_id 42
priority 100
garp_master_delay 3
advert_int 1
authentication {
auth_type PASS
auth_pass nsw_lbs_MySQL
}
track_script {
chk_servers
}
virtual_ipaddress {
10.0.8.33
}
# notify_master /scripts/keepalived_master.sh
notify_backup "/etc/init.d/mysqld restart"
# notify_stop /scripts/keepalived_stop.sh
}
其实这里的关键部分就是如何做到高可用设计,上面的设计思路如下:
keepalived实时检测 /scripts/keepalived_mysql.sh脚本,当发现mysql挂掉后,就会重启keeplived服务,这时vip切换到另外一台mysql机器,此时mysql成为backup,同时执行notify_backup中的命令,重启mysql数据库,这样做的好处就是不会导致双主挂掉,导致丢数据的情况(这里担心的是如果mysql挂掉而长时间没有恢复的话,就会造成部分数据库丢失),如果是haproxy挂掉,则直接停掉keepalived,vip切换到另外一台机器,主从依然正常,影响不大,后续自己手动重启就可以了。
2、到/scripts目录下增加 keepalived_mysql.sh 检测脚本
[root@CS-mysql02 scripts]# cd /scripts/
[root@CS-mysql02 scripts]# ll
总用量 20
-rwxr-xr-x 1 yukw yukw 237 11月 11 16:17 keepalived_mysql.sh
[root@CS-mysql02 scripts]# cd /scripts/
[root@CS-mysql02 scripts]# vim keepalived_mysql.sh
#!/bin/bash
source /etc/profile
mysql_process=$(ps -C mysqld --no-header | wc -l)
haproxy_process=$(ps -C haproxy --no-header | wc -l)
if [ $mysql_process -eq 0 ]; then
/etc/init.d/keepalived restart
fi
if [ $haproxy_process -eq 0 ];then
/etc/init.d/keepalived stop
fi
3、重启keepalived服务
[root@CS-mysql02 scripts]# /etc/init.d/keepalived restart
Restarting keepalived (via systemctl): [ 确定 ]
附加
关于keepalived脑裂现象(双方都获取vip现象),可能是存在组播被禁用现象
组播和单播的配置
【keepalived】关于keepalived配置中的 mcast_src_ip 和 unicast_src_ip - 简书
好了,这就是Keepalived+Haproxy+Mysql高可用架构部署的方法了,如有问题可与博主一起交流讨论!
最后
以上就是有魅力缘分为你收集整理的Keepalived+Haproxy+Mysql(双主)高可用架构部署Keepalived+Haproxy+Mysql(双主)高可用架构部署附加 的全部内容,希望文章能够帮你解决Keepalived+Haproxy+Mysql(双主)高可用架构部署Keepalived+Haproxy+Mysql(双主)高可用架构部署附加 所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复