새소식

MySQL & Maria

[ MySQL ] Container DB Replication ( Using. Docker-compose )

  • -
반응형

Docker Compose를 이용해서 이전에 포스팅했던 Docker Image를 빌드한후 DB Replicatoin 올리는 방법을 작성해보곗습니다.

간략하게 요약하면 아래 순으로 실행이됩니다.

Docker hub에 올려논 Docker Image를 통해서 → Docker file을 만들고 → Docker Compose를 이용하여 Replication 설계

https://github.com/dkwlfowh/docker_mysql8_repl

■ Docker-Compose 설치

[root@pmm-server ~]# sudo curl -L "https://github.com/docker/compose/releases/download/1.24.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
[root@pmm-server ~]# sudo chmod +x /usr/local/bin/docker-compose
[root@pmm-server ~]# sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
[root@pmm-server ~]# docker-compose -version 
docker-compose version 1.24.1, build 4667896b

 

■ Docker-Compose.yml 구조

Dockerfile을 이용해서 Images를 빌드하고 images이름은 leeyunhyeong/master이다.

[root@pmm-server docker_repl]# cat docker-compose.yml 

version : '3'
services:
  mysql_master:
    image: leeyunhyeong/master
    user: root
    build:
      context: ./master
      dockerfile: Dockerfile
    stdin_open: true
    tty: true
    ports:
      - 3306:3306
    networks:
      dock_net:
        ipv4_address: 172.19.0.11
    container_name: my8_master
    restart: always

  mysql_slave:
    image: leeyunhyeong/slave
    user: root
    build:
      context: ./slave
      dockerfile: Dockerfile
    stdin_open: true
    tty: true
    ports:
      - 3307:3306
    networks:
      dock_net:
        ipv4_address: 172.19.0.12
    container_name: my8_slave
    restart: always

networks:
  dock_net:
networks:
  dock_net:
    driver: bridge
    ipam:
     config:
        - subnet: 172.19.0.0/16

 

 

■ Master Dockerfile & script 파일

  • FROM : build 할 이미지
  • USER : 아래 커맨드를 수행할 os user
  • ADD : host의 파일이나 디렉토리를 container로 복사
  • RUN : container에서 수행할 커맨드
[root@pmm-server docker_repl]# cat master/Dockerfile 
FROM leeyunhyeong/mysql8:1
MAINTAINER leeyun

USER root
RUN echo "master" > master.txt

ADD master.sh /root/
RUN chmod 755 /root/master.sh
[root@pmm-server docker_repl]# cat master/master.sh 
#!/bin/bash
set -e
sed -i -r -e "/[myqsld]/a\server_id=1" /etc/my.cnf
/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf --user=mysql &
echo "LEE"
## create user
sleep 10
/mysql/bin/mysql -uroot -p'root' -e "create user 'repl'@'172.19.0.%' identified with mysql_native_password by 'repl'"
/mysql/bin/mysql -uroot -p'root' -e "grant all on *.* to 'repl'@'172.19.0.%'"
/mysql/bin/mysql -uroot -p'root' -e "create user 'root'@'%' identified with mysql_native_password by 'root'"
/mysql/bin/mysql -uroot -p'root' -e "grant all on *.* to 'root'@'%'"

 

■ Slave Dockerfile & script 파일

[root@pmm-server docker_repl]# cat slave/Dockerfile 
FROM leeyunhyeong/mysql8:1
MAINTAINER leeyun

USER root
RUN echo "slave" > slave.txt

ADD slave.sh /root/
RUN chmod 755 /root/slave.sh
[root@pmm-server docker_repl]# cat slave/slave.sh 
#!/bin/bash
set -e
sed -i -r -e "/[myqsld]/a\server_id=$1" /etc/my.cnf
rm -rf /data/data/auto.cnf
/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf --user=mysql &

### create user
sleep 10

/mysql/bin/mysql -uroot -p'root' -e "create user 'repl'@'172.19.0.%' identified with mysql_native_password by 'repl'"
/mysql/bin/mysql -uroot -p'root' -e "grant all on *.* to 'repl'@'172.19.0.%'"
/mysql/bin/mysql -uroot -p'root' -e "create user 'root'@'%' identified with mysql_native_password by 'root'"
/mysql/bin/mysql -uroot -p'root' -e "grant all on *.* to 'root'@'%'"

## get  status
master_log_file=`/mysql/bin/mysql -uroot -p'root' -h 172.19.0.11 -e"show master status\G" | grep mysql-bin`
re="[a-z]*-bin.[0-9]*"

if [[ ${master_log_file} =~ $re ]];then
    master_log_file=${BASH_REMATCH[0]}
fi

master_log_pos=`/mysql/bin/mysql -uroot -p'root' -h 172.19.0.11 -e"show master  status\G" | grep Position`

re="[0-9]+"

if [[ ${master_log_pos} =~ $re ]];then
    master_log_pos=${BASH_REMATCH[0]}
fi

query="change master to master_host='172.19.0.11', master_user='repl', master_password='repl', master_log_file='${master_log_file}', master_log_pos=${master_log_pos}, master_port=3306"

/mysql/bin/mysql -uroot -p'root'  -e "${query}"
/mysql/bin/mysql -uroot -p'root'  -e "start slave"
/bin/bash

exit;

 

■ Docker-Compose를 이용해 Image 빌드

[root@pmm-server docker_repl]# docker-compose -f /root/docker_repl/docker-compose.yml build
Building mysql_master
Step 1/6 : FROM leeyunhyeong/mysql8:1
1: Pulling from leeyunhyeong/mysql8
2d473b07cdd5: Already exists
90be23feaba8: Already exists
Digest: sha256:5870ca603617e7381c8b9fdaf37721368a716caa19818f62b8806ec0a074db82
Status: Downloaded newer image for leeyunhyeong/mysql8:1
 ---> 392141461f9c
Step 2/6 : MAINTAINER leeyun
 ---> Running in 5c93a4ea9edc
Removing intermediate container 5c93a4ea9edc
 ---> daf8ec20e3f1
Step 3/6 : USER root
 ---> Running in fae04893bafc
Removing intermediate container fae04893bafc
 ---> bc523972aa3e
Step 4/6 : RUN echo "master" > master.txt
 ---> Running in 47099cde8ec9
Removing intermediate container 47099cde8ec9
 ---> 26adf0b145b7
Step 5/6 : ADD master.sh /root/
 ---> c823a12716fb
Step 6/6 : RUN chmod 755 /root/master.sh
 ---> Running in 6968338457cb
Removing intermediate container 6968338457cb
 ---> ce1b79466590
Successfully built ce1b79466590
Successfully tagged leeyunhyeong/master:latest
Building mysql_slave
Step 1/6 : FROM leeyunhyeong/mysql8:1
 ---> 392141461f9c
Step 2/6 : MAINTAINER leeyun
 ---> Using cache
 ---> daf8ec20e3f1
Step 3/6 : USER root
 ---> Using cache
 ---> bc523972aa3e
Step 4/6 : RUN echo "slave" > slave.txt
 ---> Running in aff03a85dc51
Removing intermediate container aff03a85dc51
 ---> 909d74b93528
Step 5/6 : ADD slave.sh /root/
 ---> acbe810914e4
Step 6/6 : RUN chmod 755 /root/slave.sh
 ---> Running in b2eb79e8c2ca
Removing intermediate container b2eb79e8c2ca
 ---> 29c4b8a7e660
Successfully built 29c4b8a7e660
Successfully tagged leeyunhyeong/slave:latest

 

■ 2-Node Replication 구성 

[root@pmm-server docker_repl]# docker run -itd --net docker_repl_dock_net --ip 172.19.0.11 -p 3306:3306 --name my8_master leeyunhyeong/master
[root@pmm-server docker_repl]# docker run -itd --net docker_repl_dock_net --ip 172.19.0.12 -p 3307:3306 --name my8_slave leeyunhyeong/slave
[root@pmm-server docker_repl]# docker exec my8_master /root/master.sh
[root@pmm-server docker_repl]# docker exec my8_slave /root/slave.sh 2

 

■ 다중 Replication 구성 스크립트 실행

  • Script 실행후 원하는 node 갯수를 입력
  • 해당 갯수만큼 DB Container가 생성되며 Replication 설정이 완료되게됩니다.
[root@pmm-server docker_repl]# cat lee.sh 
#!/bin/bash
#### ENV
port=3305

echo "################ HOW MANY SERVER ##############"
echo "COUNT: "
read count

echo "#################### BUILD DOCKER IMAGES #####################"
docker-compose -f /root/docker_mysql8_repl/docker-compose.yml build


echo "#################### Network Create #####################"
check_net=`docker network ls | grep 'lee' | awk '{print $2}'`

if [ $check_net == "lee" ]; then
  echo 'exists Network'
else
  docker network create --gateway 172.19.0.1 --subnet 172.19.0.0/24 lee
fi


echo "#################### CREATE MASTER DATABASE ####################"
echo "Master IP:172.19.0.11 / Port 3306:3306"
docker run -itd --cap-add=sys_nice --net lee --ip 172.19.0.11 -p 3306:3306 --name my8_master leeyunhyeong/master
docker exec my8_master /root/master.sh


echo "#################### CREATE SLAVE DATABASE ####################"

for cnt in `seq 2 $count`;
do
 echo -e "\n Slave IP:172.19.0.1${cnt} / Port `expr $port + $cnt`:3306"
 docker run -itd --cap-add=sys_nice --net lee --ip 172.19.0.1${cnt} -p `expr $port + $cnt`:3306 --name my8_slave`expr ${cnt} - 1` leeyunhyeong/slave
 docker exec my8_slave`expr ${cnt} - 1` /root/slave.sh $cnt
done

docker ps

echo -e "\n################### Slave Database Replicaiton Check ##################"

for cnt in `seq 2 $count`;
do
 echo -e "\n Current Container =  my8_slave`expr ${cnt} - 1`"
 docker exec my8_slave`expr ${cnt} - 1` /mysql/bin/mysql -uroot -proot -e "show slave status\G" | grep "Master_Host\|Slave_IO_Running\|Slave_SQL_Running\|Last_IO_Error\|Last_SQL_Error"
done

 

반응형
Contents

포스팅 주소를 복사했습니다

이 글이 도움이 되었다면 공감 부탁드립니다.