├── .gitmodules ├── README.md ├── galera_sync_replication ├── pxc-node1-rc.yaml ├── pxc-node1-service.yaml ├── pxc-node1.yaml ├── pxc-node2-service.yaml ├── pxc-node2.yaml ├── pxc-node3-service.yaml └── pxc-node3.yaml └── mysql_async_replication ├── README.md ├── image ├── README.md ├── master │ ├── Dockerfile │ ├── docker-entrypoint.sh │ ├── my.cnf.tmpl │ └── random.sh └── slave │ ├── Dockerfile │ ├── docker-entrypoint.sh │ ├── my.cnf.tmpl │ └── random.sh ├── mysql-master-service.json ├── mysql-master.json ├── mysql-slave-service.json └── mysql-slave.json /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "galera_sync_replication/image"] 2 | path = galera_sync_replication/image 3 | url = git@github.com:CaptTofu/percona_xtradb_cluster_docker.git 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # MySQL simple replication example for Kubernetes 2 | 3 | This repository is very simple and designed to primarily be a proof-of-concept. 4 | 5 | The POC is essentially show how to launch two types of MySQL replication on Kubernetes: 6 | 7 | * Asynchronous master and slave(s) 8 | * Galera synchronous (Percona XtraDB Cluster) 9 | -------------------------------------------------------------------------------- /galera_sync_replication/pxc-node1-rc.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: v1beta3 2 | kind: ReplicationController 3 | metadata: 4 | labels: 5 | name: pxc-node1 6 | name: pxc-node1 7 | spec: 8 | replicas: 1 9 | selector: 10 | name: pxc-node1 11 | template: 12 | metadata: 13 | labels: 14 | name: pxc-node1 15 | spec: 16 | containers: 17 | - command: 18 | - /entrypoint.sh 19 | resources: 20 | limits: 21 | cpu: 1 22 | env: 23 | - name: GALERA_CLUSTER 24 | value: "true" 25 | - name: WSREP_CLUSTER_ADDRESS 26 | value: gcomm:// 27 | - name: WSREP_SST_USER 28 | value: sst 29 | - name: WSREP_SST_PASSWORD 30 | value: sst 31 | - name: MYSQL_USER 32 | value: mysql 33 | - name: MYSQL_PASSWORD 34 | value: mysql 35 | - name: MYSQL_ROOT_PASSWORD 36 | value: c-krit 37 | image: "capttofu/percona_xtradb_cluster_5_6:latest" 38 | name: pxc-node1 39 | ports: 40 | - containerPort: 3306 41 | name: client 42 | - containerPort: 4444 43 | name: sst 44 | - containerPort: 4567 45 | name: rep 46 | - containerPort: 4568 47 | name: incsst 48 | -------------------------------------------------------------------------------- /galera_sync_replication/pxc-node1-service.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | apiVersion: v1 3 | kind: Service 4 | id: pxc-node1 5 | metadata: 6 | lables: 7 | name: pxc-node1 8 | name: pxc-node1 9 | spec: 10 | ports: 11 | - port: 3306 12 | targetPort: 3306 13 | name: mysql 14 | - port: 4444 15 | targetPort: 4444 16 | name: state-snapshot-transfer 17 | - port: 4567 18 | targetPort: 4567 19 | name: replication-traffic 20 | - port: 4568 21 | targetPort: 4568 22 | name: incremental-state-transfer 23 | selector: 24 | name: pxc-node1 25 | labels: 26 | name: pxc-node1 27 | -------------------------------------------------------------------------------- /galera_sync_replication/pxc-node1.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: v1 2 | kind: Pod 3 | metadata: 4 | name: pxc-node1 5 | labels: 6 | name: pxc-node1 7 | spec: 8 | containers: 9 | - resources: 10 | limits: 11 | cpu: 0.5 12 | image: capttofu/percona_xtradb_cluster_5_6:latest 13 | name: pxc-node1 14 | ports: 15 | - containerPort: 3306 16 | hostPort: 3306 17 | name: mysql 18 | - containerPort: 4444 19 | hostPort: 4444 20 | #name: state-snapshot-transfer 21 | - containerPort: 4567 22 | hostPort: 4567 23 | # name: replication-traffic 24 | - containerPort: 4568 25 | hostPort: 4568 26 | # name: incremental-state-transfer 27 | env: 28 | - name: GALERA_CLUSTER 29 | value: "true" 30 | - name: WSREP_CLUSTER_ADDRESS 31 | value: gcomm:// 32 | - name: WSREP_SST_USER 33 | value: sst 34 | - name: WSREP_SST_PASSWORD 35 | value: sst 36 | - name: MYSQL_USER 37 | value: mysql 38 | - name: MYSQL_PASSWORD 39 | value: mysql 40 | - name: MYSQL_ROOT_PASSWORD 41 | value: c-krit 42 | -------------------------------------------------------------------------------- /galera_sync_replication/pxc-node2-service.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | apiVersion: v1 3 | kind: Service 4 | id: pxc-node2 5 | metadata: 6 | lables: 7 | name: pxc-node2 8 | name: pxc-node2 9 | spec: 10 | ports: 11 | - port: 3306 12 | targetPort: 3306 13 | name: mysql 14 | - port: 4444 15 | targetPort: 4444 16 | name: state-snapshot-transfer 17 | - port: 4567 18 | targetPort: 4567 19 | name: replication-traffic 20 | - port: 4568 21 | targetPort: 4568 22 | name: incremental-state-transfer 23 | selector: 24 | name: pxc-node2 25 | labels: 26 | name: pxc-node2 27 | -------------------------------------------------------------------------------- /galera_sync_replication/pxc-node2.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: v1 2 | kind: Pod 3 | metadata: 4 | name: pxc-node2 5 | labels: 6 | name: pxc-node2 7 | spec: 8 | containers: 9 | - resources: 10 | limits: 11 | cpu: 0.5 12 | image: capttofu/percona_xtradb_cluster_5_6:latest 13 | name: pxc-node1 14 | ports: 15 | - containerPort: 3306 16 | hostPort: 3306 17 | name: mysql 18 | - containerPort: 4444 19 | hostPort: 4444 20 | #name: state-snapshot-transfer 21 | - containerPort: 4567 22 | hostPort: 4567 23 | # name: replication-traffic 24 | - containerPort: 4568 25 | hostPort: 4568 26 | # name: incremental-state-transfer 27 | env: 28 | - name: GALERA_CLUSTER 29 | value: "true" 30 | - name: WSREP_CLUSTER_ADDRESS 31 | value: gcomm:// 32 | - name: WSREP_SST_USER 33 | value: sst 34 | - name: WSREP_SST_PASSWORD 35 | value: sst 36 | - name: MYSQL_USER 37 | value: mysql 38 | - name: MYSQL_PASSWORD 39 | value: mysql 40 | - name: MYSQL_ROOT_PASSWORD 41 | value: c-krit 42 | -------------------------------------------------------------------------------- /galera_sync_replication/pxc-node3-service.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | apiVersion: v1 3 | kind: Service 4 | id: pxc-node3 5 | metadata: 6 | lables: 7 | name: pxc-node3 8 | name: pxc-node3 9 | spec: 10 | ports: 11 | - port: 3306 12 | targetPort: 3306 13 | name: mysql 14 | - port: 4444 15 | targetPort: 4444 16 | name: state-snapshot-transfer 17 | - port: 4567 18 | targetPort: 4567 19 | name: replication-traffic 20 | - port: 4568 21 | targetPort: 4568 22 | name: incremental-state-transfer 23 | selector: 24 | name: pxc-node3 25 | labels: 26 | name: pxc-node3 27 | -------------------------------------------------------------------------------- /galera_sync_replication/pxc-node3.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: v1 2 | kind: Pod 3 | metadata: 4 | name: pxc-node3 5 | labels: 6 | name: pxc-node3 7 | spec: 8 | containers: 9 | - resources: 10 | limits: 11 | cpu: 0.5 12 | image: capttofu/percona_xtradb_cluster_5_6:latest 13 | name: pxc-node1 14 | ports: 15 | - containerPort: 3306 16 | hostPort: 3306 17 | name: mysql 18 | - containerPort: 4444 19 | hostPort: 4444 20 | #name: state-snapshot-transfer 21 | - containerPort: 4567 22 | hostPort: 4567 23 | # name: replication-traffic 24 | - containerPort: 4568 25 | hostPort: 4568 26 | # name: incremental-state-transfer 27 | env: 28 | - name: GALERA_CLUSTER 29 | value: "true" 30 | - name: WSREP_CLUSTER_ADDRESS 31 | value: gcomm:// 32 | - name: WSREP_SST_USER 33 | value: sst 34 | - name: WSREP_SST_PASSWORD 35 | value: sst 36 | - name: MYSQL_USER 37 | value: mysql 38 | - name: MYSQL_PASSWORD 39 | value: mysql 40 | - name: MYSQL_ROOT_PASSWORD 41 | value: c-krit 42 | -------------------------------------------------------------------------------- /mysql_async_replication/README.md: -------------------------------------------------------------------------------- 1 | # MySQL simple asynchronous replication example for Kubernetes 2 | 3 | 4 | Kubernetes launches pods which the containers in those pods have environment variables set, the service files for the mysql master in particular $MYSQL_MASTER_SERVICE_HOST, provides a way for the the slave pod's containers to connect to the master and start replicating. This is done using the entrypoint script to run the necessary "CHANGE MASTER..." SQL statements. 5 | 6 | Contained in this repository are Kubernetes pod and service files for both master and slave as well as the files a master and slave images. These images each have a Dockerfile, an entrypoint script performing the necessary Kube-fu to set up replication. 7 | 8 | ## Image file building - if you don't want to use the ones specified 9 | 10 | cd images/master 11 | docker build -t yourname/mysql_master_kubernetes . 12 | docker push yourname/mysql_master_kubernetes 13 | cd images/slave 14 | docker build -t yourname/mysql_slave_kubernetes . 15 | docker push yourname/mysql_slave_kubernetes 16 | 17 | 18 | ## Kubernetes USAGE 19 | 20 | kubectl -f mysql-master.json 21 | kubectl -f mysql-master-service.json 22 | kubectl -f mysql-slave.json 23 | kubectl -f mysql-slave-service.json 24 | 25 | Any questions? Look at the code and contact the author! 26 | -------------------------------------------------------------------------------- /mysql_async_replication/image/README.md: -------------------------------------------------------------------------------- 1 | ## Docker image files for master slave 2 | -------------------------------------------------------------------------------- /mysql_async_replication/image/master/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM debian:wheezy 2 | 3 | # add our user and group first to make sure their IDs get assigned consistently, regardless of whatever dependencies get added 4 | RUN groupadd -r mysql && useradd -r -g mysql mysql 5 | 6 | # FATAL ERROR: please install the following Perl modules before executing /usr/local/mysql/scripts/mysql_install_db: 7 | # File::Basename 8 | # File::Copy 9 | # Sys::Hostname 10 | # Data::Dumper 11 | RUN apt-get update && apt-get install -y perl --no-install-recommends && rm -rf /var/lib/apt/lists/* 12 | 13 | # gpg: key 5072E1F5: public key "MySQL Release Engineering " imported 14 | RUN apt-key adv --keyserver pool.sks-keyservers.net --recv-keys A4A9406876FCBD3C456770C88C718D3B5072E1F5 15 | 16 | ENV MYSQL_MAJOR 5.7 17 | ENV MYSQL_VERSION 5.7.7-rc 18 | 19 | RUN echo "deb http://repo.mysql.com/apt/debian/ wheezy mysql-${MYSQL_MAJOR}-dmr" > /etc/apt/sources.list.d/mysql.list 20 | 21 | # the "/var/lib/mysql" stuff here is because the mysql-server postinst doesn't have an explicit way to disable the mysql_install_db codepath besides having a database already "configured" (ie, stuff in /var/lib/mysql/mysql) 22 | # also, we set debconf keys to make APT a little quieter 23 | RUN { \ 24 | echo mysql-community-server mysql-community-server/data-dir select ''; \ 25 | echo mysql-community-server mysql-community-server/root-pass password ''; \ 26 | echo mysql-community-server mysql-community-server/re-root-pass password ''; \ 27 | echo mysql-community-server mysql-community-server/remove-test-db select false; \ 28 | } | debconf-set-selections \ 29 | && apt-get update && apt-get install -y mysql-server="${MYSQL_VERSION}"* && rm -rf /var/lib/apt/lists/* \ 30 | && rm -rf /var/lib/mysql && mkdir -p /var/lib/mysql 31 | 32 | VOLUME /var/lib/mysql 33 | 34 | COPY my.cnf.tmpl /tmp/my.cnf.tmpl 35 | # need random.sh because otherwise, ENV $RANDOM is not set 36 | COPY random.sh /tmp/random.sh 37 | RUN /tmp/random.sh 38 | 39 | COPY docker-entrypoint.sh /entrypoint.sh 40 | ENTRYPOINT ["/entrypoint.sh"] 41 | 42 | EXPOSE 3306 43 | CMD ["mysqld"] 44 | -------------------------------------------------------------------------------- /mysql_async_replication/image/master/docker-entrypoint.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | set -e 3 | 4 | if [ "${1:0:1}" = '-' ]; then 5 | set -- mysqld "$@" 6 | fi 7 | 8 | if [ "$1" = 'mysqld' ]; then 9 | # read DATADIR from the MySQL config 10 | DATADIR="$("$@" --verbose --help 2>/dev/null | awk '$1 == "datadir" { print $2; exit }')" 11 | 12 | if [ ! -d "$DATADIR/mysql" ]; then 13 | if [ -z "$MYSQL_ROOT_PASSWORD" -a -z "$MYSQL_ALLOW_EMPTY_PASSWORD" ]; then 14 | echo >&2 'error: database is uninitialized and MYSQL_ROOT_PASSWORD not set' 15 | echo >&2 ' Did you forget to add -e MYSQL_ROOT_PASSWORD=... ?' 16 | exit 1 17 | fi 18 | if [ -z "$MYSQL_REPLICATION_PASSWORD" ]; then 19 | echo >&2 'error: database is uninitialized and MYSQL_REPLICATION_PASSWORD not set' 20 | echo >&2 ' Did you forget to add -e MYSQL_REPLICATION_PASSWORD=... ?' 21 | exit 1 22 | fi 23 | 24 | echo 'Initializing database' 25 | mysqld --initialize-insecure=on --datadir="$DATADIR" 26 | echo 'Database initialized' 27 | 28 | # These statements _must_ be on individual lines, and _must_ end with 29 | # semicolons (no line breaks or comments are permitted). 30 | # TODO proper SQL escaping on ALL the things D: 31 | 32 | tempSqlFile='/tmp/mysql-first-time.sql' 33 | cat > "$tempSqlFile" <<-EOSQL 34 | DELETE FROM mysql.user ; 35 | CREATE USER 'root'@'%' IDENTIFIED BY '${MYSQL_ROOT_PASSWORD}' ; 36 | GRANT ALL ON *.* TO 'root'@'%' WITH GRANT OPTION ; 37 | DROP DATABASE IF EXISTS test ; 38 | EOSQL 39 | 40 | if [ "$MYSQL_DATABASE" ]; then 41 | echo "CREATE DATABASE IF NOT EXISTS \`$MYSQL_DATABASE\` ;" >> "$tempSqlFile" 42 | fi 43 | 44 | if [ "$MYSQL_USER" -a "$MYSQL_PASSWORD" ]; then 45 | echo "CREATE USER '$MYSQL_USER'@'%' IDENTIFIED BY '$MYSQL_PASSWORD' ;" >> "$tempSqlFile" 46 | 47 | if [ "$MYSQL_DATABASE" ]; then 48 | echo "GRANT ALL ON \`$MYSQL_DATABASE\`.* TO '$MYSQL_USER'@'%' ;" >> "$tempSqlFile" 49 | fi 50 | fi 51 | 52 | echo "GRANT REPLICATION SLAVE, REPLICATION CLIENT on *.* TO 'repl'@'10.100.%' IDENTIFIED BY '$MYSQL_REPLICATION_PASSWORD';" >> "$tempSqlFile" 53 | echo "GRANT REPLICATION SLAVE, REPLICATION CLIENT on *.* TO 'repl'@'10.244.%' IDENTIFIED BY '$MYSQL_REPLICATION_PASSWORD';" >> "$tempSqlFile" 54 | echo "GRANT REPLICATION SLAVE, REPLICATION CLIENT on *.* TO 'repl'@'172.17.%' IDENTIFIED BY '$MYSQL_REPLICATION_PASSWORD';" >> "$tempSqlFile" 55 | 56 | echo 'FLUSH PRIVILEGES ;' >> "$tempSqlFile" 57 | 58 | set -- "$@" --init-file="$tempSqlFile" 59 | fi 60 | 61 | chown -R mysql:mysql "$DATADIR" 62 | fi 63 | 64 | exec "$@" 65 | -------------------------------------------------------------------------------- /mysql_async_replication/image/master/my.cnf.tmpl: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 2 | # 3 | # This program is free software; you can redistribute it and/or modify 4 | # it under the terms of the GNU General Public License as published by 5 | # the Free Software Foundation; version 2 of the License. 6 | # 7 | # This program is distributed in the hope that it will be useful, 8 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | # GNU General Public License for more details. 11 | # 12 | # You should have received a copy of the GNU General Public License 13 | # along with this program; if not, write to the Free Software 14 | # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 15 | 16 | # 17 | # The MySQL Community Server configuration file. 18 | # 19 | # For explanations see 20 | # http://dev.mysql.com/doc/mysql/en/server-system-variables.html 21 | 22 | [client] 23 | port = 3306 24 | socket = /var/run/mysqld/mysqld.sock 25 | 26 | [mysqld_safe] 27 | pid-file = /var/run/mysqld/mysqld.pid 28 | socket = /var/run/mysqld/mysqld.sock 29 | nice = 0 30 | 31 | [mysqld] 32 | user = mysql 33 | datadir = /var/lib/mysql/data 34 | tmpdir = /tmp 35 | 36 | log-bin-index = /var/log/mysql/mysql-bin.index 37 | log-bin = /var/log/mysql/mysql-bin 38 | server-id = SERVER_ID 39 | 40 | # Recommended in standard MySQL setup 41 | sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES 42 | 43 | # Disabling symbolic-links is recommended to prevent assorted security risks 44 | symbolic-links=0 45 | 46 | !includedir /etc/mysql/conf.d/ 47 | -------------------------------------------------------------------------------- /mysql_async_replication/image/master/random.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | sed "s/SERVER_ID/${RANDOM}/g" /tmp/my.cnf.tmpl > /etc/mysql/my.cnf 4 | -------------------------------------------------------------------------------- /mysql_async_replication/image/slave/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM debian:wheezy 2 | 3 | # add our user and group first to make sure their IDs get assigned consistently, regardless of whatever dependencies get added 4 | RUN groupadd -r mysql && useradd -r -g mysql mysql 5 | 6 | # FATAL ERROR: please install the following Perl modules before executing /usr/local/mysql/scripts/mysql_install_db: 7 | # File::Basename 8 | # File::Copy 9 | # Sys::Hostname 10 | # Data::Dumper 11 | RUN apt-get update && apt-get install -y perl --no-install-recommends && rm -rf /var/lib/apt/lists/* 12 | 13 | # gpg: key 5072E1F5: public key "MySQL Release Engineering " imported 14 | RUN apt-key adv --keyserver pool.sks-keyservers.net --recv-keys A4A9406876FCBD3C456770C88C718D3B5072E1F5 15 | 16 | ENV MYSQL_MAJOR 5.7 17 | ENV MYSQL_VERSION 5.7.7-rc 18 | 19 | RUN echo "deb http://repo.mysql.com/apt/debian/ wheezy mysql-${MYSQL_MAJOR}-dmr" > /etc/apt/sources.list.d/mysql.list 20 | 21 | # the "/var/lib/mysql" stuff here is because the mysql-server postinst doesn't have an explicit way to disable the mysql_install_db codepath besides having a database already "configured" (ie, stuff in /var/lib/mysql/mysql) 22 | # also, we set debconf keys to make APT a little quieter 23 | RUN { \ 24 | echo mysql-community-server mysql-community-server/data-dir select ''; \ 25 | echo mysql-community-server mysql-community-server/root-pass password ''; \ 26 | echo mysql-community-server mysql-community-server/re-root-pass password ''; \ 27 | echo mysql-community-server mysql-community-server/remove-test-db select false; \ 28 | } | debconf-set-selections \ 29 | && apt-get update && apt-get install -y mysql-server="${MYSQL_VERSION}"* && rm -rf /var/lib/apt/lists/* \ 30 | && rm -rf /var/lib/mysql && mkdir -p /var/lib/mysql 31 | 32 | VOLUME /var/lib/mysql 33 | 34 | COPY my.cnf.tmpl /tmp/my.cnf.tmpl 35 | COPY random.sh /tmp/random.sh 36 | RUN /tmp/random.sh 37 | 38 | COPY docker-entrypoint.sh /entrypoint.sh 39 | ENTRYPOINT ["/entrypoint.sh"] 40 | 41 | EXPOSE 3306 42 | CMD ["mysqld"] 43 | -------------------------------------------------------------------------------- /mysql_async_replication/image/slave/docker-entrypoint.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | set -e 3 | 4 | if [ "${1:0:1}" = '-' ]; then 5 | set -- mysqld "$@" 6 | fi 7 | 8 | if [ "$1" = 'mysqld' ]; then 9 | # read DATADIR from the MySQL config 10 | DATADIR="$("$@" --verbose --help 2>/dev/null | awk '$1 == "datadir" { print $2; exit }')" 11 | 12 | if [ ! -d "$DATADIR/mysql" ]; then 13 | if [ -z "$MYSQL_ROOT_PASSWORD" -a -z "$MYSQL_ALLOW_EMPTY_PASSWORD" ]; then 14 | echo >&2 'error: database is uninitialized and MYSQL_ROOT_PASSWORD not set' 15 | echo >&2 ' Did you forget to add -e MYSQL_ROOT_PASSWORD=... ?' 16 | exit 1 17 | fi 18 | 19 | if [ -z "$MYSQL_REPLICATION_PASSWORD" ]; then 20 | echo >&2 'error: database is uninitialized and MYSQL_REPLICATION_PASSWORD not set' 21 | echo >&2 ' Did you forget to add -e MYSQL_REPLICATION_PASSWORD=... ?' 22 | exit 1 23 | fi 24 | 25 | echo 'Initializing database' 26 | mysqld --initialize-insecure=on --datadir="$DATADIR" 27 | echo 'Database initialized' 28 | 29 | # These statements _must_ be on individual lines, and _must_ end with 30 | # semicolons (no line breaks or comments are permitted). 31 | # TODO proper SQL escaping on ALL the things D: 32 | 33 | tempSqlFile='/tmp/mysql-first-time.sql' 34 | cat > "$tempSqlFile" <<-EOSQL 35 | DELETE FROM mysql.user ; 36 | CREATE USER 'root'@'%' IDENTIFIED BY '${MYSQL_ROOT_PASSWORD}' ; 37 | GRANT ALL ON *.* TO 'root'@'%' WITH GRANT OPTION ; 38 | DROP DATABASE IF EXISTS test ; 39 | EOSQL 40 | 41 | if [ "$MYSQL_DATABASE" ]; then 42 | echo "CREATE DATABASE IF NOT EXISTS \`$MYSQL_DATABASE\` ;" >> "$tempSqlFile" 43 | fi 44 | 45 | if [ "$MYSQL_USER" -a "$MYSQL_PASSWORD" ]; then 46 | echo "CREATE USER '$MYSQL_USER'@'%' IDENTIFIED BY '$MYSQL_PASSWORD' ;" >> "$tempSqlFile" 47 | 48 | if [ "$MYSQL_DATABASE" ]; then 49 | echo "GRANT ALL ON \`$MYSQL_DATABASE\`.* TO '$MYSQL_USER'@'%' ;" >> "$tempSqlFile" 50 | fi 51 | fi 52 | 53 | echo 'FLUSH PRIVILEGES ;' >> "$tempSqlFile" 54 | if [ ! -z "$MYSQL_MASTER_SERVICE_HOST" ]; then 55 | echo "STOP SLAVE;" >> "$tempSqlFile" 56 | echo "CHANGE MASTER TO master_host='$MYSQL_MASTER_SERVICE_HOST', master_user='repl', master_password='repl';">> "$tempSqlFile" 57 | echo "START SLAVE;" >> "$tempSqlFile" 58 | fi 59 | 60 | 61 | set -- "$@" --init-file="$tempSqlFile" 62 | fi 63 | 64 | chown -R mysql:mysql "$DATADIR" 65 | fi 66 | 67 | exec "$@" 68 | -------------------------------------------------------------------------------- /mysql_async_replication/image/slave/my.cnf.tmpl: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 2 | # 3 | # This program is free software; you can redistribute it and/or modify 4 | # it under the terms of the GNU General Public License as published by 5 | # the Free Software Foundation; version 2 of the License. 6 | # 7 | # This program is distributed in the hope that it will be useful, 8 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | # GNU General Public License for more details. 11 | # 12 | # You should have received a copy of the GNU General Public License 13 | # along with this program; if not, write to the Free Software 14 | # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 15 | 16 | # 17 | # The MySQL Community Server configuration file. 18 | # 19 | # For explanations see 20 | # http://dev.mysql.com/doc/mysql/en/server-system-variables.html 21 | 22 | [client] 23 | port = 3306 24 | socket = /var/run/mysqld/mysqld.sock 25 | 26 | [mysqld_safe] 27 | pid-file = /var/run/mysqld/mysqld.pid 28 | socket = /var/run/mysqld/mysqld.sock 29 | nice = 0 30 | 31 | [mysqld] 32 | user = mysql 33 | datadir = /var/lib/mysql/data 34 | tmpdir = /tmp 35 | 36 | log-bin-index = /var/log/mysql/mysql-bin.index 37 | log-bin = /var/log/mysql/mysql-bin 38 | server-id = SERVER_ID 39 | relay-log = /var/log/mysql/mysqld-relay-bin 40 | report-host = HOSTNAME 41 | replicate_wild_ignore_table = mysql.% 42 | 43 | 44 | # Recommended in standard MySQL setup 45 | sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES 46 | 47 | # Disabling symbolic-links is recommended to prevent assorted security risks 48 | symbolic-links=0 49 | 50 | !includedir /etc/mysql/conf.d/ 51 | -------------------------------------------------------------------------------- /mysql_async_replication/image/slave/random.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | hostname=`hostname` 4 | sed "s/SERVER_ID/${RANDOM}/g;s/HOSTNAME/${hostname}/g" /tmp/my.cnf.tmpl > /etc/mysql/my.cnf 5 | -------------------------------------------------------------------------------- /mysql_async_replication/mysql-master-service.json: -------------------------------------------------------------------------------- 1 | { 2 | "id": "mysql-master", 3 | "kind": "Service", 4 | "apiVersion": "v1beta1", 5 | "port": 3306, 6 | "containerPort": 3306, 7 | "selector": { 8 | "name": "mysql-master" 9 | }, 10 | "labels": { 11 | "name": "mysql-master" 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /mysql_async_replication/mysql-master.json: -------------------------------------------------------------------------------- 1 | { 2 | "id": "mysql-master", 3 | "kind": "Pod", 4 | "apiVersion": "v1beta1", 5 | "desiredState": { 6 | "manifest": { 7 | "version": "v1beta1", 8 | "id": "mysql-master", 9 | "containers": [{ 10 | "name": "mysql-master", 11 | "image": "capttofu/mysql_master_kubernetes:latest", 12 | "cpu": 100, 13 | "env": [ 14 | { 15 | "name": "MYSQL_USER", 16 | "value": "mysql" 17 | }, 18 | { 19 | "name": "MYSQL_PASSWORD", 20 | "value": "mysql" 21 | }, 22 | { 23 | "name": "MYSQL_DATABASE", 24 | "value": "sample" 25 | }, 26 | { 27 | "name": "MYSQL_ROOT_PASSWORD", 28 | "value": "root" 29 | }, 30 | { 31 | "name": "MYSQL_REPLICATION_PASSWORD", 32 | "value": "repl" 33 | } 34 | ], 35 | "ports": [{ 36 | "containerPort": 3306, 37 | "hostPort": 3306 38 | }] 39 | }] 40 | } 41 | }, 42 | "labels": { 43 | "name": "mysql-master" 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /mysql_async_replication/mysql-slave-service.json: -------------------------------------------------------------------------------- 1 | { 2 | "id": "mysql-slave", 3 | "kind": "Service", 4 | "apiVersion": "v1beta1", 5 | "port": 3306, 6 | "containerPort": 3306, 7 | "selector": { 8 | "name": "mysql-slave" 9 | }, 10 | "labels": { 11 | "name": "mysql-slave" 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /mysql_async_replication/mysql-slave.json: -------------------------------------------------------------------------------- 1 | { 2 | "id": "mysql-slave", 3 | "kind": "Pod", 4 | "apiVersion": "v1beta1", 5 | "desiredState": { 6 | "manifest": { 7 | "version": "v1beta1", 8 | "id": "mysql-slave", 9 | "containers": [{ 10 | "name": "mysql", 11 | "image": "capttofu/mysql_slave_kubernetes:latest", 12 | "cpu": 100, 13 | "env": [ 14 | { 15 | "name": "MYSQL_USER", 16 | "value": "mysql" 17 | }, 18 | { 19 | "name": "MYSQL_PASSWORD", 20 | "value": "mysql" 21 | }, 22 | { 23 | "name": "MYSQL_DATABASE", 24 | "value": "sample" 25 | }, 26 | { 27 | "name": "MYSQL_ROOT_PASSWORD", 28 | "value": "root" 29 | }, 30 | { 31 | "name": "MYSQL_REPLICATION_PASSWORD", 32 | "value": "repl" 33 | } 34 | ], 35 | "ports": [{ 36 | "containerPort": 3306, 37 | "hostPort": 3306 38 | }] 39 | }] 40 | } 41 | }, 42 | "labels": { 43 | "name": "mysql-slave" 44 | } 45 | } 46 | --------------------------------------------------------------------------------