KubeSphere构建mysql集群

2024/03 作者:ihunter 0 0

摘要

利用kubeSphere构建mysql的主从集群,同时测试主从集群结构部署成功。

一、配置Secret资源

1.1 配置mysql master slave的Secret资源

kind: Secret
apiVersion: v1
metadata:
  name: mysql-cluster-secret
  namespace: athena-mall
  annotations:
    kubesphere.io/alias-name: 数据库集群秘钥
    kubesphere.io/creator: project-regular
data:
  MYSQL_ROOT_PASSWORD: cm9vdA==

二、配置ConfigMap资源

2.1 配置mysql-master的集群Configmap资源

kind: ConfigMap
apiVersion: v1
metadata:
  name: mysql-master
  namespace: athena-mall
  annotations:
    kubesphere.io/alias-name: 主数据库配置
    kubesphere.io/creator: project-regular
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
    secure_file_priv=/var/lib/mysql
     
    server_id=1
    log-bin=mysql-bin
    read-only=0
    binlog-do-db=gulimall_ums      # 填写你需要同步的数据库
     
    replicate-ignore-db=mysql      # 不需要同步的数据库
    replicate-ignore-db=sys
    replicate-ignore-db=information_schema
    replicate-ignore-db=performance_schema

2.2 配置mysql-slave的集群Configmap资源

kind: ConfigMap
apiVersion: v1
metadata:
  name: mysql-slave
  namespace: athena-mall
  annotations:
    kubesphere.io/alias-name: 从数据库配置
    kubesphere.io/creator: project-regular
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
    secure_file_priv=/var/lib/mysql
     
    server_id=2
    log-bin=mysql-bin
    read-only=1
    binlog-do-db=gulimall_ums  # 需要同步的数据库
     
    replicate-ignore-db=mysql  # 不需要同步的数据库
    replicate-ignore-db=sys
    replicate-ignore-db=information_schema
    replicate-ignore-db=performance_schema

三、配置mysql-pvc资源

3.1 配置mysql master slave的mysql-pvc资源

kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: mysql-master-pvc
  namespace: athena-mall
  annotations:
    kubesphere.io/alias-name: 主数据库pvc
    kubesphere.io/creator: project-regular
    kubesphere.io/description: 主数据库pvc
    pv.kubernetes.io/bind-completed: 'yes'
    pv.kubernetes.io/bound-by-controller: 'yes'
    volume.beta.kubernetes.io/storage-provisioner: openebs.io/local
    volume.kubernetes.io/selected-node: k8s-master
  finalizers:
    - kubernetes.io/pvc-protection
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 10Gi
  volumeName: pvc-5bcb27d7-f06e-4dc5-8325-73878be6fedc
  storageClassName: local
  volumeMode: Filesystem
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: mysql-slave-pvc
  namespace: athena-mall
  annotations:
    kubesphere.io/alias-name: 从数据库pvc
    kubesphere.io/creator: project-regular
    kubesphere.io/description: 从数据库pvc
    pv.kubernetes.io/bind-completed: 'yes'
    pv.kubernetes.io/bound-by-controller: 'yes'
    volume.beta.kubernetes.io/storage-provisioner: openebs.io/local
    volume.kubernetes.io/selected-node: k8s-master
  finalizers:
    - kubernetes.io/pvc-protection
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 10Gi
  volumeName: pvc-1bfd6216-62dd-46f5-97bb-334c57948c31
  storageClassName: local
  volumeMode: Filesystem

四、配置mysql-cluster-application

4.1 部署mysql有状态(master)

 

kind: Service
apiVersion: v1
metadata:
  name: mysql-master
  namespace: athena-mall
  labels:
    app: mysql-master
    version: v1
  annotations:
    kubesphere.io/creator: project-regular
    kubesphere.io/serviceType: statefulservice
spec:
  ports:
    - name: tcp-3306
      protocol: TCP
      port: 3306
      targetPort: 3306
    - name: tcp-33060
      protocol: TCP
      port: 33060
      targetPort: 33060
  selector:
    app: mysql-master
  clusterIP: None
  clusterIPs:
    - None
  type: ClusterIP
  sessionAffinity: None
  ipFamilies:
    - IPv4
  ipFamilyPolicy: SingleStack

4.2 部署mysql有状态(slave)

 

kind: Service
apiVersion: v1
metadata:
  name: mysql-slave
  namespace: athena-mall
  labels:
    app: mysql-slave
    version: v1
  annotations:
    kubesphere.io/creator: project-regular
    kubesphere.io/serviceType: statefulservice
spec:
  ports:
    - name: tcp-3306
      protocol: TCP
      port: 3306
      targetPort: 3306
    - name: tcp-33060
      protocol: TCP
      port: 33060
      targetPort: 33060
  selector:
    app: mysql-slave
  clusterIP: None
  clusterIPs:
    - None
  type: ClusterIP
  sessionAffinity: None
  ipFamilies:
    - IPv4
  ipFamilyPolicy: SingleStack

4.3 slave 同步master的设置

4.3.1 Master的设置

进入master 容器
kubectl exec -it mysql-master /bin/bash
 
进入mysql 内部(mysql –uroot -proot)
 
授权root 可以远程访问( 主从无关,为了方便我们远程连接mysql):
 
grant all privileges on . to 'root'@'%' identified by 'root' with grant option;
flush privileges;
添加用来同步的用户
 
GRANT REPLICATION SLAVE ON . to 'backup'@'%' identified by 'mysql的密码';
查看master 状态
 
show master status\G;

4.3.2 slave的设置

进入slaver 容器
kubectl exec -it mysql-slaver /bin/bash
 
进入mysql 内部(mysql –uroot -p)
 
授权root 可以远程访问( 主从无关,为了方便我们远程连接mysql)
 
grant all privileges on . to 'root'@'%' identified by 'root' with grant option;
flush privileges;
设置主库连接
 
change master to master_host='k8s中master的域名地址',master_user='backup',master_password='master的密码',master_log_file='mysql-bin.000003',master_log_pos=0,master_port=3306;
 
启动从库同步
start slave;
 
查看从库状态
show slave status\G;

五、外网暴露测试

5.1 部署的NodePort无状态服务

kind: Service
apiVersion: v1
metadata:
  name: master-ip
  namespace: athena-mall
  labels:
    app: master-ip
  annotations:
    kubesphere.io/creator: project-regular
spec:
  ports:
    - name: http-mysql1
      protocol: TCP
      port: 3306
      targetPort: 3306
      nodePort: 30212
    - name: http-mysql2
      protocol: TCP
      port: 33060
      targetPort: 33060
      nodePort: 30443
  selector:
    app: mysql-master
    version: v1
  clusterIP: 10.233.31.134
  clusterIPs:
    - 10.233.31.134
  type: NodePort
  sessionAffinity: None
  externalTrafficPolicy: Cluster
  ipFamilies:
    - IPv4
  ipFamilyPolicy: SingleStack
kind: Service
apiVersion: v1
metadata:
  name: slave-ip
  namespace: athena-mall
  labels:
    app: slave-ip
  annotations:
    kubesphere.io/creator: project-regular
spec:
  ports:
    - name: http-mysql
      protocol: TCP
      port: 3306
      targetPort: 3306
      nodePort: 32597
  selector:
    app: mysql-slave
    version: v1
  clusterIP: 10.233.1.47
  clusterIPs:
    - 10.233.1.47
  type: NodePort
  sessionAffinity: None
  externalTrafficPolicy: Cluster
  ipFamilies:
    - IPv4
  ipFamilyPolicy: SingleStack

5.2 测试主从集群是否可用

赞(0) 更多分享

上篇: KubeSphere——常用应用UI可视化部署实战(1)
下篇: KubeSphere部署worldpress应用