使用Docker-Compose快速部署TiDB集群环境
一、快速部署
参考连接:https://docs.pingcap.com/zh/tidb/v3.0/deploy-test-cluster-using-docker-compose
本文档介绍如何在单机上通过 Docker Compose 快速一键部署一套 TiDB 测试集群。Docker Compose 可以通过一个 YAML 文件定义多个容器的应用服务,然后一键启动或停止。
确保你的机器上已安装:
- Docker(17.06.0 及以上版本)
- Docker Compose
- Git
1.1、下载tidb-docker-compose
[root@docker35 ~]# git clone https://github.com/pingcap/tidb-docker-compose.git
Cloning into 'tidb-docker-compose'...
remote: Enumerating objects: 8, done.
remote: Counting objects: 100% (8/8), done.
remote: Compressing objects: 100% (8/8), done.
remote: Total 554 (delta 0), reused 6 (delta 0), pack-reused 546
Receiving objects: 100% (554/554), 284.17 KiB | 204.00 KiB/s, done.
Resolving deltas: 100% (243/243), done.
1.2、创建并启动集群
创建并启动集群,获取最新 Docker 镜像:
cd tidb-docker-compose && docker-compose pull && docker-compose up -d
执行过程:
[root@docker35 ~]# cd tidb-docker-compose/
[root@docker35 tidb-docker-compose]# ll
total 64
drwxr-xr-x 3 root root 60 Apr 14 15:13 compose
drwxr-xr-x 4 root root 4096 Apr 14 15:13 config
drwxr-xr-x 3 root root 59 Apr 14 15:13 dashboard-installer
drwxr-xr-x 3 root root 19 Apr 14 15:13 docker
-rw-r--r-- 1 root root 11343 Apr 14 15:13 docker-compose-binlog.yml
-rw-r--r-- 1 root root 292 Apr 14 15:13 docker-compose-test.yml
-rw-r--r-- 1 root root 1566 Apr 14 15:13 docker-compose-tiflash-nightly.yml
-rw-r--r-- 1 root root 5445 Apr 14 15:13 docker-compose.yml
-rw-r--r-- 1 root root 4736 Apr 14 15:13 docker-swarm.yml
-rw-r--r-- 1 root root 11294 Apr 14 15:13 LICENSE
drwxr-xr-x 2 root root 24 Apr 14 15:13 pd
-rw-r--r-- 1 root root 11335 Apr 14 15:13 README.md
drwxr-xr-x 2 root root 24 Apr 14 15:13 tidb
drwxr-xr-x 2 root root 24 Apr 14 15:13 tidb-binlog
drwxr-xr-x 2 root root 24 Apr 14 15:13 tidb-vision
drwxr-xr-x 2 root root 24 Apr 14 15:13 tikv
drwxr-xr-x 6 root root 95 Apr 14 15:13 tispark
drwxr-xr-x 2 root root 29 Apr 14 15:13 tools
[root@docker35 tidb-docker-compose]# du -sh .
3.3M .
[root@docker35 tidb-docker-compose]# docker-compose pull && docker-compose up -d
Pulling pd0 ... done
Pulling pd1 ... done
Pulling pd2 ... done
Pulling tikv0 ... done
Pulling tikv1 ... done
Pulling tikv2 ... done
Pulling tidb ... done
Pulling tispark-master ... done
Pulling tispark-slave0 ... done
Pulling tidb-vision ... done
Pulling pushgateway ... done
Pulling prometheus ... done
Pulling grafana ... done
Building with native build. Learn about native build in Compose here: https://docs.docker.com/go/compose-native-build/
Creating network "tidb-docker-compose_default" with the default driver
Creating tidb-docker-compose_prometheus_1 ... done
Creating tidb-docker-compose_grafana_1 ... done
Creating tidb-docker-compose_pd0_1 ... done
Creating tidb-docker-compose_pd2_1 ... done
Creating tidb-docker-compose_pd1_1 ... done
Creating tidb-docker-compose_tidb-vision_1 ... done
Creating tidb-docker-compose_pushgateway_1 ... done
Creating tidb-docker-compose_tikv2_1 ... done
Creating tidb-docker-compose_tikv0_1 ... done
Creating tidb-docker-compose_tikv1_1 ... done
Creating tidb-docker-compose_tidb_1 ... done
Creating tidb-docker-compose_tispark-master_1 ... done
Creating tidb-docker-compose_tispark-slave0_1 ... done
[root@docker35 tidb-docker-compose]# docker ps | grep tidb
0f0c9308b722 pingcap/tispark:latest "/opt/spark/sbin/sta…" 20 minutes ago Up 26 seconds 0.0.0.0:38081->38081/tcp tidb-docker-compose_tispark-slave0_1
b0cfd6181ced pingcap/tispark:latest "/opt/spark/sbin/sta…" 20 minutes ago Up 26 seconds 0.0.0.0:7077->7077/tcp, 0.0.0.0:8080->8080/tcp tidb-docker-compose_tispark-master_1
8d3eb1f375c4 pingcap/tidb:latest "/tidb-server --stor…" 20 minutes ago Up 25 seconds 0.0.0.0:4000->4000/tcp, 0.0.0.0:10080->10080/tcp tidb-docker-compose_tidb_1
15c524d6a77d pingcap/tikv:latest "/tikv-server --addr…" 20 minutes ago Up 2 seconds 20160/tcp tidb-docker-compose_tikv2_1
34e5e542e6af pingcap/tikv:latest "/tikv-server --addr…" 20 minutes ago Up 1 second 20160/tcp tidb-docker-compose_tikv1_1
cf16e9611c14 pingcap/tikv:latest "/tikv-server --addr…" 20 minutes ago Up 32 seconds 20160/tcp tidb-docker-compose_tikv0_1
560daefe9121 pingcap/tidb-vision:latest "/bin/sh -c 'sed -i …" 20 minutes ago Up 26 seconds 80/tcp, 443/tcp, 2015/tcp, 0.0.0.0:8010->8010/tcp tidb-docker-compose_tidb-vision_1
25ba4d9a30d7 prom/pushgateway:v0.3.1 "/bin/pushgateway --…" 20 minutes ago Up 35 seconds 9091/tcp tidb-docker-compose_pushgateway_1
cd1c88da452c pingcap/pd:latest "/pd-server --name=p…" 20 minutes ago Up 26 seconds 2380/tcp, 0.0.0.0:1029->2379/tcp tidb-docker-compose_pd0_1
ce363e945cdc prom/prometheus:v2.2.1 "/bin/prometheus --l…" 20 minutes ago Up 36 seconds 0.0.0.0:9090->9090/tcp tidb-docker-compose_prometheus_1
5768329d5789 pingcap/pd:latest "/pd-server --name=p…" 20 minutes ago Up 32 seconds 2380/tcp, 0.0.0.0:1027->2379/tcp tidb-docker-compose_pd2_1
f132ec8ae049 pingcap/pd:latest "/pd-server --name=p…" 20 minutes ago Up 32 seconds 2380/tcp, 0.0.0.0:1028->2379/tcp tidb-docker-compose_pd1_1
cc36a50a0d74 grafana/grafana:6.0.1 "/run.sh" 20 minutes ago Up 33 seconds 0.0.0.0:3000->3000/tcp tidb-docker-compose_grafana_1
[root@docker35 tidb-docker-compose]# docker ps | grep tidb | wc -l
13
二、访问集群
2.1、访问TiDB
[root@docker35 tidb-docker-compose]# mysql -h 127.0.0.1 -P 4000 -u root
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.25-TiDB-v5.0.0 TiDB Server (Apache License 2.0) Community Edition, MySQL 5.7 compatible
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| INFORMATION_SCHEMA |
| METRICS_SCHEMA |
| PERFORMANCE_SCHEMA |
| mysql |
| test |
+--------------------+
5 rows in set (0.00 sec)
mysql> create database lhrdb charset utf8mb4;
Query OK, 0 rows affected (0.07 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| INFORMATION_SCHEMA |
| METRICS_SCHEMA |
| PERFORMANCE_SCHEMA |
| lhrdb |
| mysql |
| test |
+--------------------+
6 rows in set (0.00 sec)
2.2、访问集群 Grafana 监控
访问集群 Grafana 监控页面:http://localhost:3000 默认用户名和密码均为 admin。如下内容都可以监控到:
例如:
2.3、集群数据可视化
集群数据可视化(https://github.com/pingcap/tidb-vision):http://localhost:8010
如果启用了 tidb-vision,可以通过 http://localhost:8010 查看。
2.4、访问Spark Web UI
三、自定义集群
在完成快速部署后,以下组件已默认部署:3 个 PD,3 个 TiKV,1 个 TiDB 和监控组件 Prometheus,Pushgateway,Grafana 以及 tidb-vision。
如果想自定义集群,可以直接修改 docker-compose.yml
,但是手动修改比较繁琐而且容易出错,强烈建议使用 Helm 模板引擎生成 docker-compose.yml
文件。
-
安装 Helm
Helm 可以用作模板渲染引擎,只需要下载其 binary 文件即可以使用。
curl https://raw.githubusercontent.com/kubernetes/helm/master/scripts/get | bash
如果是 Mac 系统,也可以通过 Homebrew 安装:
brew install kubernetes-helm
-
下载
tidb-docker-compose
git clone https://github.com/pingcap/tidb-docker-compose.git
-
自定义集群
cd tidb-docker-compose && cp compose/values.yaml values.yaml && vim values.yaml
修改
values.yaml
里面的配置,例如集群规模,TiDB 镜像版本等。tidb-vision 是 TiDB 集群可视化页面,可以可视化地显示 PD 对 TiKV 数据的调度。如果不想部署该组件,可以将
tidbVision
项留空。PD,TiKV,TiDB 和 tidb-vision 支持从 GitHub 源码或本地文件构建 Docker 镜像,供开发测试使用。
- 如果希望从本地已编译好的 binary 文件构建 PD,TiKV 或 TiDB 镜像,需要将其
image
字段留空,并将已编译好的 binary 拷贝到对应的pd/bin/pd-server
,tikv/bin/tikv-server
,tidb/bin/tidb-server
。 - 如果希望从本地构建 tidb-vision 镜像,需要将其
image
字段留空,并将 tidb-vision 项目拷贝到tidb-vision/tidb-vision
。
- 如果希望从本地已编译好的 binary 文件构建 PD,TiKV 或 TiDB 镜像,需要将其
-
生成
docker-compose.yml
文件helm template -f values.yaml compose > generated-docker-compose.yml
-
使用生成的
docker-compose.yml
创建并启动集群获取最新 Docker 镜像:
docker-compose -f generated-docker-compose.yml pull docker-compose -f generated-docker-compose.yml up -d
-
访问集群
mysql -h 127.0.0.1 -P 4000 -u root
四、访问 Spark shell 并加载 TiSpark
向 TiDB 集群中插入一些样本数据:
docker-compose exec tispark-master bash &&
cd /opt/spark/data/tispark-sample-data &&
mysql -h tidb -P 4000 -u root < dss.ddl
当样本数据加载到 TiDB 集群之后,可以使用 docker-compose exec tispark-master /opt/spark/bin/spark-shell
来访问 Spark shell。
docker-compose exec tispark-master /opt/spark/bin/spark-shell
...
Spark context available as 'sc' (master = local[*], app id = local-1527045927617).
Spark session available as 'spark'.
Welcome to
____ __
/ __/__ ___ _____/ /__
_\ \/ _ \/ _ `/ __/ '_/
/___/ .__/\_,_/_/ /_/\_\ version 2.1.1
/_/
Using Scala version 2.11.8 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_172)
Type in expressions to have them evaluated.
Type :help for more information.
scala> import org.apache.spark.sql.TiContext
...
scala> val ti = new TiContext(spark)
...
scala> ti.tidbMapDatabase("TPCH_001")
...
scala> spark.sql("select count(*) from lineitem").show
+--------+
|count(1)|
+--------+
| 60175|
+--------+
你也可以通过 Python 或 R 来访问 Spark:
docker-compose exec tispark-master /opt/spark/bin/pyspark &&
docker-compose exec tispark-master /opt/spark/bin/sparkR
更多关于 TiSpark 的信息,参见 TiSpark 的详细文档,连接:https://docs.pingcap.com/zh/tidb/v3.0/get-started-with-tispark。
- 点赞
- 收藏
- 关注作者
评论(0)