6.2 分布式数据库TiDB

官方网站:https://pingcap.com/

6.2.1 TiDB 简介

TiDB 是 PingCAP 公司设计的开源分布式 HTAP (Hybrid Transactional and Analytical Processing) 数据库,结合了传统的 RDBMS 和 NoSQL 的最佳特性。TiDB 兼容 MySQL,支持无限的水平扩展,具备强一致性和高可用性。TiDB 的目标是为 OLTP (Online Transactional Processing) 和 OLAP (Online Analytical Processing) 场景提供一站式的解决方案。

TiDB 具备如下特性:

  • 高度兼容 MySQL

大多数情况下,无需修改代码即可从 MySQL 轻松迁移至 TiDB,分库分表后的 MySQL 集群亦可通过 TiDB 工具进行实时迁移。

  • 水平弹性扩展

通过简单地增加新节点即可实现 TiDB 的水平扩展,按需扩展吞吐或存储,轻松应对高并发、海量数据场景。

  • 分布式事务

TiDB 100% 支持标准的 ACID 事务。

  • 真正金融级高可用

相比于传统主从 (M-S) 复制方案,基于 Raft 的多数派选举协议可以提供金融级的 100% 数据强一致性保证,且在不丢失大多数副本的前提下,可以实现故障的自动恢复 (auto-failover),无需人工介入。

  • 一站式 HTAP 解决方案

TiDB 作为典型的 OLTP 行存数据库,同时兼具强大的 OLAP 性能,配合 TiSpark,可提供一站式 HTAP 解决方案,一份存储同时处理 OLTP & OLAP,无需传统繁琐的 ETL 过程。

  • 云原生 SQL 数据库

TiDB 是为云而设计的数据库,支持公有云、私有云和混合云,配合 TiDB Operator 项目 可实现自动化运维,使部署、配置和维护变得十分简单。

TiDB 的设计目标是 100% 的 OLTP 场景和 80% 的 OLAP 场景,更复杂的 OLAP 分析可以通过 TiSpark 项目来完成。

TiDB 对业务没有任何侵入性,能优雅的替换传统的数据库中间件、数据库分库分表等 Sharding 方案。同时它也让开发运维人员不用关注数据库 Scale 的细节问题,专注于业务开发,极大的提升研发的生产力。

6.2.2 kubernetes部署TiDB

6.2.2.1 安装PingCap Charts

helm repo add pingcap http://charts.pingcap.org/
"pingcap" has been added to your repositories

helm repo list
NAME            URL
stable          https://kubernetes-charts.storage.googleapis.com
local           http://127.0.0.1:8879/charts
pingcap         http://charts.pingcap.org/

helm repo update
Hang tight while we grab the latest from your chart repositories...
...Skip local chart repository
...Successfully got an update from the "pingcap" chart repository
...Successfully got an update from the "stable" chart repository
Update Complete.

# helm2
helm search tidb-cluster -l
# helm3
helm search repo tidb-cluster -l
NAME                    CHART VERSION   APP VERSION     DESCRIPTION
pingcap/tidb-cluster    v1.0.6                          A Helm chart for TiDB Cluster
pingcap/tidb-cluster    v1.0.5                          A Helm chart for TiDB Cluster
pingcap/tidb-cluster    v1.0.4                          A Helm chart for TiDB Cluster
pingcap/tidb-cluster    v1.0.3                          A Helm chart for TiDB Cluster
pingcap/tidb-cluster    v1.0.2                          A Helm chart for TiDB Cluster
pingcap/tidb-cluster    v1.0.1                          A Helm chart for TiDB Cluster
pingcap/tidb-cluster    v1.0.0                          A Helm chart for TiDB Cluster
pingcap/tidb-cluster    latest                          A Helm chart for TiDB Cluster

#helm2
helm search tidb-operator -l
# helm3
helm search repo tidb-operator -l
NAME                    CHART VERSION   APP VERSION     DESCRIPTION
pingcap/tidb-operator   v1.0.6                          tidb-operator Helm chart for Kubernetes
pingcap/tidb-operator   v1.0.5                          tidb-operator Helm chart for Kubernetes
pingcap/tidb-operator   latest                          tidb-operator Helm chart for Kubernetes
pingcap/tidb-operator   v1.0.4                          tidb-operator Helm chart for Kubernetes
pingcap/tidb-operator   v1.0.3                          tidb-operator Helm chart for Kubernetes
pingcap/tidb-operator   v1.0.2                          tidb-operator Helm chart for Kubernetes
pingcap/tidb-operator   v1.0.1                          tidb-operator Helm chart for Kubernetes
pingcap/tidb-operator   v1.0.0                          tidb-operator Helm chart for Kubernetes

6.2.2.2 安装tidb-operator

安装crd

kubectl apply -f https://raw.githubusercontent.com/pingcap/tidb-operator/master/manifests/crd.yaml
customresourcedefinition.apiextensions.k8s.io/tidbclusters.pingcap.com created
customresourcedefinition.apiextensions.k8s.io/backups.pingcap.com created
customresourcedefinition.apiextensions.k8s.io/restores.pingcap.com created
customresourcedefinition.apiextensions.k8s.io/backupschedules.pingcap.com created
customresourcedefinition.apiextensions.k8s.io/tidbmonitors.pingcap.com created
customresourcedefinition.apiextensions.k8s.io/tidbinitializers.pingcap.com created

查看crd

kubectl get crd tidbclusters.pingcap.com
NAME                       CREATED AT
tidbclusters.pingcap.com   2020-02-10T07:36:10Z

获取charts的values文件

mkdir -p /home/tidb/tidb-operator
helm inspect values pingcap/tidb-operator --version=v1.0.6 > /home/tidb/tidb-operator/values-tidb-operator.yaml

修改配置,主要修改时区,镜像地址等

cp /home/tidb/tidb-operator/values-tidb-operator.yaml{,.ori}
diff /home/tidb/tidb-operator/values-tidb-operator.yaml.ori /home/tidb/tidb-operator/values-tidb-operator.yaml
12c12
< timezone: UTC
---
> timezone: Asia/Shanghai
18c18
< defaultStorageClassName: local-storage
---
> defaultStorageClassName: managed-nfs-storage
60c60
<   kubeSchedulerImageName: k8s.gcr.io/kube-scheduler
---
>   kubeSchedulerImageName: gcr.io/google-containers/kube-scheduler

安装tidb-operator

kubectl create namespace tidb-admin
namespace/tidb-admin created

helm install tidb-operator pingcap/tidb-operator --namespace=tidb-admin --version=v1.0.6 -f /home/tidb/tidb-operator/values-tidb-operator.yaml
NAME: tidb-operator
LAST DEPLOYED: Mon Feb 10 15:41:39 2020
NAMESPACE: tidb-admin
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
1. Make sure tidb-operator components are running
   kubectl get pods --namespace tidb-admin -l app.kubernetes.io/instance=tidb-operator
2. Install CRD
   kubectl apply -f https://raw.githubusercontent.com/pingcap/tidb-operator/master/manifests/crd.yaml
   kubectl get customresourcedefinitions
3. Modify tidb-cluster/values.yaml and create a TiDB cluster by installing tidb-cluster charts
   helm install tidb-cluster

检查安装

kubectl get po -n tidb-admin -l app.kubernetes.io/name=tidb-operator
NAME                                     READY   STATUS    RESTARTS   AGE
tidb-controller-manager-b69dc6cd-tmtzr   1/1     Running   0          23s
tidb-scheduler-66844bbc55-j4nx8          2/2     Running   0          23s

6.2.2.3 安装 TiDB Cluster

获取charts的values.yaml文件

mkdir -p /home/tidb/tidb-cluster
helm inspect values pingcap/tidb-cluster --version=v1.0.6 > /home/tidb/tidb-cluster/values-v1.0.6.yaml

配置values,修改时区存储类等

cp /home/tidb/tidb-cluster/values-v1.0.6.yaml{,.ori}
diff /home/tidb/tidb-cluster/values-v1.0.6.yaml.ori /home/tidb/tidb-cluster/values-v1.0.6.yaml
21c21
< timezone: UTC
---
> timezone: Asia/Shanghai
71c71
<   storageClassName: local-storage
---
>   storageClassName: managed-nfs-storage
207c207
<   storageClassName: local-storage
---
>   storageClassName: managed-nfs-storage
389c389
<   storageClassName: local-storage
---
>   storageClassName: managed-nfs-storage
393c393
<     imagePullPolicy: Always
---
>     imagePullPolicy: IfNotPresent
477c477
<     storageClassName: local-storage
---
>     storageClassName: managed-nfs-storage
545c545
<     storageClassName: local-storage
---
>     storageClassName: managed-nfs-storage
622c622
<   storageClassName: local-storage
---
>   storageClassName: managed-nfs-storage
686c686
<   storageClassName: local-storage
---
>   storageClassName: managed-nfs-storage

部署tidb-cluster

kubectl create namespace tidb-cluster
namespace/tidb-cluster created

helm install tidb-cluster pingcap/tidb-cluster  --namespace=tidb-cluster --version=v1.0.6 -f /home/tidb/tidb-cluster/values-v1.0.6.yaml
NAME: tidb-cluster
LAST DEPLOYED: Mon Feb 10 20:52:40 2020
NAMESPACE: tidb-cluster
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
Cluster Startup
1. Watch tidb-cluster up and running
     watch kubectl get pods --namespace tidb-cluster -l app.kubernetes.io/instance=tidb-cluster -o wide
2. List services in the tidb-cluster
     kubectl get services --namespace tidb-cluster -l app.kubernetes.io/instance=tidb-cluster

Cluster access
* Access tidb-cluster using the MySQL client
    kubectl port-forward -n tidb-cluster svc/tidb-cluster-tidb 4000:4000 &
    mysql -h 127.0.0.1 -P 4000 -u root -D test
  Set a password for your user
    SET PASSWORD FOR 'root'@'%' = 'et80AjO5Ly'; FLUSH PRIVILEGES;
* View monitor dashboard for TiDB cluster
   kubectl port-forward -n tidb-cluster svc/tidb-cluster-grafana 3000:3000
   Open browser at http://localhost:3000. The default username and password is admin/admin.
   If you are running this from a remote machine, you must specify the server's external IP address.

检查pod运行情况

kubectl get po -n tidb-cluster
NAME                                      READY   STATUS    RESTARTS   AGE
tidb-cluster-discovery-77c85f6c59-72wrx   1/1     Running   0          35m
tidb-cluster-pd-0                         1/1     Running   5          34m
tidb-cluster-pd-1                         1/1     Running   1          34m
tidb-cluster-pd-2                         1/1     Running   0          34m
tidb-cluster-tidb-0                       2/2     Running   0          7m39s
tidb-cluster-tidb-1                       2/2     Running   0          7m39s
tidb-cluster-tikv-0                       1/1     Running   0          8m20s
tidb-cluster-tikv-1                       1/1     Running   0          8m20s
tidb-cluster-tikv-2                       1/1     Running   0          8m19s

NAME                            TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)                          AGE
tidb-cluster-discovery          ClusterIP   10.233.41.170   <none>        10261/TCP                        3m51s
tidb-cluster-grafana            NodePort    10.233.61.238   <none>        3000:32252/TCP                   3m50s
tidb-cluster-monitor-reloader   NodePort    10.233.47.135   <none>        9089:30451/TCP                   3m51s
tidb-cluster-pd                 ClusterIP   10.233.33.120   <none>        2379/TCP                         2m40s
tidb-cluster-pd-peer            ClusterIP   None            <none>        2380/TCP                         2m40s
tidb-cluster-prometheus         NodePort    10.233.22.58    <none>        9090:32307/TCP                   3m51s
tidb-cluster-tidb               NodePort    10.233.27.124   <none>        4000:32095/TCP,10080:30000/TCP   3m51s

测试访问

yum -y install mariadb
kubectl port-forward -n tidb-cluster svc/tidb-cluster-tidb 4000:4000 &
mysql -h 127.0.0.1 -P 4000 -u root -D test
Handling connection for 4000
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.7.25-TiDB-v3.0.5 MySQL Community Server (Apache License 2.0)

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 [test]> show databases;
+--------------------+
| Database           |
+--------------------+
| INFORMATION_SCHEMA |
| PERFORMANCE_SCHEMA |
| mysql              |
| test               |
+--------------------+
4 rows in set (0.01 sec)

MySQL [test]> select @@version;
+--------------------+
| @@version          |
+--------------------+
| 5.7.25-TiDB-v3.0.5 |
+--------------------+
1 row in set (0.01 sec)

其他操作跟MySQL高度兼容(略)

Copyright © huangzhongde.cn 2021 all right reserved,powered by Gitbook该文件修订时间: 2022-01-28 00:02:24

results matching ""

    No results matching ""