Skip to content

Commit b53b813

Browse files
authored
Add Support for MariaDB Replication (#15)
Signed-off-by: SK Ali Arman <[email protected]>
1 parent 381b0be commit b53b813

File tree

6 files changed

+495
-0
lines changed

6 files changed

+495
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/usr/bin/env bash
2+
3+
if [ -f /scripts/backup_data_stream_pod_ip.txt ]; then
4+
echo -n "Yes"
5+
else
6+
echo -n "No"
7+
fi

scripts/backup-stream.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env bash
2+
3+
ip=$(cat "/scripts/backup_data_stream_pod_ip.txt")
4+
echo "Start master data transferring..ip $ip"
5+
export MYSQL_PWD="$MYSQL_ROOT_PASSWORD"
6+
mariabackup --backup --stream=mbstream --user=root | socat -u STDIN TCP:$ip:3307
7+
if [ $? -eq 0 ]; then
8+
echo "Backup data for pod $ip transferred successfully."
9+
else
10+
echo "Backup data transfer for pod $ip failed."
11+
fi
12+
rm /scripts/backup_data_stream_pod_ip.txt

scripts/maxscale.sh

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#!/bin/sh
2+
3+
args="$@"
4+
echo "INFO" "Storing default config into /etc/maxscale/maxscale.cnf"
5+
6+
mkdir -p /etc/maxscale/maxscale.cnf.d
7+
cat >>/etc/maxscale/maxscale.cnf <<EOL
8+
[maxscale]
9+
threads=1
10+
log_debug=1
11+
EOL
12+
13+
cat >>/etc/maxscale/maxscale.cnf.d/maxscale.cnf <<EOL
14+
# Auto-generated server list from environment
15+
EOL
16+
17+
serverList=""
18+
# Split HOST_LIST into an array
19+
for ((i=1; i<=REPLICAS; i++)); do
20+
cat >> /etc/maxscale/maxscale.cnf.d/maxscale.cnf <<EOL
21+
[server$i]
22+
type=server
23+
address=$BASE_NAME-$((i - 1)).$GOVERNING_SERVICE_NAME.$POD_NAMESPACE.svc.cluster.local
24+
port=3306
25+
protocol=MariaDBBackend
26+
EOL
27+
if [[ -n "$serverList" ]]; then
28+
serverList+=","
29+
fi
30+
serverList+="server$i"
31+
done
32+
33+
if [[ "${UI:-}" == "true" ]]; then
34+
cat >>/etc/maxscale/maxscale.cnf <<EOL
35+
admin_secure_gui=false
36+
# this enables external access to the REST API outside of localhost
37+
# review / modify for any public / non development environments
38+
admin_host=0.0.0.0
39+
EOL
40+
else
41+
echo "UI is not set to true or does not exist."
42+
fi
43+
44+
cat >>/etc/maxscale/maxscale.cnf.d/maxscale.cnf <<EOL
45+
46+
[ReplicationMonitor]
47+
type=monitor
48+
module=mariadbmon
49+
servers=$serverList
50+
user=monitor_user
51+
password='$MYSQL_ROOT_PASSWORD'
52+
auto_failover=ON
53+
auto_rejoin=ON
54+
enforce_read_only_slaves=true
55+
monitor_interval=2s
56+
replication_user=repl
57+
replication_password='$MYSQL_ROOT_PASSWORD'
58+
EOL
59+
60+
61+
cat >>/etc/maxscale/maxscale.cnf.d/maxscale.cnf <<EOL
62+
63+
[RW-Split-Router]
64+
type=service
65+
router=readwritesplit
66+
servers=$serverList
67+
user=maxscale
68+
password='$MYSQL_ROOT_PASSWORD'
69+
master_reconnection=true
70+
master_failure_mode=fail_on_write
71+
transaction_replay=true
72+
slave_selection_criteria=ADAPTIVE_ROUTING
73+
master_accept_reads=true
74+
EOL
75+
76+
cat >>/etc/maxscale/maxscale.cnf.d/maxscale.cnf <<EOL
77+
78+
[RW-Split-Listener]
79+
type=listener
80+
service=RW-Split-Router
81+
protocol=MariaDBClient
82+
port=3306
83+
EOL
84+
85+
echo "INFO: MaxScale configuration files have been successfully created."
86+
IFS=' '
87+
set -- $args
88+
docker-entrypoint.sh maxscale "$@"
89+
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#!/usr/bin/env bash
2+
3+
script_name=${0##*/}
4+
5+
function timestamp() {
6+
date +"%Y/%m/%d %T"
7+
}
8+
9+
function log() {
10+
local type="$1"
11+
local msg="$2"
12+
echo "$(timestamp) [$script_name] [$type] $msg"
13+
}
14+
15+
# wait for the peer-list file created by coordinator
16+
log "WARNING" "waiting for peer-list file to come"
17+
while [ ! -f "/scripts/peer-list" ]; do
18+
sleep 1
19+
done
20+
21+
log "INFO" "found peer-list file"
22+
23+
# get the comma separated peer names for galera.cnf file
24+
hosts=$(cat "/scripts/peer-list")
25+
26+
log "INFO" "hosts are {$hosts}"
27+
svr_id=$(($(echo -n "${HOSTNAME}" | sed -e "s/${BASE_NAME}-//g") + 1))
28+
29+
echo "Generated server_id -> $svr_id"
30+
echo "Hostname: ${HOSTNAME}"
31+
echo "Base Name: ${BASE_NAME}"
32+
33+
# write configuration file
34+
if [[ $MARIADB_VERSION == "1:11"* ]]; then
35+
cat >>/etc/mysql/conf.d/my.cnf <<EOL
36+
[mariadbd]
37+
log-bin
38+
log_bin=mariadb-bin
39+
log_slave_updates=1
40+
server_id=$svr_id
41+
bind-address=0.0.0.0
42+
gtid_strict_mode=ON
43+
binlog-format=mixed
44+
EOL
45+
else
46+
cat >>/etc/mysql/conf.d/my.cnf <<EOL
47+
[mysqld]
48+
log-bin
49+
log_bin=mariadb-bin
50+
log_slave_updates=1
51+
server_id=$svr_id
52+
bind-address=0.0.0.0
53+
gtid_strict_mode=ON
54+
binlog-format=mixed
55+
EOL
56+
fi
57+
58+
# wait for the pre script copied by coordinator
59+
log "WARNING" "waiting for pre-run-on-present script to come"
60+
while [ ! -f "/run-script/pre-run-on-present.sh" ]; do
61+
sleep 1
62+
done
63+
64+
log "INFO" "found pre-run-on-present script"
65+
66+
# run the pre script copied by mariadb-coordinator
67+
./run-script/pre-run-on-present.sh
68+
69+
# wait for the script copied by coordinator
70+
log "WARNING" "waiting for run-on-present script to come"
71+
while [ ! -f "/run-script/run-on-present.sh" ]; do
72+
sleep 1
73+
done
74+
75+
log "INFO" "found run-on-present script"
76+
77+
# run the script copied by mariadb-coordinator and pass the arguments
78+
./run-script/run-on-present.sh $@

scripts/std-replication-run.sh

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/usr/bin/env bash
2+
3+
script_name=${0##*/}
4+
5+
function timestamp() {
6+
date +"%Y/%m/%d %T"
7+
}
8+
9+
function log() {
10+
local type="$1"
11+
local msg="$2"
12+
echo "$(timestamp) [$script_name] [$type] $msg"
13+
}
14+
15+
# include directory in my.cnf for custom-config
16+
if [ ! -z "$(ls -A /etc/mysql/custom.conf.d/)" ]; then
17+
echo '!includedir /etc/mysql/custom.conf.d/' >>/etc/mysql/my.cnf
18+
fi
19+
20+
while [ true ]; do
21+
log "INFO" "initializing run.sh"
22+
23+
# clean up old files
24+
rm -rf /run-script/*
25+
if [ -f "/scripts/peer-list" ]; then
26+
rm /scripts/peer-list
27+
fi
28+
29+
if [ -f "/scripts/seqno" ]; then
30+
rm /scripts/seqno
31+
fi
32+
33+
# start on-start script
34+
./scripts/std-replication-on-start.sh $@
35+
sleep 1
36+
done

0 commit comments

Comments
 (0)