1. 程式人生 > >基於 DC2 和 Docker Compose 搭建 TiDB

基於 DC2 和 Docker Compose 搭建 TiDB

TiDB 簡介

TiDB 是一款由 PingCAP 設計的開源分散式 HTAP 資料庫。結合了傳統的 RDBMS 和 NoSQL 優勢,TiDB(NewSQL)具備以下特性:

  • 高度支援 MySQL 協議
  • 輕鬆應對水平擴充套件
  • 100% 支援分散式 ACID 事務
  • 基於 Raft 協議保證資料一致性,並實現故障自動恢復
  • 一站式 HTAP 解決方案
  • 雲原生 SQL 資料庫

TiDB 可以輕鬆替換現有的以 MySQL 協議為基礎的各類資料庫中介軟體或者分庫分表方案,減少開發人員對資料庫 Scale 細節問題的關注來提升生產力。

TiDB 架構

在這裡插入圖片描述

TiKV Server

TiKV Server 是一個分散式 Key-Value 儲存。儲存資料的基本單位是 Region,每個 TiKV 節點會負責多個 Region。TiKV 使用 Raft 協議做複製,保持資料的一致性和容災。副本以 Region 為單位進行管理,不同節點上的多個 Region 構成一個 Raft Group,互為副本。資料在多個 TiKV 之間的負載均衡由 PD 以 Region 為單位進行排程。

延伸閱讀:
https://pingcap.com/blog-cn/tidb-internal-1/

PD Server

Placement Driver(簡稱 PD)是整個叢集的管理模組,其主要工作有三個:一是儲存叢集的元資訊(某個 Key 儲存在哪個 TiKV 節點);二是對 TiKV 叢集進行排程和負載均衡(如資料的遷移、Raft group leader 的遷移等);三是分配全域性唯一且遞增的事務 ID。PD 是一個叢集,需要部署奇數個節點,一般線上推薦至少部署 3 個節點。

延伸閱讀:
https://pingcap.com/blog-cn/tidb-internal-3/

TiDB Server

TiDB Server 負責接收 SQL 請求,處理 SQL 相關的邏輯,並通過 PD 找到儲存計算所需資料的 TiKV 地址,與 TiKV 互動獲取資料,最終返回結果。TiDB Server 是無狀態的,其本身並不儲存資料,只負責計算,可以無限水平擴充套件,可以通過負載均衡元件(如 LVS、HAProxy 或 F5)對外提供統一的接入地址。

延伸閱讀:
https://pingcap.com/blog-cn/tidb-internal-2/

TiSpark

TiSpark 作為 TiDB 中解決使用者複雜 OLAP 需求的主要元件,將 Spark SQL 直接執行在 TiDB 儲存層上,同時融合 TiKV 分散式叢集的優勢,並融入大資料社群生態。至此,TiDB 可以通過一套系統,同時支援 OLTP 與 OLAP,免除使用者資料同步的煩惱。

環境依賴

本文基於 Linux 發行版本 Centos7.x 配合流行的包管理工具 yum 進行演示,因此您需要準備一臺 Centos7.x 版本的伺服器並安裝好 yum。滴滴雲 DC2 擁有價效比高、安全可靠和秒級計費等優勢,建議您直接購買 DC2 來學習本教程。

以下內容基於滴滴雲 DC2(CentOS7.3 16 核 CPU 32GB 記憶體 90GB HDD 儲存按時長計費)公網 IP(116.85.37.138 按出流量計費)進行演示。

安裝過程

登入 DC2 並切換到 root 賬戶

$ ssh [email protected]
$ sudo -iu root

安裝 Docker-CE

  1. 切換 yum 源到國內:
$ yum-config-manager \
    --add-repo \
    https://mirrors.ustc.edu.cn/docker-ce/linux/centos/docker-ce.repo
  1. 更新 yum 軟體源快取,並安裝 Docker-CE:
$ yum makecache fast
$ yum install docker-ce
  1. 啟動 Docker-CE:
$ systemctl enable docker
$ systemctl start docker
  1. 測試 Docker-CE 是否安裝成功, 輸出以下資訊,說明安裝成功:
$ docker run hello-world
Unable to find image 'hello-world:latest' locally

latest: Pulling from library/hello-world
d1725b59e92d: Pulling fs layer 
d1725b59e92d: Pull complete 
Digest: sha256:0add3ace90ecb4adbf7777e9aacf18357296e799f81cabc9fde470971e499788
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.
...

安裝Docker Compose

Compose 專案是 Docker 官方的開源專案,負責實現對 Docker 容器叢集的快速編排。下載二進位制檔案並進行安裝:

$ curl -L https://github.com/docker/compose/releases/download/1.17.1/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
$ chmod +x /usr/local/bin/docker-compose

安裝 TiDB

  1. 下載 tidb-docker-compose:
$ git clone https://github.com/pingcap/tidb-docker-compose.git
  1. 建立叢集:
$ cd tidb-docker-compose && docker-compose pull

Pulling dashboard-installer ... done
Pulling pd2                 ... done
Pulling pd1                 ... done
Pulling pd0                 ... done
Pulling tikv0               ... done
Pulling tikv1               ... done
Pulling tikv2               ... done
Pulling prometheus          ... done
Pulling tispark-master      ... done
Pulling tispark-slave0      ... done
Pulling tidb                ... done
Pulling tidb-vision         ... done
Pulling grafana             ... done
Pulling pushgateway         ... done
  1. 啟動叢集:
$ docker-compose up -d

Creating network "tidb-docker-compose_default" with the default driver
Creating tidb-docker-compose_grafana_1             ... done
Creating tidb-docker-compose_pd2_1                 ... done
Creating tidb-docker-compose_pd0_1                 ... done
Creating tidb-docker-compose_dashboard-installer_1 ... done
Creating tidb-docker-compose_pd1_1                 ... done
Creating tidb-docker-compose_pushgateway_1         ... done
Creating tidb-docker-compose_tidb-vision_1         ... done
Creating tidb-docker-compose_prometheus_1          ... done
Creating tidb-docker-compose_tikv0_1               ... done
Creating tidb-docker-compose_tikv2_1               ... done
Creating tidb-docker-compose_tikv1_1               ... done
Creating tidb-docker-compose_tispark-master_1      ... done
Creating tidb-docker-compose_tidb_1                ... done
Creating tidb-docker-compose_tispark-slave0_1      ... done

驗證 TiDB 功能

  1. 安裝 MySQL 客戶端,並連線 TiDB 預設埠:
$ yum install -y mysql
  1. 下載 MySQL 驗證資料:
$ git clone https://github.com/datacharmer/test_db.git
  1. 連線 TiDB 預設埠隨意操作資料進行驗證:
$ mysql -h 127.0.0.1 -P 4000 -u root
$ MySQL > source test_db/employees.sql;
$ MySQL [employees]> desc employees;
+------------+---------------+------+------+---------+-------+
| Field      | Type          | Null | Key  | Default | Extra |
+------------+---------------+------+------+---------+-------+
| emp_no     | int(11)       | NO   | PRI  | NULL    |       |
| birth_date | date          | NO   |      | NULL    |       |
| first_name | varchar(14)   | NO   |      | NULL    |       |
| last_name  | varchar(16)   | NO   |      | NULL    |       |
| gender     | enum('M','F') | NO   |      | NULL    |       |
| hire_date  | date          | NO   |      | NULL    |       |
+------------+---------------+------+------+---------+-------+
  1. 訪問叢集 Grafana 監控頁面(http://116.85.37.138:3000)以及叢集資料視覺化頁面(http://116.85.37.138:8010)進行驗證

在這裡插入圖片描述

參考

https://pingcap.com/docs-cn/
https://legacy.gitbook.com/book/yeasy/docker_practice/details