LoginSignup
2

More than 5 years have passed since last update.

MySQL ReplicationのStackを作る(Rancher 2.0 Tech Preview版)

Posted at

説明不足気味なので、適宜注釈を入れたい。。。

目的

Rancher 2.0で任意のStackを作成するための手順を整理する。
例として MySQLのレプリケーション構成を利用する。

注意

Tech Preview だからな!!

手順

MySQLのMasterを準備

Kobito.EAfbk2.png

MySQLMasterのコマンド内容
mysqld --port=3306 --server-id=101 --character-set-server=utf8mb4 --datadir=/var/lib/mysql --log-error=/var/log/mysql/error.log --slow-query-log --slow-query-log-file=/var/log/mysql/slow_query_log.log --long-query-time=5 --log-bin --max-connections=4096 --gtid-mode=on --enforce-gtid-consistency=on

Kobito.soxioE.png

MySQLのMasterのside-kickを準備

サイドキックコンテナの実行コマンド
while :; do
    sleep 10;
    if [ `mysqladmin -h MySQLMaster -u root -proot | grep alive | wc -l` -eq 1 ]; then
        echo "alive";
        mysql -u root -proot -h MySQLMaster -e "CREATE USER 'repl'@'%' IDENTIFIED BY 'repl'; GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%';";
        break;
    fi
done

Dockerコンテナのコマンドとして実行しなければいけないので、ワンライナー化する。

サイドキックコンテナの実行コマンド(ワンライナー)
/bin/bash -c "while :; do sleep 10; if [ `mysqladmin -h MySQLMaster -u root -proot | grep alive | wc -l` -eq 1 ]; then echo \"alive\"; mysql -u root -proot -h MySQLMaster -e \"CREATE USER 'repl'@'%' IDENTIFIED BY 'repl'; GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%';\"; break; fi done"

Kobito.FcRaxG.png

コマンドにはワンライナー化したコマンドを指定する。

Kobito.PvxMxM.png

MySQLのSlaveを準備

MySQLSlaveのコマンド内容
mysqld --port=3306 --server-id=102 --character-set-server=utf8mb4 --datadir=/var/lib/mysql --log-error=/var/log/mysql/error.log --slow-query-log --slow-query-log-file=/var/log/mysql/slow_query_log.log --long-query-time=5 --log-bin --max-connections=4096 --gtid-mode=on --enforce-gtid-consistency=on

Kobito.b5X7Xh.png

ホストをまたいだパケットが通らないので。
Kobito.tcMgPT.png

Kobito.NS9FYF.png

MySQLのSlaveのside-kickを準備

MySQLSlaveのサイドキックコンテナで実行するコマンド
while :;
    do sleep 10;
    if [ `mysqladmin -h MySQLMaster -u root -proot | grep alive | wc -l` -eq 1 ]; then
        echo "master alive";
        if [ `mysqladmin -h MySQLSlave -u root -proot | grep alive | wc -l` -eq 1 ]; then
            echo "slavealive";
            mysql -u root -proot -h MySQLSlave -e "CHANGE MASTER TO MASTER_HOST='MySQLMaster', MASTER_USER='repl', MASTER_PASSWORD='repl', MASTER_AUTO_POSITION=1; START SLAVE;";
            sleep 10;
            mysql -u root -proot -h 10.42.2.11 -e "show slave status\G";
            break;
        fi
    fi
done

MySQLSlaveのサイドキックコンテナで実行するコマンド(ワンライナー版)
/bin/bash -c "while :; do sleep 10; if [ `mysqladmin -h MySQLMaster -u root -proot | grep alive | wc -l` -eq 1 ]; then echo \"master alive\"; if [ `mysqladmin -h MySQLSlave -u root -proot | grep alive | wc -l` -eq 1 ]; then echo \"slave also alive\"; mysql -u root -proot -h MySQLSlave -e \"CHANGE MASTER TO MASTER_HOST='MySQLMaster', MASTER_USER='repl', MASTER_PASSWORD='repl', MASTER_AUTO_POSITION=1; START SLAVE;\"; sleep 10; mysql -u root -proot -h MySQLSlave -e \"show slave status\\G\";break; fi fi done"

Kobito.alAcGx.png

Kobito.TSFQnr.png

一旦ここまでで完成。
Kobito.HrcZFt.png

下図のとおり、slaveコンテナからmasterは見えている。
Kobito.MPfC0U.png

config.yml

config.ymlは出力できたが、まだこれを使ってのインポートにはバグがある模様。
まあ、Tech Previewだし...

config.yml
version: "2"
services:
  MySQLMaster:
    command: [mysqld, --port=3306, --server-id=101, --character-set-server=utf8mb4,
      --datadir=/var/lib/mysql, --log-error=/var/log/mysql/error.log, --slow-query-log,
      --slow-query-log-file=/var/log/mysql/slow_query_log.log, --long-query-time=5,
      --log-bin, --max-connections=4096, --gtid-mode=on, --enforce-gtid-consistency=on]
    environment:
    - MYSQL_ROOT_PASSWORD=root
    image: mysql:5.7
    labels:
      io.rancher.container.pull_image: always
    ports:
    - 3306:3306/tcp
    stop_signal: SIGTERM
    restart: always
    stdin_open: true
    tty: true
    scale: 1
  MySQLMasterSidekick:
    command: [/bin/bash, -c, 'while :; do sleep 10; if [ `mysqladmin -h MySQLMaster
        -u root -proot | grep alive | wc -l` -eq 1 ]; then echo "alive"; mysql -u
        root -proot -h MySQLMaster -e "CREATE USER ''repl''@''%'' IDENTIFIED BY ''repl'';
        GRANT REPLICATION SLAVE ON *.* TO ''repl''@''%'';"; break; fi done']
    image: mysql:5.7
    labels:
      io.rancher.container.pull_image: always
    network_mode: container:MySQLMaster
    stop_signal: SIGTERM
    restart: "no"
    stdin_open: true
    tty: true
    scale: 1
  MySQLSlave:
    command: [mysqld, --port=3306, --server-id=102, --character-set-server=utf8mb4,
      --datadir=/var/lib/mysql, --log-error=/var/log/mysql/error.log, --slow-query-log,
      --slow-query-log-file=/var/log/mysql/slow_query_log.log, --long-query-time=5,
      --log-bin, --max-connections=4096, --gtid-mode=on, --enforce-gtid-consistency=on]
    environment:
    - MYSQL_ROOT_PASSWORD=root
    image: mysql:5.7
    labels:
      io.rancher.container.pull_image: always
      io.rancher.scheduler.affinity:container: mysqlreplication-mysqlmaster-1
    stop_signal: SIGTERM
    restart: always
    stdin_open: true
    tty: true
    scale: 1
  MySQLSlaveSidekick:
    command: [/bin/bash, -c, 'while :; do sleep 10; if [ `mysqladmin -h MySQLMaster
        -u root -proot | grep alive | wc -l` -eq 1 ]; then echo "master alive"; if
        [ `mysqladmin -h MySQLSlave -u root -proot | grep alive | wc -l` -eq 1 ];
        then echo "slave also alive"; mysql -u root -proot -h MySQLSlave -e "CHANGE
        MASTER TO MASTER_HOST=''MySQLMaster'', MASTER_USER=''repl'', MASTER_PASSWORD=''repl'',
        MASTER_AUTO_POSITION=1; START SLAVE;"; sleep 10; mysql -u root -proot -h MySQLSlave
        -e "show slave status\G";break; fi fi done']
    image: mysql:5.7
    labels:
      io.rancher.container.pull_image: always
    network_mode: container:MySQLSlave
    stop_signal: SIGTERM
    restart: on-failure
    stdin_open: true
    tty: true
    scale: 1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2