table of Contents
Introduction to Alibaba Cloud SLB
Background introduction
Recently, it is necessary to deploy MySQL high-availability architecture on Alibaba Cloud ECS, because Keepalived cannot be used to provide VIP (virtual IP) on Alibaba Cloud, and failover cannot be automatically completed without VIP. After investigation, it is found that Alibaba Cloud product SLB can roughly meet the demand. SLB Traffic distribution can be performed to eliminate single points of failure and achieve high service availability. So I plan to use Alibaba Cloud product SLB (Load Balancing) instead of VIP. This blog is mainly to verify whether the write and query requests of the database can be automatically transferred to the standby database if the main database service is down in the SLB active/standby mode. If possible, the later ECS self-built high availability solution is to use SLB+MHA to achieve high availability of the main library.
SLB Alibaba Cloud official introduction
Introduction to Alibaba Cloud SLB
Server Load Balancer is a load balancing service that distributes traffic to multiple cloud servers. Load balancing can expand the external service capabilities of the application system through traffic distribution, and improve the availability of the application system by eliminating single points of failure.
note:
When creating an SLB, you need to use a group of active and standby servers. The primary server corresponds to the primary database server in the MHA, and the standby server corresponds to the standby master in the MHA, so that the usual traffic will be sent to the primary database, and when a failure occurs, the traffic will be sent. To the standby master. For the rest, you can consult the company’s OP colleagues or Alibaba Cloud’s technical staff.
Environment introduction
IP | Role in SLB | Roles in the database architecture (MHA) | Abbreviation |
---|---|---|---|
10.96.0.202 | SLB address | VIP | 202 |
10.96.1.114 | Main server | Main library server | 4 |
10.96.1.115 | Standby server | Standby master server | 5 |
10.96.1.116 | Not added to SLB | Slave library server | 6 |
Create test table
Table structure description: add a column of timestamp to the test table, which is convenient to record the insertion time and switch log for comparison. The other field is of num type, and you can use the for loop to continuously insert data into the table.
create database test;
use test;
CREATE TABLE `hatest` (
`ID` BIGINT (20) NOT NULL AUTO_INCREMENT,
num BIGINT (20) NOT NULL,
`CREATE_TIME` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`UPDATE_TIME` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
PRIMARY KEY (`ID`)
) ENGINE = INNODB AUTO_INCREMENT = 1 DEFAULT CHARSET = utf8 ROW_FORMAT = DYNAMIC COMMENT = '用户表';
Test script preparation
Install dependent packages:
pip install PyMySQL
# Author admin
# DATE 2020/9/10
# -*- coding:UTF-8 -*-
import pymysql
import time,datetime
import logging
import os
logging.basicConfig(filename=os.path.join(os.getcwd(),'ha.log'),level=logging.DEBUG)
def insert():
while True:
try:
conn = pymysql.connect(host='10.96.0.202',port=3306,user='dba',password='dba',database='test',charset='utf8')
cursor = conn.cursor()
for num in range(0,10000000):
data_time = time.strftime("%Y-%m-%d %H:%M:%S")
sql1="insert into hatest(num) values(%d);"%num
sql2 = "show variables like 'server_id';"
cursor.execute(sql1)
data_time = datetime.datetime.now()
cursor.execute(sql2)
server_id = cursor.fetchone()
logging.warning('%s server_id is %s'%(datetime.datetime.now(),server_id[1]))
conn.commit()
cursor.close()
except Exception as e:
logger_info = "%s %s" %(datetime.datetime.now(),e)
logging.warning(logger_info)
insert()
Script description: Use the SLB address 10.96.0.202 to insert 10 million data into the database, manually stop the database service on the 4 server during the insertion, and verify that 5 can continue to insert data after the SLB switch. Each time data is inserted, the server_Id of the database will be recorded in the log to identify which database is being written, and the error during switching will be recorded in the log
Simulate failover
Startup script
Manually stop the mysql service on 4
Observe the log and the data in the table
test
22:17 Start script continuous writing
22:18:11 4上 停止数据库 /etc/init.d/mysqld stop 日志和数据库的表中都显示在18分11秒停止写入
ha.log
Table in database
22:18:11 continues to 22:18:20, the database cannot be written 22:18:20 SLB完成主备切换 5数据库开始写入 ,日志数据库表中显示在18分20秒开始写入5环境
22:19 4上启动 /etc/init.d/mysqld start
22:19:10 seconds 4 environment database service started, 5 environment stopped writing, but 4 environment did not start writing
Verification complete