使用Docker 搭建 MySQL 双主热备 + Keepalived 高可用集群

使用Docker 搭建 MySQL 双主热备 + Keepalived 高可用集群

首先,准备两台Linux主机,并且安装好docker

Master1: 10.122.52.101
Master2: 10.122.52.102
VIP: 10.122.52.108

第二步,搭建主主集群,这个参考MySQL主主集群搭建的笔记

MySQL主主配置实践

第三步,开始在Master1服务器上安装Keepalived

  1. 修改配置文件

    vi /etc/my.cnf
    

    添加如下配置,下面的server-id 要保证在这个集群里面的唯一性

    server-id=1
    log-bin=mysql-bin
    relay-log=mysql-relay
    
  2. 安装keepalived

    yum -y install keepalived
    vi /etc/keepalived/keepalived.conf
    

​ 修改配置文件如下:

! Configuration File for keepalived

global_defs {
   router_id MySQL-Master         #router_id可以自定义
}

vrrp_instance VI_1 {
    state MASTER                  # 设置为MASTER
    interface ens33               # 设置为本机的网卡
    virtual_router_id 51
    priority 100                  # 优先级100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        10.122.52.108             # 设置VIP
    }
}

virtual_server 10.122.52.201 3306 { # 监听VIP3306端口
    delay_loop 6
    lb_algo rr
    lb_kind DR                    # 使用DR模式轮循
    net_mask 255.255.255.0
    persistence_timeout 50
    protocol TCP

    real_server 10.122.52.101 3306 {  # 本机MySQL开启3306
        weight 1
        TCP_CHECK {               # 健康检查
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
            connect_port 3306     # 设置连接端口3306
        }
    }

    real_server 10.122.52.102 3306 {   # Master2服务器MySQL
        weight 1
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
            connect_port 3306
        }
    }
}
  1. 启动keepalived

    service keepalived start
    

第四步,开始在Master2服务器上安装Keepalived

  1. 修改配置文件
vi /etc/my.cnf

​ 添加如下配置,下面的server-id 要保证在这个集群里面的唯一性

server-id=1
log-bin=mysql-bin
relay-log=mysql-relay
  1. 安装keepalived
yum -y install keepalived
vi /etc/keepalived/keepalived.conf		

​ 修改配置文件如下:

! Configuration File for keepalived

global_defs {
   router_id MySQL-Backup
}

vrrp_instance VI_1 {
    state BACKUP               # 设置为BACKUP
    interface ens33
    virtual_router_id 51
    priority 50
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        10.122.52.108
    }
}

virtual_server 10.122.52.108 3306 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    persistence_timeout 50
    net_mask 255.255.255.0
    protocol TCP

    real_server 10.122.52.101 3306 {
        weight 1
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
            connect_port 3306
        }
    }

    real_server 10.122.52.102 3306 {
        weight 1
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
            connect_port 3306
        }
    }
}
  1. 启动Keepalived
service keepalived start

第五步 验证

  1. 验证主主备份的效果

​ 分别连接Master1和Master2 的MySQL服务器

​ 在Master1中创建一个测试的数据库,然后在Master2中查询这个数据库里面的记录,如果存在,则说明它们互为主备。

CREATE table students (
	id int not null, 
	name varchar(100)
)

insert into students values(1, 'daniel')

select * from students
  1. 验证Keepalived的效果
    • 使用VIP连接上MySQL数据库,插入一条记录,然后,在另外两个Master1和Master2的连接中查询,如果都存在,则说明keepalived的VIP正常工作。
    • 随意停止一个服务器Master1 或者 Master2,然后,再使用VIP的连接来插入和查询数据,正常,则说明keepalived正常工作。(注意:当停止的服务器是vip当前连接的Master服务器,则会有几秒的卡顿)

参考: https://blog.csdn.net/Milend/article/details/107976295

你可能感兴趣的:(Docker,MySql,mysql,docker,linux)