文章转载来源:
一、背景说明
二、部署步骤
1、docker-compose部署innodb cluster
创建对应目录 mkdir -p /mysql3310/{conf,data,logs}
创建docker-compose.ymlvim 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
加入: # Mysql
MYSQL_ROOT_PASSWORD=xxxxxxxxx
DIR_MYSQL_DATA=/mysql3310/data
创建my.cnfvim ./conf/my.cnf
加入: [mysqld]
server-id=103310
port=3310
mysqlx_port=33160
datadir=/var/lib/mysql
user=mysql
default-storage-engine=INNODB
character-set-server=utf8
sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'
general_log=ON
general_log_file=/var/lib/logs/mysql.log
log-error=/var/lib/logs/mysql.err
pid-file=/var/lib/logs/mysql.pid
enforce_gtid_consistency=on
gtid_mode=on
binlog_transaction_dependency_tracking=WRITESET
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
container_name: mysql01 -->mysql02\mysql03
hostname: mysql01 -->mysql02\mysql03
server-id=103310 -->113310\server-id=123310
docker-compose -f docker-compose.yml up -d
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')
cat >>/etc/hosts<<EOF
10.8.91.10 mysql01
10.8.91.11 mysql02
10.8.91.12 mysql03
EOF
2、docker-compose部署keepalived+router
创建目录 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
加入: A=`netstat -nltp|grep 6446 |wc -l`
if [ $A -eq 0 ];then
pkill keepalived
fi
创建Dockerfile 文件vim /keepalived/Dockerfile
加入: FROM osixia/keepalived
MAINTAINER yxx
ADD ./router_check.sh /etc/keepalived/router_check.sh
RUN 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 .
运行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-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
复制第一台机器的/keepalived/keepalived.conf 并做如下修改: state MASTER -->BACKUP
unicast_src_ip 10.8.91.11
unicast_peer {
10.8.91.10
}