1、下载相关镜像并上传到harbor私有仓库
拉取官网镜像
nerdctl pull registry.cn-hangzhou.aliyuncs.com/hxpdocker/xtrabackup:1.0 nerdctl pull MysqL:5.7.36
对镜像tag标签重打,方便推送到自己的harbor私有仓库 nerdctl tag registry.cn-hangzhou.aliyuncs.com/hxpdocker/xtrabackup:1.0 harbor.magedu.net/baseimages/xtrabackup:1.0 nerdctl tag MysqL:5.7.36 harbor.magedu.net/baseimages/MysqL:5.7.36
将镜像推送到harbor私有仓库 nerdctl push harbor.magedu.net/baseimages/xtrabackup:1.0 nerdctl push harbor.magedu.net/baseimages/MysqL:5.7.39
2、在nfs共享目录创建MysqL pv目录
mkdir -pv /nfs_share/k8sdata/magedu/MysqL-datadir-{1..6}
3、创建pv
kubectl apply -f MysqL-persistentvolume.yaml
root@easzlab-deploy:~/MysqL/pv# cat MysqL-persistentvolume.yaml --- apiVersion: v1 kind: PersistentVolume Metadata: name: MysqL-datadir-1 namespace: magedu spec: capacity: storage: 50Gi accessModes: - ReadWriteOnce nfs: path: /nfs_share/k8sdata/magedu/MysqL-datadir-1 server: 172.16.88.169 --- apiVersion: v1 kind: PersistentVolume Metadata: name: MysqL-datadir-2 namespace: magedu spec: capacity: storage: 50Gi accessModes: - ReadWriteOnce nfs: path: /nfs_share/k8sdata/magedu/MysqL-datadir-2 server: 172.16.88.169 --- apiVersion: v1 kind: PersistentVolume Metadata: name: MysqL-datadir-3 namespace: magedu spec: capacity: storage: 50Gi accessModes: - ReadWriteOnce nfs: path: /nfs_share/k8sdata/magedu/MysqL-datadir-3 server: 172.16.88.169 --- apiVersion: v1 kind: PersistentVolume Metadata: name: MysqL-datadir-4 namespace: magedu spec: capacity: storage: 50Gi accessModes: - ReadWriteOnce nfs: path: /nfs_share/k8sdata/magedu/MysqL-datadir-4 server: 172.16.88.169 --- apiVersion: v1 kind: PersistentVolume Metadata: name: MysqL-datadir-5 namespace: magedu spec: capacity: storage: 50Gi accessModes: - ReadWriteOnce nfs: path: /nfs_share/k8sdata/magedu/MysqL-datadir-5 server: 172.16.88.169 --- apiVersion: v1 kind: PersistentVolume Metadata: name: MysqL-datadir-6 namespace: magedu spec: capacity: storage: 50Gi accessModes: - ReadWriteOnce nfs: path: /nfs_share/k8sdata/magedu/MysqL-datadir-6 server: 172.16.88.169 root@easzlab-deploy:~/MysqL/pv#View Code
root@easzlab-deploy:~/MysqL# cat MysqL-configmap.yaml apiVersion: v1 kind: ConfigMap Metadata: name: MysqL namespace: magedu labels: app: MysqL data: master.cnf: | # Apply this config only on the master. [MysqLd] log-bin log_bin_trust_function_creators=1 lower_case_table_names=1 slave.cnf: | # Apply this config only on slaves. [MysqLd] super-read-only log_bin_trust_function_creators=1 root@easzlab-deploy:~/MysqL#
5、创建MysqL-service
root@easzlab-deploy:~/MysqL# cat MysqL-services.yaml # Headless service for stable DNS entries of StatefulSet members. apiVersion: v1 kind: Service Metadata: namespace: magedu name: MysqL labels: app: MysqL spec: ports: - name: MysqL port: 3306 clusterIP: None selector: app: MysqL --- # Client service for connecting to any MysqL instance for reads. # For writes, you must instead connect to the master: MysqL-0.MysqL. apiVersion: v1 kind: Service Metadata: name: MysqL-read namespace: magedu labels: app: MysqL spec: ports: - name: MysqL port: 3306 selector: app: MysqL root@easzlab-deploy:~/MysqL#
6、创建statefulset文件
root@easzlab-deploy:~/MysqL# cat MysqL-statefulset.yaml apiVersion: apps/v1 kind: StatefulSet Metadata: name: MysqL namespace: magedu spec: selector: matchLabels: app: MysqL serviceName: MysqL replicas: 3 template: Metadata: labels: app: MysqL spec: initContainers: - name: init-MysqL #初始化容器1、基于当前pod name匹配角色是master还是slave,并动态生成相对应的配置文件 image: harbor.magedu.net/baseimages/MysqL:5.7.36 command: - bash - "-c" - | set -ex # Generate MysqL server-id from pod ordinal index. [[ `hostname` =~ -([0-9]+)$ ]] || exit 1 #匹配hostname的最后一位、最后是一个顺序叠加的整数 ordinal=${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 ]]; then #如果是master、则cpmaster配置文件 cp /mnt/config-map/master.cnf /mnt/conf.d/ else #否则cp slave配置文件 cp /mnt/config-map/slave.cnf /mnt/conf.d/ fi volumeMounts: - name: conf #临时卷、emptyDir mountPath: /mnt/conf.d - name: config-map mountPath: /mnt/config-map - name: clone-MysqL #初始化容器2、用于生成MysqL配置文件、并从上一个pod完成首次的全量数据clone(slave 3从slave2 clone,而不是每个slave都从master clone实现首次全量同步,但是后期都是与master实现增量同步) image: harbor.magedu.net/baseimages/xtrabackup:1.0 command: - bash - "-c" - | set -ex # Skip the clone if data already exists. [[ -d /var/lib/MysqL/MysqL ]] && exit 0 # Skip the clone on master (ordinal index 0). [[ `hostname` =~ -([0-9]+)$ ]] || exit 1 ordinal=${BASH_REMATCH[1]} [[ $ordinal -eq 0 ]] && exit 0 #如果最后一位是0(master)则退出clone过程 # Clone data from prevIoUs peer. ncat --recv-only MysqL-$(($ordinal-1)).MysqL 3307 | xbstream -x -C /var/lib/MysqL #从上一个pod执行clone(binlog),xbstream为解压缩命令 # Prepare the backup.xue xtrabackup --prepare --target-dir=/var/lib/MysqL #通过xtrabackup恢复binlog volumeMounts: - name: data mountPath: /var/lib/MysqL subPath: MysqL - name: conf mountPath: /etc/MysqL/conf.d containers: - name: MysqL #业务容器1(MysqL主容器) image: harbor.magedu.net/baseimages/MysqL:5.7.36 env: - name: MysqL_ALLOW_EMPTY_PASSWORD value: "1" ports: - name: MysqL containerPort: 3306 volumeMounts: - name: data #挂载数据目录至/var/lib/MysqL mountPath: /var/lib/MysqL subPath: MysqL - name: conf #配置文件/etc/MysqL/conf.d mountPath: /etc/MysqL/conf.d resources: #资源限制 requests: cpu: 500m memory: 1Gi livenessProbe: #存活探针 exec: command: ["MysqLadmin", "ping"] initialDelaySeconds: 30 periodSeconds: 10 timeoutSeconds: 5 readinessProbe: #就绪探针 exec: # Check we can execute queries over TCP (skip-networking is off). command: ["MysqL", "-h", "127.0.0.1", "-e", "SELECT 1"] initialDelaySeconds: 5 periodSeconds: 2 timeoutSeconds: 1 - name: xtrabackup #业务容器2(xtrabackup),用于后期同步master 的binglog并恢复数据 image: harbor.magedu.net/baseimages/xtrabackup:1.0 ports: - name: xtrabackup containerPort: 3307 command: - bash - "-c" - | set -ex cd /var/lib/MysqL # Determine binlog position of cloned data, if any. if [[ -f xtrabackup_slave_info ]]; then # XtraBackup already generated a partial "CHANGE MASTER TO" query # because we're cloning from an existing slave. mv xtrabackup_slave_info change_master_to.sql.in # Ignore xtrabackup_binlog_info in this case (it's useless). rm -f xtrabackup_binlog_info elif [[ -f xtrabackup_binlog_info ]]; then # We're cloning directly from master. Parse binlog position. [[ `cat xtrabackup_binlog_info` =~ ^(.*?)[[:space:]]+(.*?)$ ]] || exit 1 rm xtrabackup_binlog_info echo "CHANGE MASTER TO MASTER_LOG_FILE='${BASH_REMATCH[1]}',\ MASTER_LOG_POS=${BASH_REMATCH[2]}" > change_master_to.sql.in #生成CHANGE MASTER命令 fi # Check if we need to complete a clone by starting replication. 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" # In case of container restart, attempt this at-most-once. mv change_master_to.sql.in change_master_to.sql.orig #执行CHANGE MASTER操作并启动SLAVE MysqL -h 127.0.0.1 <<EOF $(<change_master_to.sql.orig), MASTER_HOST='MysqL-0.MysqL', MASTER_USER='root', MASTER_PASSWORD='', MASTER_CONNECT_RETRY=10; START SLAVE; EOF fi # Start a server to send backups when requested by peers. #监听在3307端口,用于为下一个pod同步全量数据 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: data mountPath: /var/lib/MysqL subPath: MysqL - name: conf mountPath: /etc/MysqL/conf.d resources: requests: cpu: 100m memory: 100Mi volumes: - name: conf emptyDir: {} - name: config-map configMap: name: MysqL volumeClaimTemplates: - Metadata: name: data spec: accessModes: ["ReadWriteOnce"] resources: requests: storage: 10Gi root@easzlab-deploy:~/MysqL#
7、安装部署前确认保证MysqL nfs共享目录下无其他文件
8、查看MysqL-0主库状态
root@easzlab-deploy:~# kubectl exec -it MysqL-0 -n magedu bash kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead. Defaulted container "MysqL" out of: MysqL, xtrabackup, init-MysqL (init), clone-MysqL (init) root@MysqL-0:/# MysqL Welcome to the MysqL monitor. Commands end with ; or \g. Your MysqL connection id is 427 Server version: 5.7.36-log MysqL Community Server (GPL) copyright (c) 2000, 2021, Oracle and/or its affiliates. Oracle is a registered Trademark of Oracle Corporation and/or its affiliates. Other names may be Trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MysqL> show master status; +--------------------+----------+--------------+------------------+-------------------+ | File | Position | binlog_Do_DB | binlog_Ignore_DB | Executed_Gtid_Set | +--------------------+----------+--------------+------------------+-------------------+ | MysqL-0-bin.000005 | 328 | | | | +--------------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec) MysqL>
9、查看MysqL-1从库状态
root@easzlab-deploy:~# kubectl exec -it MysqL-1 -n magedu bash kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead. Defaulted container "MysqL" out of: MysqL, xtrabackup, init-MysqL (init), clone-MysqL (init) root@MysqL-1:/# MysqL Welcome to the MysqL monitor. Commands end with ; or \g. Your MysqL connection id is 614 Server version: 5.7.36 MysqL Community Server (GPL) copyright (c) 2000, 2021, Oracle and/or its affiliates. Oracle is a registered Trademark of Oracle Corporation and/or its affiliates. Other names may be Trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MysqL> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: MysqL-0.MysqL Master_User: root Master_Port: 3306 Connect_Retry: 10 Master_Log_File: MysqL-0-bin.000003 Read_Master_Log_Pos: 328 Relay_Log_File: MysqL-1-relay-bin.000002 Relay_Log_Pos: 496 Relay_Master_Log_File: MysqL-0-bin.000003 Slave_IO_Running: Yes Slave_sql_Running: Yes Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0 Last_Error: Skip_Counter: 0 Exec_Master_Log_Pos: 328 Relay_Log_Space: 705 Until_Condition: None Until_Log_File: Until_Log_Pos: 0 Master_SSL_Allowed: No Master_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 0 Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_sql_Errno: 0 Last_sql_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 100 Master_UUID: 18abe35c-2230-11ed-8807-62f8b68c9a0f Master_Info_File: /var/lib/MysqL/master.info sql_Delay: 0 sql_Remaining_Delay: NULL Slave_sql_Running_State: Slave has read all relay log; waiting for more updates Master_Retry_Count: 86400 Master_Bind: Last_IO_Error_Timestamp: Last_sql_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: Auto_Position: 0 Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version: 1 row in set (0.00 sec) ERROR: No query specified MysqL>
10、查看MysqL-2从库状态
root@easzlab-deploy:~# kubectl exec -it MysqL-2 -n magedu bash kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead. Defaulted container "MysqL" out of: MysqL, xtrabackup, init-MysqL (init), clone-MysqL (init) root@MysqL-2:/# MysqL Welcome to the MysqL monitor. Commands end with ; or \g. Your MysqL connection id is 652 Server version: 5.7.36 MysqL Community Server (GPL) copyright (c) 2000, 2021, Oracle and/or its affiliates. Oracle is a registered Trademark of Oracle Corporation and/or its affiliates. Other names may be Trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MysqL> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: MysqL-0.MysqL Master_User: root Master_Port: 3306 Connect_Retry: 10 Master_Log_File: MysqL-0-bin.000003 Read_Master_Log_Pos: 328 Relay_Log_File: MysqL-2-relay-bin.000002 Relay_Log_Pos: 496 Relay_Master_Log_File: MysqL-0-bin.000003 Slave_IO_Running: Yes Slave_sql_Running: Yes Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0 Last_Error: Skip_Counter: 0 Exec_Master_Log_Pos: 328 Relay_Log_Space: 705 Until_Condition: None Until_Log_File: Until_Log_Pos: 0 Master_SSL_Allowed: No Master_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 0 Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_sql_Errno: 0 Last_sql_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 100 Master_UUID: 18abe35c-2230-11ed-8807-62f8b68c9a0f Master_Info_File: /var/lib/MysqL/master.info sql_Delay: 0 sql_Remaining_Delay: NULL Slave_sql_Running_State: Slave has read all relay log; waiting for more updates Master_Retry_Count: 86400 Master_Bind: Last_IO_Error_Timestamp: Last_sql_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: Auto_Position: 0 Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version: 1 row in set (0.01 sec) ERROR: No query specified MysqL>
11、在主库MysqL-0测试创建新的数据是否会同步到MysqL-1、MysqL-2
15、再次创建新的数据,检查是否会同步
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。