版本
kubernetes: 1.17.3
mysql: 5.7
2. 创建mysql-master-rc.yaml文件
[root@master k8s]# cat mysql-master-rc.yaml
apiVersion: v1
kind: ReplicationController
metadata:
name: mysql-master-rc
namespace: mysql-space
labels:
name: mysql-master-rc
spec:
replicas: 1
selector:
name: mysql-master-pod
template:
metadata:
labels:
name: mysql-master-pod #该pod拥有的标签,对应上面的spec.selector.name,否则匹配不上
spec:
containers:
- name: mysql
image: mysql:5.7
imagePullPolicy: IfNotPresent
ports:
- containerPort: 3306
env:
- name: MYSQL_ROOT_PASSWORD
value: "mysql123"
- name: MYSQL_REPLICATION_USER
value: "repl"
- name: MYSQL_REPLICATION_PASSWORD
value: "repl123"
args:
- --log-bin=mysql-bin
- --binlog-ignore-db=mysql
- --server-id=1001
- --symbolic-links=0
- --sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
3. 创建mysql-master-svc.yaml文件
[root@master k8s]# cat mysql-master-svc.yaml
apiVersion: v1
kind: Service
metadata:
name: mysql-master-svc
namespace: mysql-space
labels:
name: mysql-master-svc
spec:
type: NodePort
ports:
- port: 3306
protocol: TCP
targetPort: 3306
name: http
nodePort: 32306
selector:
name: mysql-master-pod
4. 创建mysql-slave-rc.yaml文件
[root@master k8s]# cat mysql-slave-rc.yaml
apiVersion: v1
kind: ReplicationController
metadata:
name: mysql-slave-rc
namespace: mysql-space
labels:
name: mysql-slave-rc
spec:
replicas: 1
selector:
name: mysql-slave-pod
template:
metadata:
labels:
name: mysql-slave-pod
spec:
containers:
- name: mysql
image: mysql:5.7
imagePullPolicy: IfNotPresent
ports:
- containerPort: 3306
env:
- name: MYSQL_ROOT_PASSWORD
value: "mysql123"
- name: MYSQL_REPLICATION_USER
value: "repl"
- name: MYSQL_REPLICATION_PASSWORD
value: "repl123"
args:
- --server-id=2001
- --symbolic-links=0
- --sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
- --default-authentication-plugin=mysql_native_password
5. 创建mysql-slave-svc.yaml文件
[root@master k8s]# cat mysql-slave-svc.yaml
apiVersion: v1
kind: Service
metadata:
name: mysql-slave-svc
namespace: mysql-space
labels:
name: mysql-slave-svc
spec:
type: NodePort
ports:
- port: 3306
protocol: TCP
targetPort: 3306
name: http
nodePort: 32307
selector:
name: mysql-slave-pod
6. 创建namespace
kubectl create namespace mysql-space
7. 分别创建mysql master slave服务
kubectl create -f mysql-master-rc.yaml
kubectl create -f mysql-master-svc.yaml
kubectl create -f mysql-slave-rc.yaml
kubectl create -f mysql-slave-svc.yaml
8. 验证服务是否正常
[root@master k8s]# kubectl get pod -n mysql-space
NAME READY STATUS RESTARTS AGE
mysql-master-rc-p427f 1/1 Running 0 128m
mysql-slave-rc-lhg47 1/1 Running 0 88m
9. 进入master给slave库授权
[root@master k8s]# kubectl exec -ti -n mysql-space mysql-master-rc-p427f -- mysql -u root -pmysql123
mysql> GRANT REPLICATION SLAVE ON *.* to 'repl'@'%' IDENTIFIED by 'repl123';
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000003 | 1032 | | mysql | |
+------------------+----------+--------------+------------------+-------------------+
10. 进入slave库启动同步功能
下面的master_host参考上图中红框中
[root@master k8s]# kubectl exec -ti -n mysql-space mysql-slave-rc-lhg47 -- mysql -u root -pmysql123
mysql> change master to master_host='mysql-master-svc.mysql-space',master_user='repl',master_password='repl123',master_log_file='mysql-bin.000003',master_log_pos=1032 ;
mysql> start slave;
查看状态是否正常,如果没有错误信息,那就ok
mysql> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: mysql-master-svc.mysql-space
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 1032
Relay_Log_File: mysql-slave-rc-lhg47-relay-bin.000002
Relay_Log_Pos: 482
Relay_Master_Log_File: mysql-bin.000003
11. 验证同步功能实现
可以在master库中创建表,验证slave库中是否有