app.kubernetes.io/name: mysql
# https://kubernetes.io/docs/concepts/services-networking/service/
app.kubernetes.io/name: mysql
# https://kubernetes.io/docs/concepts/services-networking/service/
app.kubernetes.io/name: mysql
app.kubernetes.io/name: mysql
# https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/
app.kubernetes.io/name: mysql
# 基于 Pod 序号生成 MySQL 服务器的 ID。
[[ $HOSTNAME =~ -([0-9]+)$ ]] || exit 1
ordinal=${BASH_REMATCH[1]}
echo [mysqld] > /mnt/conf.d/server-id.cnf
# 添加偏移量以避免使用 server-id=0 这一保留值。
echo server-id=$((100 + $ordinal)) >> /mnt/conf.d/server-id.cnf
# 将合适的 conf.d 文件从 config-map 复制到 emptyDir。
if [[ $ordinal -eq 0 ]]; then
cp /mnt/config-map/primary.cnf /mnt/conf.d/
cp /mnt/config-map/replica.cnf /mnt/conf.d/
mountPath: /mnt/config-map
image: ist0ne/xtrabackup:1.0
[[ -d /var/lib/mysql/mysql ]] && exit 0
[[ `hostname` =~ -([0-9]+)$ ]] || exit 1
ordinal=${BASH_REMATCH[1]}
[[ $ordinal -eq 0 ]] && exit 0
ncat --recv-only mysql-$(($ordinal-1)).mysql-svc 3307 | xbstream -x -C /var/lib/mysql
xtrabackup --prepare --target-dir=/var/lib/mysql
mountPath: /var/lib/mysql
mountPath: /etc/mysql/conf.d
- name: MYSQL_ALLOW_EMPTY_PASSWORD
- mountPath: /var/lib/mysql
mountPath: /etc/mysql/conf.d
image: ist0ne/xtrabackup:1.0
# 确定克隆数据的 binlog 位置(如果有的话)。
if [[ -f xtrabackup_slave_info && "x$(<xtrabackup_slave_info)" != "x" ]]; then
# XtraBackup 已经生成了部分的 “CHANGE MASTER TO” 查询
# 因为我们从一个现有副本进行克隆。(需要删除末尾的分号!)
cat xtrabackup_slave_info | sed -E 's/;$//g' > change_master_to.sql.in
# 在这里要忽略 xtrabackup_binlog_info (它是没用的)。
rm -f xtrabackup_slave_info xtrabackup_binlog_info
elif [[ -f xtrabackup_binlog_info ]]; then
# 我们直接从主实例进行克隆。解析 binlog 位置。
[[ `cat xtrabackup_binlog_info` =~ ^(.*?)[[:space:]]+(.*?)$ ]] || exit 1
rm -f xtrabackup_binlog_info xtrabackup_slave_info
echo "CHANGE MASTER TO MASTER_LOG_FILE='${BASH_REMATCH[1]}',\
MASTER_LOG_POS=${BASH_REMATCH[2]}" > change_master_to.sql.in
if [[ -f change_master_to.sql.in ]]; then
echo "Waiting for mysqld to be ready (accepting connections)"
until mysql -h 127.0.0.1 -e "SELECT 1"; do sleep 1; done
echo "Initializing replication from clone position"
-e "$(<change_master_to.sql.in), \
MASTER_HOST='mysql-0.mysql-svc', \
MASTER_CONNECT_RETRY=10; \
mv change_master_to.sql.in change_master_to.sql.orig
exec ncat --listen --keep-open --send-only --max-conns=1 3307 -c \
"xtrabackup --backup --slave-info --stream=xbstream --host=127.0.0.1 --user=root"
mountPath: /var/lib/mysql
mountPath: /etc/mysql/conf.d
accessModes: ["ReadWriteOnce"]
storageClassName: "nfs-client-159"