docker-compose值mysql双主(HA)+ keepalived

  一、mysql的常用集群方案有很多种吧,但是相对容易的就是mysql的HA方案,也有更加合适的MGR方案。但是两种方案都存在利弊。比如HA方案中部署简单,也可以配置keepalived通过VIP飘移来实现IP不变的会用。但是存在的问题就是,数据会存在一定的延迟,故障事务问题等。MGR的方案主要是限制很多,比如:主键(所有表),维护等方式。

  二、这里介绍HA的双主方式+keepalived的方式来实现故障转移。

  三、部署方式:

  1)准备2台机器:

  VIP:192.168.5.100

  master-1:192.168.5.101

  master-2:192.168.5.102

  2)dockerfile

  a、master-1

FROM mysql:8.0.19
MAINTAINER xbd
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
COPY ./config/db/xbd_master_1.sql /docker-entrypoint-initdb.d

  xbd_master_1.sql

CREATE USER 'root'@'xbd-master-2' IDENTIFIED BY 'root';
grant replication slave, replication client on *.* to 'root'@'xbd-master-2';
flush privileges;
change master to master_host='xbd-master-2', master_user='root',master_password='root',master_port=3306;
start slave;

  b、master-2

FROM mysql:8.0.19
MAINTAINER xbd
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
COPY ./config/db/xbd_master_2.sql /docker-entrypoint-initdb.d

  xbd_master_2.sql

CREATE USER 'root'@'xbd-master-1' IDENTIFIED BY 'root';
grant replication slave, replication client on *.* to 'root'@'xbd-master-1';
flush privileges;
change master to master_host='xbd-master-1', master_user='root',master_password='root',master_port=3306;
start slave;

  3)docker-compose相关配置

version: "2"
services:
  xbd-master-1:
    build:
      context: ./
      dockerfile: ./config/Dockerfile/Dockerfile-master-1
    image: xbd-master-1
    restart: always
    container_name: xbd-master-1
    volumes:
      - /var/lib/mysql/xbd-master-1:/var/lib/mysql
    ports:
      - 3306:3306
    environment:
      - MYSQL_ROOT_PASSWORD=root
    privileged: true
    extra_hosts:
      - xbd-master-2:192.168.5.102
    command: ['--server-id=1',
              '--log-bin=xbd-master-1-bin',
              '--binlog-ignore-db=mysql',
              '--binlog_cache_size=256M',
              '--binlog_format=mixed',
              '--relay_log=xbd-master-1-relay',
              '--lower_case_table_names=1',
              '--character-set-server=utf8',
              '--collation-server=utf8_general_ci',
              '--sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION']

  xbd-master-2:
    build:
      context: ./
      dockerfile: ./config/Dockerfile/Dockerfile-master-2
    image: xbd-master-2
    restart: always
    container_name: xbd-master-2
    volumes:
      - /var/lib/mysql/xbd-master-2:/var/lib/mysql
    ports:
      - 3306:3306
    environment:
      - MYSQL_ROOT_PASSWORD=root
    privileged: true
    extra_hosts:
      - xbd-master-1:192.168.5.101
    command: ['--server-id=2',
              '--log-bin=xbd-master-2-bin',
              '--binlog-ignore-db=mysql',
              '--binlog_cache_size=256M',
              '--binlog_format=mixed',
              '--relay_log=xbd-master-2-relay',
              '--lower_case_table_names=1',
              '--character-set-server=utf8',
              '--collation-server=utf8_general_ci',
              '--sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION']

  使用docker-compose的方式主要是保证服务不会因为认为因素挂掉。被动挂了,可以重拉。

  4)安装keepalived这个相对简单

yum install -y keepalived

  在/etc/keepalived中修改keepalived.conf文件,没有就自己建一个

  a、master

global_defs {
    router_id MySQL-Master
}

vrrp_instance MAIN {
    state MASTER
    interface ens33
    virtual_router_id 1
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass xbd
    }
    virtual_ipaddress {
        192.168.5.100
    }
}

  b、backup

global_defs {
    router_id MySQL-Backup
}

vrrp_instance MAIN {
    state BACKUP
    interface ens33
    virtual_router_id 1
    priority 99
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass xbd
    }
    virtual_ipaddress {
        192.168.5.100
    }
}

  四、需要的文件

  

 

   五、测试:

  1)mysql双主

  

    

   2)测试虚拟IP

  

 

 

 

 

 

 

posted @ 2022-11-21 14:41  小不点丶  阅读(744)  评论(0编辑  收藏  举报