一、简介
1、TIDB介绍
TiDB 是一款同时支持在线事务处理与在线分析处理 (Hybrid Transactional and Analytical Processing, HTAP) 的融合型分布式数据库产品,具备水平扩容或者缩容、金融级高可用、实时 HTAP、云原生的分布式数据库、兼容 MySQL 5.7 协议和 MySQL 生态等重要特性。TiDB 适合高可用、强一致要求较高、数据规模较大等各种应用场景。
2、五大核心特性
- 一键水平扩容或者缩容
- 金融级高可用
- 实时 HTAP
- 云原生的分布式数据库
- 兼容 MySQL 5.7 协议和 MySQL 生态
3、TIDB整体架构
组件介绍:
- TiDB Server:SQL 层,对外暴露 MySQL 协议的连接 endpoint,负责接受客户端的连接,执行 SQL 解析和优化,最终生成分布式执行计划。TiDB 层本身是无状态的,实践中可以启动多个 TiDB 实例,通过负载均衡组件(如 LVS、HAProxy 或 F5)对外提供统一的接入地址,客户端的连接可以均匀地分摊在多个 TiDB 实例上以达到负载均衡的效果。TiDB Server 本身并不存储数据,只是解析 SQL,将实际的数据读取请求转发给底层的存储节点 TiKV(或 TiFlash)。
- PD (Placement Driver) Server:整个 TiDB 集群的元信息管理模块,负责存储每个 TiKV 节点实时的数据分布情况和集群的整体拓扑结构,提供 TiDB Dashboard 管控界面,并为分布式事务分配事务 ID。PD 不仅存储元信息,同时还会根据 TiKV 节点实时上报的数据分布状态,下发数据调度命令给具体的 TiKV 节点,可以说是整个集群的“大脑”。此外,PD 本身也是由至少 3 个节点构成,拥有高可用的能力。建议部署奇数个 PD 节点。
- TiKV Server:负责存储数据,从外部看 TiKV 是一个分布式的提供事务的 Key-Value 存储引擎。存储数据的基本单位是 Region,每个 Region 负责存储一个 Key Range(从 StartKey 到 EndKey 的左闭右开区间)的数据,每个 TiKV 节点会负责多个 Region。TiKV 的 API 在 KV 键值对层面提供对分布式事务的原生支持,默认提供了 SI (Snapshot Isolation) 的隔离级别,这也是 TiDB 在 SQL 层面支持分布式事务的核心。TiDB 的 SQL 层做完 SQL 解析后,会将 SQL 的执行计划转换为对 TiKV API 的实际调用。所以,数据都存储在 TiKV 中。另外,TiKV 中的数据都会自动维护多副本(默认为三副本),天然支持高可用和自动故障转移。
- TiFlash:TiFlash 是一类特殊的存储节点。和普通 TiKV 节点不一样的是,在 TiFlash 内部,数据是以列式的形式进行存储,主要的功能是为分析型的场景加速。
二、部署TIDB
1、安装TIDB-operator的CRDs
kubectl create -f https://raw.githubusercontent.com/pingcap/tidb-operator/v1.3.1/manifests/crd.yaml
2、添加helm仓库
helm repo add pingcap https://charts.pingcap.org/
3、查看tidb的operator:
helm search repo tidb-operator -l
#我们安装v1.3.1版本:
helm fetch pingcap/tidb-operator --version v1.3.1
#修改values.yml
vim values.yml
scheduler:
kubeSchedulerImageName: registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler
#修改镜像,默认镜像无法拉取
4、安装tidb-operator:
helm upgrade --install tidb-operator ./tidb-operator -n tidb-test
#查看pod是否已启动
5、安装tidb
#创建tidb集群的service
kubectl create ns tidb-cluster
#下载tidb deployment文件
curl -O https://raw.githubusercontent.com/pingcap/tidb-operator/master/examples/basic-cn/tidb-cluster.yaml
#修改tidb-cluster.yaml文件:
vim tidb-cluster.yaml
# IT IS NOT SUITABLE FOR PRODUCTION USE.
# This YAML describes a basic TiDB cluster with minimum resource requirements,
# which should be able to run in any Kubernetes cluster with storage support.
apiVersion: pingcap.com/v1alpha1
kind: TidbCluster
metadata:
name: tidb
spec:
version: v5.4.2
timezone: UTC
pvReclaimPolicy: Retain
enableDynamicConfiguration: true
configUpdateStrategy: RollingUpdate
discovery: {}
helper:
image: alpine:3.16.0
pd:
baseImage: uhub.service.ucloud.cn/pingcap/pd
maxFailoverCount: 0
replicas: 3
# if storageClassName is not set, the default Storage Class of the Kubernetes cluster will be used
storageClassName: lowcode-prod
requests:
storage: "20Gi"
config: {}
tikv:
baseImage: uhub.service.ucloud.cn/pingcap/tikv
maxFailoverCount: 0
# If only 1 TiKV is deployed, the TiKV region leader
# cannot be transferred during upgrade, so we have
# to configure a short timeout
evictLeaderTimeout: 1m
replicas: 3
# if storageClassName is not set, the default Storage Class of the Kubernetes cluster will be used
storageClassName: lowcode-prod
requests:
storage: "50Gi"
config:
storage:
# In basic examples, we set this to avoid using too much storage.
reserve-space: "0MB"
rocksdb:
# In basic examples, we set this to avoid the following error in some Kubernetes clusters:
# "the maximum number of open file descriptors is too small, got 1024, expect greater or equal to 82920"
max-open-files: 256
raftdb:
max-open-files: 256
tidb:
baseImage: uhub.service.ucloud.cn/pingcap/tidb
maxFailoverCount: 0
replicas: 3
service:
type: ClusterIP
config: {}
#安装tidb
kubectl apply -f tidb-cluster.yaml -n tidb-cluster
#查看是否启动
kubectl get pod -n tidb-cluster
6、安装tidb的监控:
#下载monitor deployment文件
curl -O https://raw.githubusercontent.com/pingcap/tidb-operator/master/examples/basic-cn/tidb-monitor.yaml
#安装:
kubectl apply -f tidb-monitor.yaml -n tidb-cluster
7、连接tidb
#配置端口转发:
kubectl port-forward svc/tidb-tidb 4000:4000 -n tidb-cluster
#链接:
mysql -u root -h 127.0.0.1 -P 4000
#查看tidb版本:
select tidb_version();
8、访问grafana
#配置端口转发:
kubectl port-forward svc/tidb-grafana 3000:3000 -n tidb-cluster
#浏览器访问:
http://127.0.0.1:3000
#用户名密码:admin/admin
【完成】