1.创建mysql主从环境的命名空间
[root@k8s-master1 mysql]# kubectl create ns mysql
namespace/mysql created
2.创建master的pvc
[root@k8s-master1 mysql]# cat mysql-master-pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:name: mysql-pvcnamespace: mysqlannotations:volume.beta.kubernetes.io/storage-class: "nfs-client"
spec:resources:requests:storage: 5GivolumeMode: FilesystemaccessModes:- ReadWriteOnce
[root@k8s-master1 mysql]# kubectl apply -f mysql-master-pvc.yaml
persistentvolumeclaim/mysql-pvc created
3.创建mysql-master的headliness
[root@k8s-master1 mysql]# cat mysql-master-headliness.yaml
kind: Service
apiVersion: v1
metadata:name: mysql-master-hsnamespace: mysqllabels:app: mysql-master-ss
spec:selector:app: mysql-master-ssclusterIP: Noneports:- port: 3306targetPort: 3306
[root@k8s-master1 mysql]# kubectl apply -f mysql-master-headliness.yaml
service/mysql-master-hs created
4.创建mysql-master-service对外访问
[root@k8s-master1 mysql]# cat mysql-master-service.yaml
apiVersion: v1
kind: Service
metadata:name: mysql-master-servicenamespace: mysql
spec:type: NodePortselector:app: mysql-master-ssports:- port: 3306targetPort: 3306nodePort: 42222
[root@k8s-master1 mysql]# kubectl apply -f mysql-master-service.yaml
service/mysql-master-service created
5.创建mysql-master部署文件
[root@k8s-master1 mysql]# cat mysql-master-StatefulSet.yaml
apiVersion: apps/v1
kind: StatefulSet
metadata:name: mysql-master-ssnamespace: mysqllabels:app: mysql-master-ss
spec:replicas: 1selector:matchLabels:app: mysql-master-ssserviceName: mysql-master-hstemplate:metadata:labels:app: mysql-master-ssspec:containers:- name: mysqlimage: mysql:5.7.35ports:- containerPort: 3306env:- name: MYSQL_ROOT_PASSWORDvalue: "mysql123"- name: MYSQL_REPLICATION_USERvalue: "repl"- name: MYSQL_REPLICATION_PASSWORDvalue: "repl123"volumeMounts:- name: mysql-datamountPath: /var/lib/mysqlargs:- --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_SUBSTITUTIONvolumeClaimTemplates:- metadata:name: mysql-dataspec:accessModes: ["ReadWriteOnce"]storageClassName: nfs-clientresources:requests:storage: 5Gi
[root@k8s-master1 mysql]# kubectl apply -f mysql-master-StatefulSet.yaml
statefulset.apps/mysql-master-ss created
6.检查服务启动情况
[root@k8s-master1 mysql]# kubectl get all -n mysql
NAME READY STATUS RESTARTS AGE
pod/mysql-master-ss-0 1/1 Running 0 48sNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/mysql-master-hs ClusterIP None <none> 3306/TCP 6m22s
service/mysql-master-service NodePort 10.255.42.172 <none> 3306:42222/TCP 2m43sNAME READY AGE
statefulset.apps/mysql-master-ss 1/1 48s
7.创建mysql-slave的pvc
[root@k8s-master1 mysql]# cat mysql-slave-pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:name: mysql-slave-pvcnamespace: mysqlannotations:volume.beta.kubernetes.io/storage-class: "nfs-client"
spec:resources:requests:storage: 5GivolumeMode: FilesystemaccessModes:- ReadWriteOnce
[root@k8s-master1 mysql]# kubectl apply -f mysql-slave-pvc.yaml
persistentvolumeclaim/mysql-slave-pvc created
8.建mysql-slave的headliness和service用来对外访问(slave节点可以不创建service)
[root@k8s-master1 mysql]# cat mysql-slave-headliness.yaml
kind: Service
apiVersion: v1
metadata:name: mysql-slave-headlinessnamespace: mysqllabels:app: mysql-slave-ss
spec:selector:app: mysql-slave-ssclusterIP: Noneports:- port: 3306targetPort: 3306
[root@k8s-master1 mysql]# kubectl apply -f mysql-slave-headliness.yaml
service/mysql-slave-headliness created[root@k8s-master1 mysql]# cat mysql-slave-service.yaml
apiVersion: v1
kind: Service
metadata:name: mysql-slave-servicenamespace: mysql
spec:type: NodePortselector:app: mysql-slave-ssports:- port: 3306targetPort: 3306nodePort: 42223
[root@k8s-master1 mysql]# kubectl apply -f mysql-slave-service.yaml
service/mysql-slave-service created
9.创建mysql-slave部署文件(mysql-serverid不能一样一定要区分开)
[root@k8s-master1 mysql]# cat mysql-slave-StatefulSet.yaml
apiVersion: apps/v1
kind: StatefulSet
metadata:name: mysql-slave-ssnamespace: mysqllabels:app: mysql-slave-ss
spec:replicas: 1selector:matchLabels:app: mysql-slave-ssserviceName: mysql-slave-headlinesstemplate:metadata:labels:app: mysql-slave-ssspec:containers:- name: mysqlimage: mysql:5.7.35ports:- containerPort: 3306env:- name: MYSQL_ROOT_PASSWORDvalue: "mysql123"- name: MYSQL_REPLICATION_USERvalue: "repl"- name: MYSQL_REPLICATION_PASSWORDvalue: "repl123"volumeMounts:- name: mysql-datamountPath: /var/lib/mysqlargs:- --log-bin=mysql-bin- --binlog-ignore-db=mysql- --server-id=2002- --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_SUBSTITUTIONvolumeClaimTemplates:- metadata:name: mysql-dataspec:accessModes: ["ReadWriteOnce"]storageClassName: nfs-clientresources:requests:storage: 5Gi
10.检查服务的启动情况
[root@k8s-master1 mysql]# kubectl get pods -n mysql -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
mysql-master-ss-0 1/1 Running 0 7m40s 10.10.135.211 k8s-master3 <none> <none>
mysql-slave-ss-0 1/1 Running 0 23s 10.10.135.208 k8s-master3 <none> <none>
11.配置主从同步
[root@k8s-master1 mysql]# kubectl exec -it mysql-master-ss-0 -n mysql bash
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
root@mysql-master-ss-0:/#
root@mysql-master-ss-0:/# mysql -u root -pmysql123
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.35-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>
创建slave库授权账号
mysql> GRANT REPLICATION SLAVE ON *.* to 'repl'@'%' IDENTIFIED by 'repl123';
Query OK, 0 rows affected, 1 warning (0.01 sec)获取二进制日志mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000003 | 437 | | mysql | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)进入slave库启动同步功能 mysql> change master to master_host='mysql-master-hs.mysql',master_user='repl',master_password='repl123',master_log_file='mysql-bin.000003',master_log_pos=437 ;
Query OK, 0 rows affected, 2 warnings (0.03 sec)
开启同步
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
检查同步状态
mysql> show slave status \G;
12。验证同步