简介

Vitess 是用于部署、扩展和管理大型 MySQL 实例集群的数据库解决方案。

特性

  • 扩展性 VitessMySQL 数据库的很多重要特性和 NoSQL 数据库的可扩展性于一体。其内建拆分分片功能使您能够对您的 MySQL 数据库集群无限水平扩展,同时无需为应用添加分片逻辑。
  • 性能 Vitess 自动重写对数据库性能有损害的查询。它还使用缓存机制来调解查询,并防止重复查询同时到达您的数据库。通过每夜的基准來监控性能表现。
  • 管理 Vitess 可以支持自动处理主故障转移和备份等功能。它使用分布式元数据服务来跟踪和管理服务器,使您的应用程序无需关心数据库拓扑变化。
  • 连接池 Vitess 避免了 MySQL 连接的高内存开销。 Vitess 服务器轻松地一次处理数千个连接。
  • 分片管理 MySQL 本身并不提供拆分分片功能,但是您的业务数据量增大到一定程度是您是需要增加集群的。Vitess 提供在线拆分功能,只需要很少的时间就完成新集群的切换,无需您在应用程序中添加任何拆分逻辑。
  • 工作流 Vitess 会跟踪有关集群配置的所有元数据,以便集群拓扑始终是最新的,对不同的客户端保持一致。

Vitess 可以运行在 Kubernetes 和虚拟机上。下面主要介绍一下 Kubernetes 上的安装使用,更详细的文档可以参考官方文档

前置条件

  • 拥有一套 Kubernetes 集群
    • 若没有,可使用 minikube 快速在本地运行一个 minikube start --cpus=4 --memory=8000
    • 还需要默认存储类,用于保存有状态的服务数据
  • 下载 vitess
    • git clone https://github.com/vitessio/vitess.git
  • 安装 etcd 管理端
  • 安装 helm

启动单片 keyspace 集群

1git clone https://github.com/vitessio/vitess.git
2git checkout v11.0.1
3cd vitess/examples/helm

在此目录中,您将看到一组yaml文件。每个文件名的第一个数字表示示例的阶段。接下来的两位数字表示执行它们的顺序。例如'101_initial_cluster.yaml’是第一阶段的第一个文件。我们现在将执行:

 1helm install vitess ../../helm/vitess -f 101_initial_cluster.yaml
 2NAME: vitess
 3LAST DEPLOYED: Thu Sep 23 18:58:48 2021
 4NAMESPACE: default
 5STATUS: deployed
 6REVISION: 1
 7TEST SUITE: None
 8NOTES:
 9Release name: vitess
10
11To access administrative web pages, start a proxy with:
12  kubectl proxy --port=8001
13
14Then use the following URLs:
15
16      vtctld: http://localhost:8001/api/v1/namespaces/default/services/vtctld:web/proxy/app/
17      vtgate: http://localhost:8001/api/v1/namespaces/default/services/vtgate-zone1:web/proxy/

这将创建一个带有单个keyspace空间的初始Vitess集群。

验证

 1kubectl get po,jobs
 2NAME                                           READY   STATUS      RESTARTS   AGE
 3pod/commerce-apply-schema-initial-xh8xx        0/1     Completed   0          92s
 4pod/commerce-apply-vschema-initial-rwzpl       0/1     Completed   0          92s
 5pod/nfs-client-provisioner-78dff76b6d-xsqnt    1/1     Running     0          37m
 6pod/vtctld-58bd955948-bxgzs                    1/1     Running     0          92s
 7pod/vtgate-zone1-c7444bbf6-62ppl               1/1     Running     0          92s
 8pod/zone1-commerce-0-init-shard-master-52mdf   0/1     Completed   0          92s
 9pod/zone1-commerce-0-replica-0                 2/2     Running     0          92s
10pod/zone1-commerce-0-replica-1                 2/2     Running     0          92s
11pod/zone1-commerce-0-replica-2                 2/2     Running     0          91s
12
13NAME                                           COMPLETIONS   DURATION   AGE
14job.batch/commerce-apply-schema-initial        1/1           67s        92s
15job.batch/commerce-apply-vschema-initial       1/1           58s        92s
16job.batch/zone1-commerce-0-init-shard-master   1/1           63s        92s

测试连接

 1mysql -h 10.244.0.24
 2Welcome to the MariaDB monitor.  Commands end with ; or \g.
 3Your MySQL connection id is 1
 4Server version: 5.7.9-Vitess Percona Server (GPL), Release 29, Revision 11ad961
 5
 6Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
 7
 8Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
 9
10MySQL [(none)]> use commerce
11Database changed
12MySQL [commerce]> show tables;
13+--------------------+
14| Tables_in_commerce |
15+--------------------+
16| corder             |
17| customer           |
18| product            |
19+--------------------+
203 rows in set (0.01 sec)
21
22MySQL [commerce]> 

插入并验证

1mysql < ../common/insert_commerce_data.sql
2mysql --table < ../common/select_commerce_data.sql

使用自定义 Keyspace

1helm upgrade vitess ../../helm/vitess/ -f 201_customer_tablets.yaml

移动表

1vtctlclient MoveTables -workflow=commerce2customer commerce customer '{"customer":{}, "corder":{}}'

验证

1vtctlclient VDiff customer.commerce2customer

使用 Vitess Operator 安装

部署 Operator

 1git clone git@github.com:vitessio/vitess.git
 2git checkout v11.0.1
 3cd vitess/examples/operator
 4kubectl apply -f operator.yaml
 5customresourcedefinition.apiextensions.k8s.io/etcdlockservers.planetscale.com created
 6customresourcedefinition.apiextensions.k8s.io/vitessbackups.planetscale.com created
 7customresourcedefinition.apiextensions.k8s.io/vitessbackupstorages.planetscale.com created
 8customresourcedefinition.apiextensions.k8s.io/vitesscells.planetscale.com created
 9customresourcedefinition.apiextensions.k8s.io/vitessclusters.planetscale.com created
10customresourcedefinition.apiextensions.k8s.io/vitesskeyspaces.planetscale.com created
11customresourcedefinition.apiextensions.k8s.io/vitessshards.planetscale.com created
12serviceaccount/vitess-operator created
13role.rbac.authorization.k8s.io/vitess-operator created
14rolebinding.rbac.authorization.k8s.io/vitess-operator created
15priorityclass.scheduling.k8s.io/vitess created
16priorityclass.scheduling.k8s.io/vitess-operator-control-plane created
17deployment.apps/vitess-operator created

部署一个集群

1kubectl apply -f 101_initial_cluster.yaml
2vitesscluster.planetscale.com/example created
3secret/example-cluster-config created

检查

 1kubectl get pods
 2NAME                                             READY   STATUS    RESTARTS   AGE
 3example-etcd-faf13de3-1                          1/1     Running   0          76s
 4example-etcd-faf13de3-2                          1/1     Running   0          76s
 5example-etcd-faf13de3-3                          1/1     Running   0          76s
 6example-vttablet-zone1-2469782763-bfadd780       3/3     Running   1          76s
 7example-vttablet-zone1-2548885007-46a852d0       3/3     Running   1          76s
 8example-zone1-vtctld-1d4dcad0-69fbcf55cd-rprht   1/1     Running   0          76s
 9example-zone1-vtgate-bc6cde92-c988b4f94-s2p9z    1/1     Running   1          76s
10vitess-operator-9874b4ff-wktqr                   1/1     Running   0          94s