可以使用kubesphere,快速搭建MySQL环境。
k8s部署有状态服务的话就是参照上面的图。
部署详细说明:
操作步骤:
1)、基本信息:
2)、容器镜像:
设置MySQL容器镜像,MySQL:5.7 版本,内存设置为2G,环境变量使用之前设置好的MySQL密钥。
3)、挂载存储:
这里可以挂载配置文件或密钥,所以,接下来另开一个窗口,在配置中心->配置
,创建配置文件:
①、创建配置基本信息
②、设置mysql配置文件my.cnf
[client] default-character-set=utf8 [mysql] default-character-set=utf8 [mysqld] init_connect='SET collation_connection = utf8_unicode_ci' init_connect='SET NAMES utf8' character-set-server=utf8 collation-server=utf8_unicode_ci skip-character-set-client-handshake skip-name-resolve #master-slaver repication server_id=1 log-bin=mysql-bin read-only=0 binlog-do-db=gulimall_ums binlog-do-db=gulimall_pms binlog-do-db=gulimall_oms binlog-do-db=gulimall_sms binlog-do-db=gulimall_wms binlog-do-db=gulimall_admin replicate-ignore-db=mysql replicate-ignore-db=sys replicate-ignore-db=information_schema replicate-ignore-db=performance_schema
my.cnf配置内容直接复制粘贴到如下文本框内即可,kubesphere提供了可视化界面,也提供了yaml编辑模式,我们可以点击编辑模式查看yaml文件是如何写的。
mysql-master-cnf.yaml编辑模式:
apiVersion: v1 kind: ConfigMap metadata: namespace: gulimall labels: app: mysql-master-cnf name: mysql-master-cnf annotations: kubesphere.io/alias-name: master配置 spec: template: metadata: labels: app: mysql-master-cnf data: my.cnf: |- [client] default-character-set=utf8 [mysql] default-character-set=utf8 [mysqld] init_connect='SET collation_connection = utf8_unicode_ci' init_connect='SET NAMES utf8' character-set-server=utf8 collation-server=utf8_unicode_ci skip-character-set-client-handshake skip-name-resolve #master-slaver repication server_id=1 log-bin=mysql-bin read-only=0 binlog-do-db=gulimall_ums binlog-do-db=gulimall_pms binlog-do-db=gulimall_oms binlog-do-db=gulimall_sms binlog-do-db=gulimall_wms binlog-do-db=gulimall_admin replicate-ignore-db=mysql replicate-ignore-db=sys replicate-ignore-db=information_schema replicate-ignore-db=performance_schema
配置设置好之后,然后点击创建就创建好了:
4)、创建存储PVC
存储卷->创建
①、基本信息设置
②、存储卷设置
由于在安装kubesphere时没有创建openebs存储卷,所以,这里只能选local,应该需要创建openebs。
通过图形化界面创建好之后,我们可以看编辑模式的yaml:
apiVersion: v1 kind: PersistentVolumeClaim metadata: namespace: gulimall name: gulimall-mysql-master-pvc labels: app: gulimall-mysql-master-pvc annotations: kubesphere.io/alias-name: 主节点pvc spec: accessModes: - ReadWriteOnce resources: requests: storage: 10Gi template: metadata: labels: app: gulimall-mysql-master-pvc storageClassName: local
然后点击创建,创建好的PVC:
5)、有状态服务-挂载存储
创建好配置和PVC之后,我们再接着创建有状态服务的挂载存储步骤接着往下做:
配置文件挂载:
存储卷挂载:
然后点击添加,设置好的页面:
然后点击下一步,高级设置
保持默认,然后点击创建,另外,我们也可以看看编辑模式的yaml文件:
apiVersion: apps/v1 kind: StatefulSet metadata: namespace: gulimall labels: app: gulimall-mysql-master name: gulimall-mysql-master-qpr2er annotations: kubesphere.io/alias-name: mysql主节点 spec: replicas: 1 selector: matchLabels: app: gulimall-mysql-master template: metadata: labels: app: gulimall-mysql-master annotations: kubesphere.io/containerSecrets: null logging.kubesphere.io/logsidecar-config: '{}' spec: containers: - name: container-jbrfx9 type: worker imagePullPolicy: IfNotPresent resources: requests: cpu: '0.01' memory: 10Mi limits: cpu: '0.98' memory: 1700Mi image: 'mysql:5.7' ports: - name: tcp-3306 protocol: TCP containerPort: 3306 servicePort: 3306 - name: tcp-33060 protocol: TCP containerPort: 33060 servicePort: 33060 env: - name: MYSQL_ROOT_PASSWORD valueFrom: secretKeyRef: name: mysql-secret key: MYSQL_ROOT_PASSWORD volumeMounts: - name: volume-vvijno readOnly: false mountPath: /etc/mysql - name: volume-dddvwk readOnly: false mountPath: /var/lib/mysql serviceAccount: default affinity: podAntiAffinity: preferredDuringSchedulingIgnoredDuringExecution: - weight: 100 podAffinityTerm: labelSelector: matchLabels: app: gulimall-mysql-master topologyKey: kubernetes.io/hostname initContainers: [] imagePullSecrets: null volumes: - name: volume-vvijno configMap: name: mysql-master-cnf items: - key: my.cnf path: my.cnf - name: volume-dddvwk persistentVolumeClaim: claimName: gulimall-mysql-master-pvc updateStrategy: type: RollingUpdate rollingUpdate: partition: 0 serviceName: gulimall-mysql-master --- apiVersion: v1 kind: Service metadata: namespace: gulimall labels: app: gulimall-mysql-master annotations: kubesphere.io/serviceType: statefulservice kubesphere.io/alias-name: mysql主节点 name: gulimall-mysql-master spec: sessionAffinity: ClientIP selector: app: gulimall-mysql-master ports: - name: tcp-3306 protocol: TCP port: 3306 targetPort: 3306 - name: tcp-33060 protocol: TCP port: 33060 targetPort: 33060 clusterIP: None sessionAffinityConfig: clientIP: timeoutSeconds: 10800
创建好的页面:
可以参考上边创建master服务的步骤来创建从库的服务。
①、创建从库配置挂载文件
mysql-slaver-cnfmy.cnf
[client] default-character-set=utf8 [mysql] default-character-set=utf8 [mysqld] init_connect='SET collation_connection = utf8_unicode_ci' init_connect='SET NAMES utf8' character-set-server=utf8 collation-server=utf8_unicode_ci skip-character-set-client-handshake skip-name-resolve #master-slaver repication server_id=2 log-bin=mysql-bin read-only=1 binlog-do-db=gulimall_ums binlog-do-db=gulimall_pms binlog-do-db=gulimall_oms binlog-do-db=gulimall_wms binlog-do-db=gulimall_admin replicate-ignore-db=mysql replicate-ignore-db=sys replicate-ignore-db=information_schema replicate-ignore-db=performance_schema
②、创建存储卷PVC
③、创建slaver有状态服务
从节点创建好了:
上边我们已经通过k8s创建了主从两个服务,下边通过设置将主从同步起来。
进入master容器终端:
# 1.进入mysql内部 > mysql -uroot -p123456 # 2.授权 root可以远程访问(主从无关,为了方便我们远程连接MySQL) mysql> grant all privileges on *.* to 'root'@'%' identified by 'root' with grant option; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) # 3.添加用来同步的用户 mysql> GRANT REPLICATION SLAVE ON *.* to 'backup'@'%' identified by '123456'; Query OK, 0 rows affected, 1 warning (0.01 sec) # 4.查看master状态 mysql> show master status\G; *************************** 1. row *************************** File: mysql-bin.000003 Position: 889 Binlog_Do_DB: gulimall_ums,gulimall_pms,gulimall_oms,gulimall_sms,gulimall_wms,gulimall_admin Binlog_Ignore_DB: Executed_Gtid_Set: 1 row in set (0.00 sec)
设置好之后,记录File文件 mysql-bin.000003
,从库同步需要用到,然后exit退出。
进入slaver容器终端:
# 进入mysql内部 mysql -uroot -p123456 # 设置主库连接 主库 dns: gulimall-mysql-master.gulimall change master to master_host='gulimall-mysql-master.gulimall',master_user='backup',master_password='123456',master_log_file='mysql_bin.000003',master_log_pos=0,master_port=3306; # 启动从库同步 start slave; # 查看从从库状态 show slave status\G;
从库报了这样的一个错误:
mysql> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Master_Host: gulimall-mysql-master.gulimall Master_User: backup Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql_bin.000003 Read_Master_Log_Pos: 4 Relay_Log_File: gulimall-mysql-slaver-5mehjw-0-relay-bin.000001 Relay_Log_Pos: 4 Relay_Master_Log_File: mysql_bin.000003 Slave_IO_Running: No Slave_SQL_Running: Yes Replicate_Do_DB: Replicate_Ignore_DB: mysql,sys,information_schema,performance_schema Last_IO_Errno: 1236 Last_IO_Error: Got fatal error 1236 from master when reading data from binary log: 'Could not find first log file name in binary log index file' Last_SQL_Errno: 0 Last_SQL_Error:
解决方法:
stop slave; reset slave; start slave;
然后打印从库同步状态:
可以看到已经设置成功了。
主库创建一个数据库:
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.00 sec) mysql> CREATE DATABASE `gulimall_oms` DEFAULT CHARACTER SET utf8mb4; Query OK, 1 row affected (0.01 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | gulimall_oms | | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.00 sec)
进入从库:
可以看到从库也已经同步了刚在主库创建的gulimall_oms
库,至此我们在K8S上的主从服务已经创建完成。
相关文章:
Digtime.cn-部署-92-K8S 部署 MySQL主从
K8S部署有状态服务
Kubernetes(k8s)有状态集群服务部署与管理