1.2.2 TiDB-Operator 部署本地测试环境

1.2.2.1 背景介绍

本小结介绍如何在个人电脑(Linux 或 MacOS)上采用 kind 方式在 Kubernetes 上部署 TiDB Operator 和 TiDB 集群。部署包含三个关键环节:

  1. 通过 kind 部署 K8s 集群
  2. 在 K8s 集群上部署 TiDB Operator
  3. 在 K8s 集群中部署 TiDB 集群

在部署前,请确认资源满足以下要求:

  • 内存 4GB+、CPU 至少 2 cores
  • Docker 17.03+
  • Go 1.10+
  • net.ipv4.ip_forward 设置为 1

1.2.2.2 通过 kind 部署 K8s 集群

  1. 下载自动化部署程序
  1. # cd /root & git clone --depth=1 https://github.com/pingcap/tidb-operator && cd tidb-operator
  1. 通过程序创建集群
  1. # cd /root/tidb-operator && hack/kind-cluster-build.sh

执行成功后会有如下关键提示信息:

  1. ########## success create cluster:[kind] ##########
  2. To start using your cluster, run:
  3. kubectl config use-context kind-kind
  1. 将 K8s 集群相关命令路径加入 PATH 路径
  1. # export PATH=$PATH:/root/tidb-operator/output/bin/
  1. 验证 K8s 环境是否符合要求
  1. # kubectl cluster-info
  2. Kubernetes master is running at https://127.0.0.1:32771
  3. KubeDNS is running at https://127.0.0.1:32771/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy

输出以上信息,则说明 K8s 服务符合要求。

  1. # helm version
  2. Client: &version.Version{SemVer:"v2.9.1", GitCommit:"20adb27c7c5868466912eebdf6664e7390ebe710", GitTreeState:"clean"}
  3. Server: &version.Version{SemVer:"v2.9.1", GitCommit:"20adb27c7c5868466912eebdf6664e7390ebe710", GitTreeState:"clean"}

输出以上信息,则说明 Helm 客户端与服务端都符合要求。

1.2.2.3 在 K8s 集群上部署 TiDB Operator

  1. 通过 helm 安装 TiDB Operator

创建 TiDB CRD

  1. # kubectl apply -f https://raw.githubusercontent.com/pingcap/tidb-operator/master/manifests/crd.yaml && kubectl get crd tidbclusters.pingcap.com
  2. customresourcedefinition.apiextensions.k8s.io/tidbclusters.pingcap.com unchanged
  3. customresourcedefinition.apiextensions.k8s.io/backups.pingcap.com unchanged
  4. customresourcedefinition.apiextensions.k8s.io/restores.pingcap.com unchanged
  5. customresourcedefinition.apiextensions.k8s.io/backupschedules.pingcap.com unchanged
  6. customresourcedefinition.apiextensions.k8s.io/tidbmonitors.pingcap.com unchanged
  7. customresourcedefinition.apiextensions.k8s.io/tidbinitializers.pingcap.com unchanged
  8. customresourcedefinition.apiextensions.k8s.io/tidbclusterautoscalers.pingcap.com unchanged
  9. NAME CREATED AT
  10. tidbclusters.pingcap.com 2020-03-06T13:38:32Z

下载 TiDB Operator 的 Helm chart 文件:

  1. # mkdir -p /root/chart/
  2. https://github.com/pingcap/tidb-operator/releases 下载 tidb-operator-chart-v1.0.6.tgz 文件放到 /root/chart/ 路径下
  3. # cd /root/chart/ && tar xvf tidb-operator-chart-v1.0.6.tgz

将 /root/tidb-operator/charts/tidb-operator/values.yaml 文件内的 scheduler.kubeSchedulerImageName 值修改为 registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler 以加快镜像拉取速度。

  1. 安装 TiDB Operator
  1. # helm install --namespace=tidb-admin --name=tidb-operator /root/tidb-operator/charts/tidb-operator -f /root/tidb-operator/charts/tidb-operator/values.yaml
  2. NAME: tidb-operator
  3. LAST DEPLOYED: Fri Mar 6 14:24:09 2020
  4. NAMESPACE: tidb-admin
  5. STATUS: DEPLOYED
  6. ...
  1. 验证 Operator 运行状态
  1. # kubectl get pods -n tidb-admin
  2. NAME READY STATUS RESTARTS AGE
  3. tidb-controller-manager-85d8d498bf-2n8km 1/1 Running 0 19s
  4. tidb-scheduler-7c67d6c77b-qd54r 2/2 Running 0 19s

以上信息显示 Operator 运行正常。

1.2.2.4 在 K8s 集群中部署 TiDB 集群

  1. 下载 TiDB Cluster 的 helm chart 文件
  1. # mkdir -p /root/chart/
  2. https://github.com/pingcap/tidb-operator/releases 下载 tidb-cluster-chart-v1.0.6.tgz 文件放到 /root/chart/ 路径下
  1. 安装 TiDB Cluster
  1. # cd /root/chart/ && tar xvf tidb-cluster-chart-v1.0.6.tgz
  2. # helm install --namespace dba-test --name=test /root/tidb-operator/charts/tidb-cluster -f /root/tidb-operator/charts/tidb-cluster/values.yaml
  3. NAME: test
  4. LAST DEPLOYED: Fri Mar 6 14:50:25 2020
  5. NAMESPACE: dba-test
  6. STATUS: DEPLOYED

以上信息显示 TiDB Cluster 部署正常

  1. 观察 TiDB 的 POD 状态
  1. # kubectl get pods -n dba-test
  2. NAME READY STATUS RESTARTS AGE
  3. test-discovery-668b48577c-lqqbz 1/1 Running 0 7m37s
  4. test-monitor-5b586d8cb-227qx 3/3 Running 0 7m37s
  5. test-pd-0 1/1 Running 0 7m37s
  6. test-pd-1 1/1 Running 0 7m37s
  7. test-pd-2 1/1 Running 1 7m37s
  8. test-tidb-0 2/2 Running 0 6m18s
  9. test-tidb-1 2/2 Running 0 6m18s
  10. test-tikv-0 1/1 Running 0 6m58s
  11. test-tikv-1 1/1 Running 0 6m58s
  12. test-tikv-2 1/1 Running 0 6m58s

以上信息显示 TiDB Cluster 所有 Pod 全部运行正常。

  1. 访问 TiDB 集群
  1. # nohup kubectl port-forward svc/test-tidb 4000:4000 --namespace=dba-test &
  2. # yum install -y mysql
  3. # mysql -h 127.0.0.1 -uroot -P4000
  4. mysql -h 127.0.0.1 -P 4000 -uroot
  5. Welcome to the MariaDB monitor. Commands end with ; or \g.
  6. Your MySQL connection id is 1
  7. Server version: 5.7.25-TiDB-v3.0.5 MySQL Community Server (Apache License 2.0)
  8. Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
  9. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  10. MySQL [(none)]>

显示以上输出显示 TiDB 集群部署成功。