Bboysoul's Blog

首页 公告 RSS

在Kubernetes上部署和管理MySQL的强大工具MySQLOperator

December 6, 2023 本文有 1127 个字 需要花费 3 分钟阅读

简介

在当今的云原生时代,Kubernetes 已经成为了管理和部署应用的首选平台。同时,MySQL 作为最流行的开源关系型数据库,也在许多应用中发挥着重要的作用。本文将介绍如何使用 MySQL Operator 在 Kubernetes 上部署和管理 MySQL 集群。MySQL Operator 是一种基于 Kubernetes Operator 模式构建的工具,它封装了有状态应用程序的复杂性,使得我们可以更简单地管理和运维 MySQL 集群。

MySQL Operator 的主要功能

MySQL Operator 提供了以下主要功能:

  1. 自动化部署:通过简单的 YAML 文件描述你的 MySQL 集群,MySQL Operator 会自动创建并配置集群。

  2. 自动备份:MySQL Operator 可以定期自动备份你的 MySQL 集群

  3. 自动扩展:根据工作负载的需求,MySQL Operator 可以自动增加或减少节点的数量。

  4. 版本升级:MySQL Operator 可以帮助你轻松地升级 MySQL 集群到新版本。

下面,我们将详细介绍如何使用 MySQL Operator。

案例的 yaml 地址

https://github.com/bboysoulcn/kubernetes-sample/tree/master/mysql-operator

如何部署 MySQL Operator

首先,我们需要部署 MySQL Operator。我个人更倾向于直接使用 YAML 文件进行部署,而不是使用 Helm。MySQL Operator 的 YAML 文件可以在以下链接中找到:

https://github.com/mysql/mysql-operator/tree/trunk/deploy

deploy-crds.yaml

deploy-operator.yaml

你也可以直接使用我在 GitHub 仓库中的 YAML 文件。只需使用 kubectl apply 命令应用这两个 YAML 文件,就可以启动 MySQL Operator 的 Pod,完成部署。

如何创建第一个 MySQL 集群

要创建 MySQL 集群,首先需要创建一个 Secret 来存储集群的初始账户和密码。以下是一个示例:

apiVersion: v1
kind: Secret
metadata:
  name: mypwds
stringData:
  rootUser: root
  rootHost: '%'
  rootPassword: pass

创建 Secret 后,我们可以创建 MySQL 集群。以下是一个示例:

apiVersion: mysql.oracle.com/v2
kind: InnoDBCluster
metadata:
  name: idc-with-custom-config
spec:
  secretName: mypwds
  instances: 3
  router:
    instances: 1
  tlsUseSelfSigned: true
  datadirVolumeClaimTemplate:
    accessModes: [ "ReadWriteOnce" ]
    storageClassName: "managed-nfs-storage"
    resources:
      requests:
        storage: 300Gi
  mycnf: |
    [mysqld]
    innodb_buffer_pool_size=200M
    innodb_log_file_size=2G    
  service:
    type: LoadBalancer
  version: 8.2.0 # 版本一定要 x.x.x


  backupProfiles:       
    - name: myfancyprofile  # Embedded backup profile
      dumpInstance:         # MySQL Shell Dump
        # dumpOptions:
        #   excludeTables: "[world.country]" # Example to exclude one table 
        storage:
          persistentVolumeClaim:
            claimName: mysqlbackup-pvc # store to this pre-existing PVC
  backupSchedules:
    - name: mygreatschedule
      schedule: "0 0 * * *" # Daily, at midnight
      backupProfileName:  myfancyprofile # reference the desired backupProfiles's name 
      enabled: true # backup schedules can be temporarily disabled

以下是一些关键参数的解释:

  • secretName 是之前创建的 Secret 的名称。
  • instances 是 MySQL 实例的数量。
  • router.instances 是 Router 实例的数量。
  • datadirVolumeClaimTemplate 中定义了 PVC 相关的参数,例如我在这里定义了 storageClassName 为我集群中的 managed-nfs-storage
  • mycnf 定义了 MySQL 相关的配置参数。
  • service 定义了 Router 对外的 service 类型。
  • version 指定了 MySQL 的版本,注意版本需要使用 x.x.x 这种格式。

如何配置 MySQL 集群的备份

如果你需要备份 MySQL 集群,你需要在 InnoDBCluster 中定义 MySQL 备份相关的配置。以下是一个示例:

  backupProfiles:       
    - name: myfancyprofile  # Embedded backup profile
      dumpInstance:         # MySQL Shell Dump
        # dumpOptions:
        #   excludeTables: "[world.country]" # Example to exclude one table 
        storage:
          persistentVolumeClaim:
            claimName: mysqlbackup-pvc # store to this pre-existing PVC
  backupSchedules:
    - name: mygreatschedule
      schedule: "0 0 * * *" # Daily, at midnight
      backupProfileName:  myfancyprofile # reference the desired backupProfiles's name 
      enabled: true # backup schedules can be temporarily disabled

在配置备份之前,你需要创建一个 PVC 来存储备份。以下是一个示例:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysqlbackup-pvc
spec:
  storageClassName: managed-nfs-storage
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 2Gi

配置完成后,在 Kubernetes 中就创建了一个 CronJob。你可以通过这个 CronJob 手动创建一个 Job 来备份,也可以直接创建 MySQLBackup 资源来创建一个备份。以下是一个示例:

apiVersion: mysql.oracle.com/v2
kind: MySQLBackup
metadata:
    name: a-cool-one-off-backup
    namespace: mysql-operator
spec:
  clusterName: idc-with-custom-config
  backupProfileName: myfancyprofile

结论和下一步

其他详细的参数你可以在

https://dev.mysql.com/doc/mysql-operator/en/mysql-operator-properties.html

这里找到,同时备份的话不一定要创建一个 pv 来存储备份,是支持直接上传备份到 oci对象存储 和 s3 的

欢迎关注我的博客www.bboy.app

Have Fun


Tags:

本站总访问量 本站总访客数