Keepalived+Router+Innodb_Cluster部署

科技   2024-05-08 15:18   上海  

文章转载来源:

一、背景说明

为了完成线上项目的高可用演练,单机数据库要改成高可用架构,公司没有工时给开发去修改架构,只能在应用侧无修改的条件下,修改数据库的架构。
经过与项目经理沟通,他们想选择keepalived+双主的架构,但是到环境一看,数据库、应用全都是docker部署的,为了整齐划一,未来好维护,我选择了群都用docker部署。可是docker环境中keepalived的MySQL的主从切换脚本我没有解决,所以我弃用keepalived+双主的架构。
改成了keepalived+router+innodb cluster,这样的好处是数据库的主从切换不用keepalived去控制,keepalived只负责router的探活功能。

二、部署步骤

1、docker-compose部署innodb cluster
第一台机器
创建对应目录
mkdir -p /mysql3310/{conf,data,logs}

创建docker-compose.yml
vim docker-compose.yml
加入:
version: '3'services:  mysql:    image: mysql:8.0.35    container_name: mysql01    restart: always    extra_hosts:     - "mysql01:10.8.91.10"     - "mysql02:10.8.91.11"     - "mysql03:10.8.91.12"    hostname: mysql01    env_file: .env    network_mode: host    volumes:        - /etc/localtime:/etc/localtime:ro        - ${DIR_MYSQL_DATA}:/var/lib/mysql        - /mysql3310/logs:/var/lib/logs        - ./conf/my.cnf:/etc/my.cnf    environment:        MYSQL_ROOT_PASSWORD: "${MYSQL_ROOT_PASSWORD}"        MYSQL_USER: apps   #创建apps用户        MYSQL_PASSWORD: XXXXXX  #设置apps用户的密码        TZ: Asia/Shanghai    command: --init_connect='SET NAMES utf8mb4' --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci --innodb_flush_log_at_trx_commit=2
创建.env文件
vim .env
加入:
# MysqlMYSQL_ROOT_PASSWORD=xxxxxxxxxDIR_MYSQL_DATA=/mysql3310/data

创建my.cnf
vim ./conf/my.cnf
加入:
[mysqld]server-id=103310port=3310mysqlx_port=33160datadir=/var/lib/mysqluser=mysqldefault-storage-engine=INNODBcharacter-set-server=utf8sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'general_log=ONgeneral_log_file=/var/lib/logs/mysql.loglog-error=/var/lib/logs/mysql.errpid-file=/var/lib/logs/mysql.pidenforce_gtid_consistency=ongtid_mode=onbinlog_transaction_dependency_tracking=WRITESET[client]default-character-set=utf8[mysql]default-character-set=utf8


第二、三台机器
使用第一台机器的docker-compose.yml
并进行如下修改:
container_name: mysql01  -->mysql02\mysql03hostname: mysql01 -->mysql02\mysql03server-id=103310 -->113310\server-id=123310

启动MySQL服务

docker-compose -f docker-compose.yml up -d

用mysqlshell组建集群
shell.connect('root@10.8.91.10:3310')var sss=dba.createCluster('ssCluster')sss.addInstance('root@10.8.91.11:3310')sss.addInstance('root@10.8.91.12:3310')

组建集群前,在主机层面添加hosts:
cat >>/etc/hosts<<EOF10.8.91.10 mysql0110.8.91.11 mysql0210.8.91.12 mysql03EOF


2、docker-compose部署keepalived+router
第一台机器10.8.91.10
keepalived镜像制作
创建目录
mkdir -p /keepalived
创建keepalived.conf 文件

vim /keepalived/keepalived.conf
加入:
global_defs {    router_id LVS_DEVEL}
vrrp_script chk_router { script "/etc/keepalived/router_check.sh" interval 2 weight -20}
vrrp_instance VI_1 { state MASTER interface enp0s3 virtual_router_id 51 priority 149 nopreempt authentication { auth_type PASS auth_pass 1111 } unicast_src_ip 10.8.91.10 unicast_peer { 10.8.91.11 } virtual_ipaddress { 10.8.91.20 } track_script { chk_router } notify "/container/service/keepalived/assets/notify.sh"}

创建router_check.sh 文件
vim /keepalived/router_check.sh
加入:
#!/bin/bash  A=`netstat -nltp|grep 6446 |wc -l`if [ $A -eq 0 ];then        pkill keepalivedfi

创建Dockerfile 文件
vim  /keepalived/Dockerfile
加入:
FROM osixia/keepalivedMAINTAINER yxx
ADD ./router_check.sh /etc/keepalived/router_check.shRUN chmod +x /etc/keepalived/router_check.sh
ADD ./keepalived.conf /container/service/keepalived/assets/keepalived.conf
构建keepalived镜像
docker build -f Dockerfile  -t keepalived:1.0 .

router镜像制作
运行docker产生router配置文件

docker run  --network=host --name=mrouter  -e MYSQL_HOST=10.8.91.10  -e MYSQL_PORT=3310    -e MYSQL_USER=root  -e MYSQL_PASSWORD="XXXXXXXX"   -e MYSQL_INNODB_CLUSTER_MEMBERS=3   -e MYSQL_ROUTER_BOOTSTRAP_EXTRA_OPTIONS="--conf-use-sockets --conf-use-gr-notifications"   -ti container-registry.oracle.com/mysql/community-router
启动router 后,测试链接MySQL,如果没问题,打包容器到镜像。
docker commit mrouter mrouter:1.0
打包完成后删除容器
docker rm mrouter

docker-compose启动keepalived+router
创建docker-copmose.yml文件
vim  /keepalived/docker-copmose.yml
加入:
version: '3'services:  keepalived:    image: keepalived:1.0    container_name: keepalived    depends_on:      - mrouter    network_mode: "host"    cap_drop:      - NET_ADMIN    privileged: true    restart: on-failure:3  mrouter:    image: mrouter:1.0    container_name: mrouter    privileged: true      network_mode: "host"    restart: on-failure:3

启动keepalived+router
docker-compose -f docker-compose.yml up -d

第二台机器10.8.91.11
复制第一台机器的/keepalived/keepalived.conf
并做如下修改:
    state MASTER -->BACKUP        unicast_src_ip 10.8.91.11    unicast_peer {         10.8.91.10    }

三、测试

通过vip链接数据库
1、停止一个数据库节点容器
2、停止一个router容器
3、停止一个keepalived容器
三种场景均能提供高可用。
MySQL数据库联盟
关注后,回复“高可用”,可获取8篇MySQL高可用文章
 最新文章