| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- ---
- # 1. ConfigMap
- apiVersion: v1
- kind: ConfigMap
- metadata:
- name: mysql-slave-config
- namespace: mysql
- data:
- my.cnf: |
- [mysqld]
- server-id=2
- relay-log=mysql-relay-bin
- log-bin=mysql-bin
- binlog-format=ROW
- gtid-mode=ON
- enforce-gtid-consistency=ON
- skip-replica-start=ON
- default-authentication-plugin=mysql_native_password
- bind-address=0.0.0.0
- # 注意:不要在这里设置 read_only 或 super_read_only
- # 这些应该在复制配置完成后再设置
- ---
- # 2. PersistentVolume (Slave专用)
- apiVersion: v1
- kind: PersistentVolume
- metadata:
- name: mysql-slave-pv
- namespace: mysql
- labels:
- app: mysql
- role: slave
- spec:
- capacity:
- storage: 10Gi
- accessModes:
- - ReadWriteOnce
- persistentVolumeReclaimPolicy: Retain
- storageClassName: manual-slave
- hostPath:
- path: "/home/lq/mysql/slave/data" # 修改为你的本地路径
- type: DirectoryOrCreate
- ---
- # 3. PersistentVolumeClaim (Slave专用)
- apiVersion: v1
- kind: PersistentVolumeClaim
- metadata:
- name: mysql-slave-pvc
- namespace: mysql
- labels:
- app: mysql
- role: slave
- spec:
- storageClassName: manual-slave
- accessModes:
- - ReadWriteOnce
- resources:
- requests:
- storage: 10Gi
- ---
- # 4.Server配置
- apiVersion: v1
- kind: Service
- metadata:
- name: mysql-slave
- namespace: mysql
- labels:
- app: mysql
- role: slave
- spec:
- ports:
- - port: 3306
- name: mysql
- selector:
- app: mysql
- role: slave
- ---
- # 5. Deployment
- apiVersion: apps/v1
- kind: Deployment
- metadata:
- name: mysql-slave
- namespace: mysql
- labels:
- app: mysql
- role: slave
- spec:
- replicas: 1
- selector:
- matchLabels:
- app: mysql
- role: slave
- strategy:
- type: Recreate # 重要:MySQL Slave也需要单实例
- template:
- metadata:
- labels:
- app: mysql
- role: slave
- spec:
- containers:
- - name: mysql
- image: mysql:8.0.44
- env:
- - name: MYSQL_ROOT_PASSWORD
- value: "Lq123456!"
- ports:
- - containerPort: 3306
- volumeMounts:
- - name: config
- mountPath: /etc/mysql/conf.d/
- resources:
- requests:
- memory: "512Mi"
- cpu: "250m"
- lifecycle:
- postStart:
- exec:
- command:
- - /bin/bash
- - -c
- - |
- # 等待MySQL完全启动
- echo "等待MySQL启动..."
- for i in {1..30}; do
- if mysql -uroot -pLq123456! -e "SELECT 1" &>/dev/null; then
- echo "MySQL已启动"
- break
- fi
- echo "等待MySQL... ($i/30)"
- sleep 2
- done
-
- # 配置复制
- echo "配置主从复制..."
- mysql -uroot -pLq123456! <<EOF
- STOP REPLICA;
- RESET REPLICA ALL;
- CHANGE REPLICATION SOURCE TO
- SOURCE_HOST='mysql-master',
- SOURCE_USER='replica',
- SOURCE_PASSWORD='Replica123456!',
- SOURCE_AUTO_POSITION=1;
- START REPLICA;
- EOF
-
- # 设置只读模式
- echo "设置只读模式..."
- mysql -uroot -pLq123456! -e "SET GLOBAL read_only = ON; SET GLOBAL super_read_only = ON;"
-
- echo "配置完成"
- mysql -uroot -pLq123456! -e "SHOW REPLICA STATUS\G" | grep -E "Replica_IO_Running|Replica_SQL_Running"
- volumes:
- - name: config
- configMap:
- name: mysql-slave-config
- - name: data
- persistentVolumeClaim:
- claimName: mysql-slave-pvc
|