猿创征文 | 国产数据库之在k8s环境下部署RadonDB MySQL集群

Posted 江湖有缘

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了猿创征文 | 国产数据库之在k8s环境下部署RadonDB MySQL集群相关的知识,希望对你有一定的参考价值。

猿创征文 | 国产数据库之在k8s环境下部署RadonDB mysql集群

一、RadonDB MySQL介绍

1.RadonDB MySQL简介

1.QingCloud RadonDB 是基于 MySQL 研发的新一代分布式关系型数据库,可无限水平扩展,支持分布式事务,具备金融级数据强一致性,满足企业级核心数据库对大容量、高并发、高可靠及高可用的极致要求。
2.RadonDB MySQL 是一款基于 MySQL 的开源、高可用的云原生集群解决方案。

2.RadonDB MySQL的应用场景

RadonDB MySQL采用一主多从高可用架构,并具备安全、自动备份、监控告警、自动扩容等全套管理功能。目前已经在生产环境中大规模的使用,用户包含银行、保险、传统大企业等。

  • 数据一致性较高的金融场景

数据强一致性保证,满足金融级可靠性要求。

  • 网站运维

包含全套备份、恢复和监控等运维方案,满足网站类的业务需求。

3.RadonDB MySQL核心功能

  • MySQL 高可用

1.无中心化自动选主
2.主从秒级切换
3.集群切换的数据强一致性

  • 集群管理
  • 监控告警
  • 备份
  • 集群日志管理
  • 账户管理

4.RadonDB MySQL架构图

二、检查本地k8s环境

1.检查k8s节点状态

[root@k8s-master ~]# kubectl get nodes -owide
NAME         STATUS   ROLES                  AGE   VERSION   INTERNAL-IP     EXTERNAL-IP   OS-IMAGE                KERNEL-VERSION          CONTAINER-RUNTIME
k8s-master   Ready    control-plane,master   97d   v1.23.1   192.168.3.201   <none>        CentOS Linux 7 (Core)   3.10.0-957.el7.x86_64   containerd://1.6.6
k8s-node01   Ready    <none>                 97d   v1.23.1   192.168.3.202   <none>        CentOS Linux 7 (Core)   3.10.0-957.el7.x86_64   containerd://1.6.6
k8s-node02   Ready    <none>                 97d   v1.23.1   192.168.3.203   <none>        CentOS Linux 7 (Core)   3.10.0-957.el7.x86_64   containerd://1.6.6

2.检查helm版本

[root@k8s-master ~]# helm version
version.BuildInfoVersion:"v3.9.0", GitCommit:"7ceeda6c585217a19a1131663d8cd1f7d641b2a7", GitTreeState:"clean", GoVersion:"go1.17.5"

三、添加helm仓库

1.添加helm仓库源

[root@k8s-master ~]# helm repo add radondb https://radondb.github.io/radondb-mysql-kubernetes/
"radondb" has been added to your repositories

2.查看helm仓库列表

[root@k8s-master ~]# helm repo list
NAME   	URL                                                
bitnami	https://charts.bitnami.com/bitnami                 
radondb	https://radondb.github.io/radondb-mysql-kubernetes/

3.搜索radondb/mysql-operator的chart包

[root@k8s-master ~]# helm search repo radondb/mysql-operator
NAME                  	CHART VERSION	APP VERSION	DESCRIPTION                                       
radondb/mysql-operator	2.2.1        	v2.2.1     	Open Source,High Availability Cluster,based o...

四、部署 Operator

1.下载的chart包

[root@k8s-master radondb]# helm pull radondb/mysql-operator
[root@k8s-master radondb]# ls
mysql-operator-2.2.1.tgz

2.部署Operator

[root@k8s-master radondb]# helm install demo radondb/mysql-operator
NAME: demo
LAST DEPLOYED: Sat Oct  8 16:48:37 2022
NAMESPACE: default
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
You can create a new mysqlcluster by issuing:

cat <<EOF | kubectl apply -f-
apiVersion: mysql.radondb.com/v1alpha1
kind: MysqlCluster
metadata:
  name: sample
spec:
  replicas: 3
EOF

3.查看相关pod资源

[root@k8s-master radondb]# kubectl get pods
NAME                                      READY   STATUS    RESTARTS       AGE
app-agent-spmrv                           2/2     Running   27 (38m ago)   10d
app-agent-zdjst                           2/2     Running   42 (39m ago)   10d
demo-mysql-operator-969575-d8n9z          1/2     Running   0              105s
mysql001-8db95c95-g2nps                   1/1     Running   3 (38m ago)    3d15h
nfs-client-provisioner-779b7f4dfd-448qg   1/1     Running   4 (30h ago)    3d15h
quick-start-5ctw-dn-0-single-0            2/3     Running   9 (39m ago)    3d5h
quick-start-5ctw-gms-single-0             2/3     Running   9 (38m ago)    3d5h
webserver01                               1/1     Running   2 (39m ago)    2d

五、部署 RadonDB MySQL 集群

1.编辑部署yaml文件

apiVersion: mysql.radondb.com/v1alpha1
kind: MysqlCluster
metadata:
  name: sample
spec:
  replicas: 3
  mysqlVersion: "5.7"
  
  # the backupSecretName specify the secret file name which store S3 information,
  # if you want S3 backup or restore, please create backup_secret.yaml, uncomment below and fill secret name:
  # backupSecretName: 
  
  # if you want create mysqlcluster from S3, uncomment and fill the directory in S3 bucket below:
  # such as restoreFrom: "backup_202241423817"
  # restoreFrom: 
  
  # Restore from NFS, uncomment below and set the ip of NFS server
  # such as nfsServerAddress: "10.233.55.172"
  # nfsServerAddress: 
  mysqlOpts:
    user: radondb_usr
    password: RadonDB@123
    database: radondb
    initTokuDB: false

    # A simple map between string and string.
    # Such as:
    #    mysqlConf:
    #      expire_logs_days: "7"
    mysqlConf: 

    resources:
      requests:
        cpu: 100m
        memory: 256Mi
      limits:
        cpu: 500m
        memory: 1Gi

  xenonOpts:
    image: radondb/xenon:v2.2.1
    admitDefeatHearbeatCount: 5
    electionTimeout: 10000

    resources:
      requests:
        cpu: 50m
        memory: 128Mi
      limits:
        cpu: 100m
        memory: 256Mi

  metricsOpts:
    enabled: false
    image: prom/mysqld-exporter:v0.12.1

    resources:
      requests:
        cpu: 10m
        memory: 32Mi
      limits:
        cpu: 100m
        memory: 128Mi

  podPolicy:
    imagePullPolicy: IfNotPresent
    sidecarImage: radondb/mysql57-sidecar:v2.2.1
    busyboxImage: busybox:1.32

    slowLogTail: false
    auditLogTail: false

    labels: 
    annotations: 
    affinity: 
    priorityClassName: ""
    tolerations: []
    schedulerName: ""
    # extraResources defines quotas for containers other than mysql or xenon.
    extraResources:
      requests:
        cpu: 10m
        memory: 32Mi

  persistence:
    enabled: true
    accessModes:
    - ReadWriteOnce
    #storageClass: ""
    size: 20Gi

2.应用mysql_v1alpha1_mysqlcluster.yaml文件

[root@k8s-master radondb]# kubectl apply -f mysql_v1alpha1_mysqlcluster.yaml 
mysqlcluster.mysql.radondb.com/sample created

3.检查pod状态

[root@k8s-master radondb]# kubectl get pods
NAME                                      READY   STATUS    RESTARTS      AGE
demo-mysql-operator-969575-d8n9z          2/2     Running   0             28m
mysql001-8db95c95-g2nps                   1/1     Running   3 (65m ago)   3d15h
nfs-client-provisioner-779b7f4dfd-448qg   1/1     Running   4 (30h ago)   3d15h
sample-mysql-0                            3/3     Running   0             22m
sample-mysql-1                            3/3     Running   0             16m
sample-mysql-2                            3/3     Running   0             11m

4.检查svc服务

sample-mysql 的三节点 RadonDB MySQL 集群及用于访问节点的服务。

[root@k8s-master radondb]# kubectl get statefulset,svc
NAME                            READY   AGE
statefulset.apps/sample-mysql   3/3     24m

NAME                             TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)             AGE
service/kubernetes               ClusterIP   10.96.0.1       <none>        443/TCP             97d
service/mysql-operator-metrics   ClusterIP   10.111.23.64    <none>        8443/TCP            30m
service/radondb-mysql-webhook    ClusterIP   10.109.92.96    <none>        443/TCP             30m
service/sample-follower          ClusterIP   10.103.39.101   <none>        3306/TCP,8082/TCP   24m
service/sample-leader            ClusterIP   10.100.160.84   <none>        3306/TCP,8082/TCP   24m
service/sample-mysql             ClusterIP   None            <none>        3306/TCP,8082/TCP   24m

5.校验RadonDB MySQL 集群

[root@k8s-master radondb]# kubectl get crd | grep mysql.radondb.com
backups.mysql.radondb.com                             2022-10-08T08:48:36Z
mysqlclusters.mysql.radondb.com                       2022-10-08T08:48:36Z
mysqlusers.mysql.radondb.com                          2022-10-08T08:48:36Z

六、测试访问 RadonDB MySQL集群

1.查看RadonDB MySQL集群的svc服务

[root@k8s-master radondb]# kubectl get svc
NAME                     TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)             AGE
kubernetes               ClusterIP   10.96.0.1       <none>        443/TCP             97d
mysql-operator-metrics   ClusterIP   10.111.23.64    <none>        8443/TCP            35m
radondb-mysql-webhook    ClusterIP   10.109.92.96    <none>        443/TCP             35m
sample-follower          ClusterIP   10.103.39.101   <none>        3306/TCP,8082/TCP   29m
sample-leader            ClusterIP   10.100.160.84   <none>        3306/TCP,8082/TCP   29m
sample-mysql             ClusterIP   None            <none>        3306/TCP,8082/TCP   29m

2.通过 ClusterIP访问集群主节点

[root@k8s-node01 ~]# mysql -h 10.100.160.84 -P 3306 -u radondb_usr -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \\g.
Your MySQL connection id is 480
Server version: 5.7.34-37-log Percona Server (GPL), Release 37, Revision 7c516e9

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\\h' for help. Type '\\c' to clear the current input statement.

MySQL [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| radondb            |
+--------------------+
2 rows in set (1.10 sec)

MySQL [(none)]> 

3.通过 ClusterIP访问集群从节点

[root@k8s-node01 ~]# mysql -h  10.103.39.101 -P 3306 -u radondb_usr -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \\g.
Your MySQL connection id is 750
Server version: 5.7.34-37-log Percona Server (GPL), Release 37, Revision 7c516e9

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\\h' for help. Type '\\c' to clear the current input statement.

MySQL [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| radondb            |
+--------------------+
2 rows in set (0.93 sec)

MySQL [(none)]> 

七、创建RadonDB MySQL集群用户

1.创建Secret.yaml文件

[root@k8s-master radondb]# cat Secret.yaml 
apiVersion: v1
kind: Secret
metadata:
  name: sample-user-password    
data:
  pwdForSample: UmFkb25EQkAxMjM=  
  # pwdForSample2:
  #   # pwdForSample3:

2.创建 MysqlUser.yaml文件

[root@k8s-master radondb]# cat MysqlUser.yaml 
apiVersion: mysql.radondb.com/v1alpha1
kind: MysqlUser
metadata:
 
  name: sample-user-cr  # 用户 CR 名称,建议使用一个用户 CR 管理一个用户。
spec:
  user: admin  
  hosts:           
       - "%"
  permissions:
    - database: "*" 
      tables:        
         - "*"
      privileges:     
         - "ALL"
  
  
  userOwner: 
    clusterName: sample
    nameSpace: default 
  
  secretSelector:  
    secretName: sample-user-password  。  
    secretKey: pwdForSample  

3.应用账号密码相关yaml文件

[root@k8s-master radondb]# kubectl apply -f Secret.yaml ^C
[root@k8s-master radondb]# kubectl apply -f MysqlUser.yaml 

4.查看账号密码相关资源对象状态

[root@k8s-master radondb]# kubectl get MysqlUser
NAME             USERNAME   SUPERUSER   HOSTS   TLSTYPE   CLUSTER   NAMESPACE   AVAILABLE
sample-user-cr   admin      false       ["%"]   NONE      sample    default     True
[root@k8s-master radondb]# kubectl get secrets sample-secret 
NAME            TYPE     DATA   AGE
sample-secret   Opaque   13     75m

八、测试新建用户访问RadonDB MySQL集群

1.查看主节点状态

[root@k8s-node01 ~]# mysql -h 10.100.160.84 -P 3306 -uadmin -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \\g.
Your MySQL connection id is 1210
Server version: 5.7.34-37-log Percona Server (GPL), Release 37, Revision 7c516e9

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\\h' for help. Type '\\c' to clear the current input statement.

MySQL [(none)]> show master status;
+------------------+----------+--------------+------------------+-------------------------------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set                         |
+------------------+----------+--------------+------------------+-------------------------------------------+
| mysql-bin.000002 |    23296 |              |                  | 0e62556e-46e8-11ed-85ff-b66837dd89be:1-94 |
+------------------+----------+--------------+----------猿创征文 | 使用Docker部署openGauss国产数据库

猿创征文 | 国产数据库之openGauss的单机主备部署及快速入门

猿创征文 | 国产数据实战之docker部署MyWebSQL数据库管理工具

猿创征文 | 国产数据库实战之使用Docker部署TiDB集群

猿创征文 | 国产数据库实战之使用Docker部署TiDB集群

猿创征文 | 国产数据库实战之TiDB 数据库快速入门