[root@k8s-n0 nacos]# cat ../mysql/sts/mysql-cluster.yaml apiVersion: v1
kind: ConfigMap
metadata:name: mysql-configmapnamespace: dmgeo-liblabels:app: mysql
data:primary.cnf:|# Apply this config only on the primary.[mysqld]log-binreplica.cnf:|# Apply this config only on replicas.[mysqld]super-read-only---# Headless service for stable DNS entries of StatefulSet members.apiVersion: v1
kind: Service
metadata:name: mysqlnamespace: dmgeo-liblabels:app: mysql
spec:ports:-name: mysqlport:3306clusterIP: Noneselector:app: mysql
---# Client service for connecting to any MySQL instance for reads.# For writes, you must instead connect to the primary: mysql-0.mysql.apiVersion: v1
kind: Service
metadata:name: mysql-readnamespace: dmgeo-liblabels:app: mysql
spec:ports:-name: mysqlport:3306selector:app: mysql---apiVersion: apps/v1
kind: StatefulSet
metadata:name: mysqlnamespace: dmgeo-lib
spec:selector:matchLabels:app: mysqlserviceName: mysqlreplicas:3 template:metadata:labels:app: mysqlspec:initContainers:-name: init-mysqlimage: 192.168.145.28:1603/lego/mysql/mysql:5.7command:- bash-"-c"-|set -ex# Generate mysql server-id from pod ordinal index.[[ `hostname` =~ -([0-9]+)$ ]] || exit 1ordinal=${BASH_REMATCH[1]}echo [mysqld] > /mnt/conf.d/server-id.cnf# Add an offset to avoid reserved server-id=0 value.echo server-id=$((100 + $ordinal)) >> /mnt/conf.d/server-id.cnf# Copy appropriate conf.d files from config-map to emptyDir.if [[ $ordinal -eq 0 ]]; thencp /mnt/config-map/primary.cnf /mnt/conf.d/elsecp /mnt/config-map/replica.cnf /mnt/conf.d/fivolumeMounts:-name: mysql-confmountPath: /mnt/conf.d-name: mysql-configmapmountPath: /mnt/config-map-name: clone-mysqlimage: 192.168.145.28:1603/lego/mysql/twoeo/gcr.io-google-samples-xtrabackup:latestcommand:- bash-"-c"-|set -ex# Skip the clone if data already exists.[[ -d /var/lib/mysql/mysql ]] && exit 0# Skip the clone on primary (ordinal index 0).[[ `hostname` =~ -([0-9]+)$ ]] || exit 1ordinal=${BASH_REMATCH[1]}[[ $ordinal -eq 0 ]] && exit 0# Clone data from previous peer.ncat --recv-only mysql-$(($ordinal-1)).mysql 3307 | xbstream -x -C /var/lib/mysql# Prepare the backup.xtrabackup --prepare --target-dir=/var/lib/mysqlvolumeMounts:-name: mysql-datamountPath: /var/lib/mysqlsubPath: mysql-name: mysql-confmountPath: /etc/mysql/conf.dcontainers:-name: mysqlimage: 192.168.145.28:1603/lego/mysql/mysql:5.7env:#- name: MYSQL_ROOT_PASSWORD# value: 123456-name: MYSQL_ALLOW_EMPTY_PASSWORDvalue:"1"-name: MYSQL_ROOT_PASSWORDvalue:"root"-name: MYSQL_DATABASEvalue:"nacos_config"-name: MYSQL_USERvalue:"nacos"-name: MYSQL_PASSWORDvalue:"nacos"ports:-name: mysqlcontainerPort:3306volumeMounts:-name: mysql-datamountPath: /var/lib/mysqlsubPath: mysql-name: mysql-confmountPath: /etc/mysql/conf.dresources:requests:cpu: 500mmemory: 1GilivenessProbe:exec:command:["mysqladmin","ping"]initialDelaySeconds:30periodSeconds:10timeoutSeconds:5readinessProbe:exec:# Check we can execute queries over TCP (skip-networking is off).command:["mysql","-h","127.0.0.1","-e","SELECT 1"]initialDelaySeconds:5periodSeconds:2timeoutSeconds:1-name: xtrabackupimage: 192.168.145.28:1603/lego/mysql/twoeo/gcr.io-google-samples-xtrabackup:latestports:-name: xtrabackupcontainerPort:3307command:- bash-"-c"-|set -excd /var/lib/mysql# Determine binlog position of cloned data, if any.if [[-f xtrabackup_slave_info && "x$(<xtrabackup_slave_info)" != "x" ]]; then# XtraBackup already generated a partial "CHANGE MASTER TO" query# because we're cloning from an existing replica. (Need to remove the tailing semicolon!)cat xtrabackup_slave_info | sed -E 's/;$//g' > change_master_to.sql.in# Ignore xtrabackup_binlog_info in this case (it's useless).rm -f xtrabackup_slave_info xtrabackup_binlog_infoelif [[-f xtrabackup_binlog_info ]]; then# We're cloning directly from primary. Parse binlog position.[[ `cat xtrabackup_binlog_info` =~ ^(.*?)[[:space:]]+(.*?)$ ]]|| exit 1rm -f xtrabackup_binlog_info xtrabackup_slave_infoecho "CHANGE MASTER TO MASTER_LOG_FILE='${BASH_REMATCH[1]}',\MASTER_LOG_POS=${BASH_REMATCH[2]}" > change_master_to.sql.infi# Check if we need to complete a clone by starting replication.if [[-f change_master_to.sql.in ]]; thenecho "Waiting for mysqld to be ready (accepting connections)"until mysql -h 127.0.0.1 -e "SELECT 1"; do sleep 1; doneecho "Initializing replication from clone position"mysql -h 127.0.0.1 \-e "$(<change_master_to.sql.in), \MASTER_HOST='mysql-0.mysql', \MASTER_USER='root', \MASTER_PASSWORD='', \MASTER_CONNECT_RETRY=10; \START SLAVE;" || exit 1# In case of container restart, attempt this at-most-once.mv change_master_to.sql.in change_master_to.sql.origfi# Start a server to send backups when requested by peers.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"volumeMounts:-name: mysql-datamountPath: /var/lib/mysqlsubPath: mysql-name: mysql-confmountPath: /etc/mysql/conf.dresources:requests:cpu: 100mmemory: 100Mivolumes:-name: mysql-confemptyDir:{}-name: mysql-configmapconfigMap:name: mysql-configmapvolumeClaimTemplates:-metadata:name: mysql-dataspec:accessModes:["ReadWriteOnce"]resources:requests:storage: 10Gi