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-pvc
namespace: mysql
annotations:
volume.beta.kubernetes.io/storage-class: "nfs-client"
spec:
resources:
requests:
storage: 5Gi
volumeMode: Filesystem
accessModes:
- 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-hs
namespace: mysql
labels:
app: mysql-master-ss
spec:
selector:
app: mysql-master-ss
clusterIP: None
ports:
- port: 3306
targetPort: 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-service
namespace: mysql
spec:
type: NodePort
selector:
app: mysql-master-ss
ports:
- port: 3306
targetPort: 3306
nodePort: 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-ss
namespace: mysql
labels:
app: mysql-master-ss
spec:
replicas: 1
selector:
matchLabels:
app: mysql-master-ss
serviceName: mysql-master-hs
template:
metadata:
labels:
app: mysql-master-ss
spec:
containers:
- name: mysql
image: mysql:5.7.35
ports:
- containerPort: 3306
env:
- name: MYSQL_ROOT_PASSWORD
value: "mysql123"
- name: MYSQL_REPLICATION_USER
value: "repl"
- name: MYSQL_REPLICATION_PASSWORD
value: "repl123"
volumeMounts:
- name: mysql-data
mountPath: /var/lib/mysql
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
volumeClaimTemplates:
- metadata:
name: mysql-data
spec:
accessModes: ["ReadWriteOnce"]
storageClassName: nfs-client
resources:
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 48s
NAME 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 2m43s
NAME 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-pvc
namespace: mysql
annotations:
volume.beta.kubernetes.io/storage-class: "nfs-client"
spec:
resources:
requests:
storage: 5Gi
volumeMode: Filesystem
accessModes:
- 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-headliness
namespace: mysql
labels:
app: mysql-slave-ss
spec:
selector:
app: mysql-slave-ss
clusterIP: None
ports:
- port: 3306
targetPort: 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-service
namespace: mysql
spec:
type: NodePort
selector:
app: mysql-slave-ss
ports:
- port: 3306
targetPort: 3306
nodePort: 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-ss
namespace: mysql
labels:
app: mysql-slave-ss
spec:
replicas: 1
selector:
matchLabels:
app: mysql-slave-ss
serviceName: mysql-slave-headliness
template:
metadata:
labels:
app: mysql-slave-ss
spec:
containers:
- name: mysql
image: mysql:5.7.35
ports:
- containerPort: 3306
env:
- name: MYSQL_ROOT_PASSWORD
value: "mysql123"
- name: MYSQL_REPLICATION_USER
value: "repl"
- name: MYSQL_REPLICATION_PASSWORD
value: "repl123"
volumeMounts:
- name: mysql-data
mountPath: /var/lib/mysql
args:
- --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_SUBSTITUTION
volumeClaimTemplates:
- metadata:
name: mysql-data
spec:
accessModes: ["ReadWriteOnce"]
storageClassName: nfs-client
resources:
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。验证同步