部署方式
通过部署mysql主从容器,配置主从pod之间数据同步。

配置数据库访问的密码

创建 Mysql 密码的 Secret[root@k8s-master1 master]# kubectl create secret generic mysql-password --namespace=app --from-literal=mysql_root_password=rootsecret/mysql-passowrd createdYou have new mail in /var/spool/mail/root[root@k8s-master1 master]# kubectl get secret -n appNAMETYPEDATA AGEdefault-token-w6q98 kubernetes.io/service-account-token 348dingress-nginx-admission Opaque335dingress-nginx-admission-token-sqbvf kubernetes.io/service-account-token 335dingress-nginx-token-bt7g2 kubernetes.io/service-account-token 335dmysql-passowrdOpaque16s

先配置MySQL-Master服务
配置数据卷

[root@k8s-master1 master]# cat mysql-master-pvc.yamlapiVersion: v1kind: PersistentVolumeClaimmetadata:name: mysql-master-pvcnamespace: appspec:accessModes:- ReadWriteOnceresources:requests:storage: 2GistorageClassName: nfs-client#这里要换成你环境的storageClassNamevolumeMode: Filesystem[root@k8s-master1 master]# kubectl apply -f mysql-master-pvc.yaml[root@k8s-master1 master]# kubectl get pvc -n appNAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGEmysql-master-pvc Boundpvc-f2b5598c-4fd8-48f2-bc93-0e8c27e225ec 2GiRWOnfs-client 10s

通过ConfiigMap配置MySQL的配置文件my.cnf

[root@k8s-master1 master]# cat my.cnf[mysqld]skip-host-cacheskip-name-resolvedatadir= /var/lib/mysqlsocket = /var/run/mysqld/mysqld.socksecure-file-priv = /var/lib/mysql-filespid-file = /var/run/mysqld/mysqld.piduser = mysqlsecure-file-priv = NULLserver-id= 1#master节点的server-id=1log-bin= master-binlog_bin_index= master-bin.indexbinlog_do_db = deploy_testbinlog_ignore_db = information_sechemabinlog_ignore_db = mysqlbinlog_ignore_db = performance_schemabinlog_ignore_db = sysbinlog-format= row[client]socket = /var/run/mysqld/mysqld.sock!includedir/etc/mysql/conf.d/[root@k8s-master1 master]# kubectl create cm mysql-master-cm --from-file=my.cnf -n appconfigmap/mysql-master-cm created[root@k8s-master1 master]# kubectl get cm -n app |grep mysqlmysql-master-cm119s

配置Service服务

[root@k8s-master1 master]# cat mysql-master-svc.yamlapiVersion: v1kind: Servicemetadata:name: mysql-master-svcnamespace: applabels:app: mysql-masterspec:ports:- port: 3306name: mysqltargetPort: 3306nodePort: 30306selector:app: mysql-mastertype: NodePortsessionAffinity: ClientIP[root@k8s-master1 master]# kubectl apply -f mysql-master-svc.yamlservice/mysql-master-svc created

配置主节点pod启动的StatefulSet配置文件

[root@k8s-master1 master]# cat mysql-master-StatefulSet.yamlapiVersion: apps/v1kind: StatefulSetmetadata:name: mysql-masternamespace: appspec:selector:matchLabels:app: mysql-masterserviceName: "mysql-master-svc"replicas: 1template:metadata:labels:app: mysql-masterspec:terminationGracePeriodSeconds: 10containers:- args:- --character-set-server=utf8mb4- --collation-server=utf8mb4_unicode_ci- --lower_case_table_names=1- --default-time_zone=+8:00name: mysql# image: docker.io/library/mysql:8.0.34image: registry.cn-shenzhen.aliyuncs.com/xiaohh-docker/mysql:8.0.34ports:- containerPort: 3306name: mysqlvolumeMounts:- name: mysql-datamountPath: /var/lib/mysql- name: mysql-confmountPath: /etc/my.cnfreadOnly: truesubPath: my.cnfenv:- name: MYSQL_ROOT_PASSWORDvalueFrom:secretKeyRef:key: mysql_root_passwordname: mysql-passwordvolumes:- name: mysql-datapersistentVolumeClaim:claimName: mysql-master-pvc- name: mysql-confconfigMap:name: mysql-master-cmitems:- key: my.cnfmode: 0644path: my.cnf[root@k8s-master1 master]# kubectl apply -f mysql-master-StatefulSet.yamlstatefulset.apps/mysql-master created

检查服务的启动情况

[root@k8s-master1 master]# kubectl get pods -n app -o wide |grep mysqlmysql-master-01/1 Running 0104s10.10.36.73k8s-node1 <none> <none>

配置mysql-salve

配置PVC

[root@k8s-master1 slave]# cat mysql-slave-pvc.yamlapiVersion: v1kind: PersistentVolumeClaimmetadata:name: mysql-slave-pvcnamespace: appspec:accessModes:- ReadWriteOnceresources:requests:storage: 2GistorageClassName: nfs-clientvolumeMode: Filesystem[root@k8s-master1 slave]# kubectl apply -f mysql-slave-pvc.yamlpersistentvolumeclaim/mysql-slave-pvc created

配置ConfigMap

[root@k8s-master1 slave]# cat my.cnf[mysqld]skip-host-cacheskip-name-resolvedatadir= /var/lib/mysqlsocket = /var/run/mysqld/mysqld.socksecure-file-priv = /var/lib/mysql-filespid-file = /var/run/mysqld/mysqld.piduser = mysqlsecure-file-priv = NULLserver-id= 2#这个要修改log-bin= master-binlog_bin_index= master-bin.indexbinlog_do_db = deploy_testbinlog_ignore_db = information_sechemabinlog_ignore_db = mysqlbinlog_ignore_db = performance_schemabinlog_ignore_db = sysbinlog-format= row[client]socket = /var/run/mysqld/mysqld.sock!includedir/etc/mysql/conf.d/[root@k8s-master1 slave]# kubectl create cm mysql-slave-cm --from-file=my.cnf -n appconfigmap/mysql-slave-cm createdYou have new mail in /var/spool/mail/root[root@k8s-master1 slave]# kubectl get cm -n app |grep mysql-slave-cmmysql-slave-cm 113s

配置Service

[root@k8s-master1 slave]# cat mysql-slave-svc.yamlapiVersion: v1kind: Servicemetadata:name: mysql-slave-svcnamespace: applabels:app: mysql-slavespec:ports:- port: 3306name: mysqltargetPort: 3306nodePort: 30307selector:app: mysql-slavetype: NodePortsessionAffinity: ClientIP[root@k8s-master1 slave]# kubectl apply -f mysql-slave-svc.yamlservice/mysql-slave-svc created

创建MySQL的Pod

[root@k8s-master1 slave]# cat mysql-slave-StatefulSet.yamlapiVersion: apps/v1kind: StatefulSetmetadata:name: mysql-slavenamespace: appspec:selector:matchLabels:app: mysql-slaveserviceName: "mysql-master-svc"replicas: 1template:metadata:labels:app: mysql-slavespec:terminationGracePeriodSeconds: 10containers:- args:- --character-set-server=utf8mb4- --collation-server=utf8mb4_unicode_ci- --lower_case_table_names=1- --default-time_zone=+8:00name: mysql# image: docker.io/library/mysql:8.0.34image: registry.cn-shenzhen.aliyuncs.com/xiaohh-docker/mysql:8.0.34ports:- containerPort: 3306name: mysqlvolumeMounts:- name: mysql-datamountPath: /var/lib/mysql- name: mysql-confmountPath: /etc/my.cnfreadOnly: truesubPath: my.cnfenv:- name: MYSQL_ROOT_PASSWORDvalueFrom:secretKeyRef:key: mysql_root_passwordname: mysql-passwordvolumes:- name: mysql-datapersistentVolumeClaim:claimName: mysql-slave-pvc- name: mysql-confconfigMap:name: mysql-slave-cmitems:- key: my.cnfmode: 0644path: my.cnf[root@k8s-master1 slave]# kubectl apply -f mysql-slave-StatefulSet.yamlstatefulset.apps/mysql-slave created[root@k8s-master1 slave]# kubectl get pods -n app -o wide |grep mysqlmysql-master-01/1 Running 0 14m 10.10.36.73k8s-node1 <none> <none>mysql-slave-0 1/1 Running 0 38s 10.10.36.74k8s-node1 <none> <none>

主从POD都创建完成了,咱们登录验证一下,并配置一下主从同步

[root@k8s-master1 slave]# kubectl exec -it mysql-master-0 -n app -- mysql -uroot -prootmysql: [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 8Server version: 8.0.34 MySQL Community Server - GPLCopyright (c) 2000, 2023, Oracle and/or its affiliates.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql>

在从节点执行,配置主从关系

mysql> change master to master_host='mysql-master-0.mysql-master-svc.app.svc.cluster.local',-> master_port=3306, master_user='root', master_password='root', master_log_file='master-bin.000003',-> master_log_pos=157,master_connect_retry=30,get_master_public_key=1;Query OK, 0 rows affected, 11 warnings (0.03 sec)master_host:这个参数是master的地址,kubernetes 提供的解析规则是 pod 名称.service名称.命名空间.svc.cluster.local,所以 master 的 mysql 地址是 deploy-mysql-master-0.deploy-mysql-master-svc.deploy-test.svc.cluster.localmaster_port:主节点的 MySQL 端口,没改默认 3306master_user:登录到主节点的 mysql 用户master_password:登录到主节点的用户密码master_log_file:之前查看 mysql 主节点状态时的 file 字段master_log_pos:之前查看 mysql 主节点状态时的 Position 字段master_connect-retry:主节点重连时间get_master_public_key:连接主 mysql 的公钥获取方式

启动 slave

mysql> start slave;Query OK, 0 rows affected, 1 warning (0.01 sec)

查看slave的状态

mysql> show slave status\G;*************************** 1. row *************************** Slave_IO_State: Waiting for source to send eventMaster_Host: mysql-master-0.mysql-master-svc.app.svc.cluster.localMaster_User: rootMaster_Port: 3306Connect_Retry: 30Master_Log_File: master-bin.000003Read_Master_Log_Pos: 157 Relay_Log_File: mysql-slave-0-relay-bin.000002Relay_Log_Pos: 327Relay_Master_Log_File: master-bin.000003 Slave_IO_Running: YesSlave_SQL_Running: Yes

在主节点创建一个数据库验证一下主从同步情况