xtrabackup

  Uncategorized

create stream

ulimit -n 10000

mysqlrootpassxtrabackup="123456"

ncat --listen --keep-open --send-only --exec \
"/root/xtrabackup/bin/xtrabackup \
--defaults-file=/etc/mysql/mysql.conf.d/mysqld.cnf \
--backup --slave-info --stream=xbstream \
--host=localhost --user=root --password=$mysqlrootpassxtrabackup" \
0.0.0.0 3307

Handle stream, receive data xtrabackup

ncat --recv-only {{.Values.xtrabackup.host}} 3307 | xbstream -x -C /var/lib/mysql
xtrabackup --prepare --target-dir=/var/lib/mysql

k8s mysql replica

apiVersion: v1
kind: ConfigMap
metadata:
    name: {{.Values.name}}
    labels:
        app: {{.Values.name}}
data:
    replica.cnf: |
        [mysqld]
        super-read-only
        sql-mode="STRICT_ALL_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER"
        max_connections=100
        interactive_timeout=900
        query_cache_type=1
        query_cache_limit=256K
        query_cache_min_res_unit=2k
        query_cache_size=32M
        innodb_file_per_table=1
        wait_timeout=900
        expire_logs_days=2
        innodb_temp_data_file_path=ibtmp1:12M:autoextend:max:5G
apiVersion: apps/v1
kind: StatefulSet
metadata:
    name: {{.Values.name}}
spec:
    selector:
        matchLabels:
            app: {{.Values.name}}
    serviceName: {{.Values.name}}
    replicas: {{.Values.replicas}}
    template:
        metadata:
            labels:
                app: {{.Values.name}}
        spec:
            initContainers:
                -   name: {{.Values.name}}-init
                    image: mysql:5.7
                    command:
                        - bash
                        - "-c"
                        - |
                            [[ $HOSTNAME =~ -([0-9]+)$ ]] || exit 1
                            ordinal=${BASH_REMATCH[1]}
                            echo [mysqld] > /mnt/conf.d/server-id.cnf
                            echo server-id=$((200 + $ordinal)) >> /mnt/conf.d/server-id.cnf
                            cp /mnt/config-map/replica.cnf /mnt/conf.d/
                    volumeMounts:
                        -   name: conf
                            mountPath: /mnt/conf.d
                        -   name: config-map
                            mountPath: /mnt/config-map
                -   name: {{.Values.name}}-clone
                    image: public.ecr.aws/q9j7f0j7/xtrabackup
                    command:
                        - bash
                        - "-c"
                        - |
                            set -ex
                            ncat --recv-only {{.Values.xtrabackup.host}} {{.Values.xtrabackup.port}} | xbstream -x -C /var/lib/mysql
                            xtrabackup --prepare --target-dir=/var/lib/mysql
                    volumeMounts:
                        -   name: conf
                            mountPath: /mnt/conf.d
                        -   name: config-map
                            mountPath: /mnt/config-map
                        -   name: data
                            mountPath: /var/lib/mysql
                            subPath: mysql
            containers:
                -   name: {{.Values.name}}
                    image: mysql:5.7
                    resources:
                        requests:
                            cpu: 1000m
                            memory: 2Gi
                    env:
                        -   name: MYSQL_ROOT_PASSWORD
                            value: "{{.Values.pass}}"
                    ports:
                        -   name: mysql
                            containerPort: 3306
                    volumeMounts:
                        -   name: data
                            mountPath: /var/lib/mysql
                            subPath: mysql
                        -   name: conf
                            mountPath: /etc/mysql/conf.d
                    livenessProbe:
                        exec:
                            command: [ "mysqladmin", "--host=localhost","--user=root","--password={{.Values.pass}}", "ping" ]
                        initialDelaySeconds: 30
                        periodSeconds: 10
                        timeoutSeconds: 5
                    readinessProbe:
                        exec:
                            command: [ "mysql", "--host=localhost","--user=root","--password={{.Values.pass}}", "-e", "SELECT 1" ]
                        initialDelaySeconds: 5
                        periodSeconds: 2
                        timeoutSeconds: 1
                    lifecycle:
                        postStart:
                            exec:
                                command:
                                    - bash
                                    - "-c"
                                    - |
                                        set -ex
                                        set +H
                                        passroot='{{.Values.pass}}'
                                        cd /var/lib/mysql
                                        if [[ -f xtrabackup_slave_info && "x$(<xtrabackup_slave_info)" != "x" ]]; then
                                            cat xtrabackup_slave_info | sed -E 's/;$//g' > change_master_to.sql.in
                                            rm -f xtrabackup_slave_info xtrabackup_binlog_info
                                        elif [[ -f xtrabackup_binlog_info ]]; then
                                            [[ `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
                                        fi

                                        if [[ -f change_master_to.sql.in ]]; then
                                            until mysql --user="root" --password=$passroot -e "SELECT 1"; do sleep 1; done
                                            mysql --user="root" --password=$passroot \
                                                -e "$(<change_master_to.sql.in), \
                                                    MASTER_HOST='{{.Values.master_db.host}}', \
                                                    MASTER_USER='{{.Values.master_db.user}}', \
                                                    MASTER_PASSWORD='{{.Values.master_db.pass}}', \
                                                    MASTER_CONNECT_RETRY=10; \
                                                    START SLAVE;" || exit 1
                                            mv change_master_to.sql.in change_master_to.sql.orig
                                        fi
            volumes:
                -   name: conf
                    emptyDir: { }
                -   name: data
                    emptyDir: { }
                -   name: config-map
                    configMap:
                        name: {{.Values.name}}
apiVersion: v1
kind: Service
metadata:
    name: {{.Values.name}}
    labels:
        app: {{.Values.name}}
spec:
    ports:
        -   name: mysql
            port: 3306
    clusterIP: None
    selector:
        app: {{.Values.name}}
---
apiVersion: v1
kind: Service
metadata:
    name: {{.Values.name}}-read
    labels:
        app: {{.Values.name}}
        readonly: "true"
spec:
    type: NodePort
    ports:
        -   name: mysql
            port: 3306
            nodePort: 30306
    selector:
        app: {{.Values.name}}
name: "mysql-replica"
pass: "123456"
replicas: 1
xtrabackup:
    host: "xxx.xxx.xxx.xxx"
    port: 3307
master_db:
    host: "xxx.xxx.xxx.xxx"
    user: "readslave"
    pass: "123456"