一.體系架構
在Keepalived + amoeba高可用負載均衡架構中,keepalived負責實現High-availability (HA) 功能控制前端機VIP(虛擬網絡地址),當有設備發生故障時,熱備服務器可以瞬間將VIP自動切換過來,實際運行中體驗只有2秒鐘切換時間,
通過amoeba mysql中間件對master和slave進行讀寫分離,主master負責寫,兩臺slave負責讀,備master負責實時同步主master的數據
二. 優點
- 讀寫分離
- mysql slave支持橫向擴展,提高性能
- 數據庫內容支持定期備份,在發生重大操作失誤后可以進行回退操作
- mysql master熱備,能在master出現問題后自動切換到另一臺備份master上繼續使用
三. 系統環境
兩臺負載機器安裝:centos7.2+docker+amoeba+keepalived,分別命名為:MYSQL_MASTER,MYSQL_BACKUP。
多臺mysql slave,這里使用兩臺
服務器 操作系統 IP地址 安裝軟件
MYSQL_MASTER Centos 7.2 64位 10.141.1.31 docker+amoeba+keepalived
MYSQL_BACKUP Centos 7.2 64位 10.141.1.5 docker+amoeba+keepalived
MYSQL_SLAVE_1 Centos 7.2 64位 10.141.1.12 docker+mysql
MYSQL_SLAVE_2 Centos 7.2 64位 10.141.1.25 docker+mysql
虛擬IP 10.141.1.8
四. 搭建環境
1. 主機準備
全部主機執行命令
setenforce 0 #關閉selinux
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
systemctl stop firewalld #關閉防火墻
systemctl stop iptables #關閉iptables
2. docker安裝(全部主機執行命令)
a. 在線安裝
參考: (https://docs.docker.com/install/linux/docker-ce/centos/#uninstall-old-versions)
yum install docker
b. 離線二進制安裝:
參考:(http://www.lxweimin.com/p/46b9a351f749)
3. 安裝mysql(全部安裝)
a. 拉取鏡像
docker pull mysql:5.7
b. 修改mysql配置文件,vim mysqld.cnf
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
#log-error = /var/log/mysql/error.log
# By default we only accept connections from localhost
#bind-address = 127.0.0.1
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
log-bin=mysql-bin
server_id = 11 #備份寫12,不能一樣
auto-increment-offset = 1 #備份mysql填寫0
auto-increment-increment = 2
c. 啟動mysql
docker run --name mysql -e MYSQL_ROOT_PASSWORD=1234 -p 3306:3306 -v /${PWD}/mysqld.cnf:/etc/mysql/mysql.conf.d/mysqld.cnf -d mysql:5.7
4. 設置mysql雙主多從
a. 賦權
(1)master給backup和兩個slave增加復制賬號
grant replication slave on *.* to 'repl'@'10.141.1.5' identified by '123456';
grant replication slave on *.* to 'repl'@'10.141.1.12' identified by '123456';
grant replication slave on *.* to 'repl'@'10.141.1.25' identified by '123456';
flush privileges;
(2)backup給master和兩個slave增加復制賬號
grant replication slave on *.* to 'repl'@'10.141.1.31' identified by '123456';
grant replication slave on *.* to 'repl'@'10.141.1.12' identified by '123456';
grant replication slave on *.* to 'repl'@'10.141.1.25' identified by '123456';
flush privileges;
(3)設置backup同步master
show master status; #在master執行,記住File和Position 值
change master to master_host='10.141.1.31',master_port=3306,master_user='repl',master_password='123456',master_log_file='mysql-bin.000003',master_log_pos=1207; #在backup執行,master_log_file為上面的File值,master_log_pos為Position 值
start slave; #啟動
show slave status\G; 查看Slave_IO_Running和Slave_SQL_Running兩個值為yes即成功
(4)設置master同步backup
show master status; #在backup執行,記住File和Position 值
change master to master_host='10.141.1.5',master_port=3306,master_user='repl',master_password='123456',master_log_file='mysql-bin.000003',master_log_pos=1050; #在master執行,master_log_file為上面的File值,master_log_pos為Position 值
start slave; #啟動
show slave status\G; 查看Slave_IO_Running和Slave_SQL_Running兩個值為yes即成功
(5)設置slave同步master
show master status; #在master執行,記住File和Position 值
change master to master_host='10.141.1.8',master_port=3306,master_user='repl',master_password='123456',master_log_file='mysql-bin.000003',master_log_pos=1207; #在backup執行,master_log_file為上面的File值,master_log_pos為Position 值
start slave; #啟動
show slave status\G; 查看Slave_IO_Running和Slave_SQL_Running兩個值為yes即成功
5. 讀寫分離
a. 分配amoeba賬號,全部執行
grant all on *.* to 'amoeba'@'%' identified by '123';
b. 啟動amoeba服務
docker run --name amoeba -it -d -p 8066:8066 -v /${PWD}/conf:/usr/local/amoeba/conf commanderhu/amoeba
啟動后修改conf下的amoeba.xml ,dbServers.xml 文件,然后重啟
docker restart amoeba
c. 測試
mysql -uamoeba -p123 -P8066 -h10.141.1.8
show databases;
6. keepalived安裝配置
參考(http://www.lxweimin.com/p/72dfd4d4cf9bc)
master配置
! Configuration File for keepalived
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.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 chk_mysql_port { #檢測mysql服務是否在運行。有很多方式,比如進程,用腳本檢測等等
script "/root/chk_mysql.sh" #這里通過腳本監測
interval 2 #腳本執行間隔,每2s檢測一次
weight -10 #腳本結果導致的優先級變更,檢測失敗(腳本返回非0)則優先級 -5
fall 2 #檢測連續2次失敗才算確定是真失敗。會用weight減少優先級(1-255之間)
rise 1 #檢測1次成功就算成功。但不修改優先級
}
vrrp_instance VI_1 {
state MASTER
#nopreempt
notify_master /root/to_master.sh
unicast_src_ip 10.141.1.29
unicast_peer {
10.141.1.24
}
interface eth0
virtual_router_id 58
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.141.1.8
}
track_script {
chk_mysql_port
}
}
slave配置
! Configuration File for keepalived
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.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 chk_mysql_port { #檢測mysql服務是否在運行。有很多方式,比如進程,用腳本檢測等等
script "/root/chk_mysql.sh" #這里通過腳本監測
interval 2 #腳本執行間隔,每2s檢測一次
weight -10 #腳本結果導致的優先級變更,檢測失敗(腳本返回非0)則優先級 -5
fall 2 #檢測連續2次失敗才算確定是真失敗。會用weight減少優先級(1-255之間)
rise 1 #檢測1次成功就算成功。但不修改優先級
}
vrrp_instance VI_1 {
state BACKUP
notify_master /root/to_master.sh
#nopreempt
unicast_src_ip 10.141.1.24
unicast_peer {
10.141.1.29
}
interface eth0
virtual_router_id 58
priority 95
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.141.1.8
}
track_script {
chk_mysql_port
}
}
slave變master執行的腳本
re=`mysql -h10.141.1.29 -uroot -p1234 -e "show master status;" | grep -v File`
echo $re
binlog=`echo $re | awk '{print $1}'`
position=`echo $re | awk '{print $2}'`
echo $binlog ------ $position
mysql -h10.141.1.30 -uroot -p1234 -e "stop slave;change master to master_host='10.141.1.8',master_port=3306,master_user='repl',master_password='123456',master_log_file='$binlog',master_log_pos=$position;start slave;show slave status\G;"
mysql -h10.141.1.4 -uroot -p1234 -e "stop slave;change master to master_host='10.141.1.8',master_port=3306,master_user='repl',master_password='123456',master_log_file='$binlog',master_log_pos=$position;start slave;show slave status\G;"
檢查腳本
counter=$(netstat -na|grep "LISTEN"|grep "3306"|wc -l)
if [ "${counter}" -eq 0 ]; then
#systemctl stop keepalived
exit 1
fi