1.2.3.2.3 在 Kubernetes 上部署 TiDB Operator

本文介绍如何在 Kubernetes 上部署 TiDB Operator。

1. 准备环境

TiDB Operator 部署前,请确认以下软件需求:

  • Kubernetes v1.12 或者更高版本
  • Helm 版本 >= v2.11.0 && < v3.0.0

注意:

  • 尽管 TiDB Operator 可以使用网络卷持久化 TiDB 数据,TiDB 数据自身会存多副本,再走额外的网络卷性能会受到很大影响。强烈建议搭建本地卷以提高性能。
  • 跨多可用区的网络卷需要 Kubernetes v1.12 或者更高版本。

安装 Helm 服务端

在集群中应用 helm 服务端组件 tiller 所需的 RBAC 规则并安装 tiller

kubectl apply -f https://raw.githubusercontent.com/pingcap/tidb-operator/master/manifeststiller-rbac.yaml && \
helm init --service-account=tiller --upgrade

通过下面命令确认 tiller Pod 进入 running 状态:

kubectl get po -n kube-system -l name=tiller

如果 Kubernetes 集群没有启用 RBAC,那么可以直接使用下列命令安装 tiller

helm init --upgrade

Kubernetes 应用在 Helm 中被打包为 chart。PingCAP 维护的 helm chart 仓库是 https://charts.pingcap.org/,可以通过下面的命令添加该仓库:

helm repo add pingcap https://charts.pingcap.org/

添加完成后,可以使用 helm search 搜索 PingCAP 提供的 chart:

helm search pingcap -l
NAME                    CHART VERSION   APP VERSION DESCRIPTION
pingcap/tidb-backup     v1.0.0                      A Helm chart for TiDB Backup or Restore
pingcap/tidb-cluster    v1.0.0                      A Helm chart for TiDB Cluster
pingcap/tidb-operator   v1.0.0                      tidb-operator Helm chart for Kubernetes

当新版本的 chart 发布后,可以使用 helm repo update 命令更新本地对于仓库的缓存:

helm repo update

2. 安装 TiDB Operator

TiDB Operator 使用 CRD (Custom Resource Definition) 扩展 Kubernetes,所以要使用 TiDB Operator,必须先创建 TidbCluster 自定义资源类型。只需要在你的 Kubernetes 集群上创建一次即可:

kubectl apply -f https://raw.githubusercontent.com/pingcap/tidb-operator/master/manifests/crd.yaml && \
kubectl get crd tidbclusters.pingcap.com

创建 TidbCluster 自定义资源类型后,接下来在 Kubernetes 集群上安装 TiDB Operator。

(1) 获取你要安装的 tidb-operator chart 中的 values.yaml 文件:

```shell
mkdir -p /home/tidb/tidb-operator && \
helm inspect values pingcap/tidb-operator --version=<chart-version> > /home/tidb/tidb-operator/values-tidb-operator.yaml
```

> **注意:**
>
> `<chart-version>` 在后续文档中代表 chart 版本,例如 `v1.0.0`,可以通过 `helm search -l tidb-operator` 查看当前支持的版本。

(2) 配置 TiDB Operator

TiDB Operator 里面会用到 `k8s.gcr.io/kube-scheduler` 镜像,如果下载不了该镜像,可以修改 `/home/tidb/tidb-operator/values-tidb-operator.yaml` 文件中的 `scheduler.kubeSchedulerImageName` 为 `registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler`。

(3) 安装 TiDB Operator

```shell
helm install pingcap/tidb-operator --name=tidb-operator --namespace=tidb-admin --version=<chart-version> -f /home/tidb/tidb-operator/values-tidb-operator.yaml && \
kubectl get po -n tidb-admin -l app.kubernetes.io/name=tidb-operator
```

3. 自定义 TiDB Operator

通过修改 /home/tidb/tidb-operator/values-tidb-operator.yaml 中的配置自定义 TiDB Operator。后续文档使用 values.yaml 指代 /home/tidb/tidb-operator/values-tidb-operator.yaml

TiDB Operator 有两个组件:

  • tidb-controller-manager
  • tidb-scheduler

这两个组件是无状态的,通过 Deployment 部署。你可以在 values.yaml 中自定义资源 limitrequestreplicas

修改为 values.yaml 后,执行下面命令使配置生效:

helm upgrade tidb-operator pingcap/tidb-operator --version=<chart-version> -f /home/tidb/tidb-operator/values-tidb-operator.yaml

4. 手动下载 chart 安装

如果安装环境无法访问 PingCAP chart 仓库,可以通过 wget http://charts.pingcap.org/tidb-cluster-<chart-version>.tgz 下载指定版本 chart,进行安装。

假设下载的 chart 放在 /home/tidb/ 目录,版本为 v1.0.6:

cd /home/tidb/ && tar -xvf tidb-operator-chart-v1.0.6.tgz

修改 /home/tidb/tidb-operator/values.yaml 文件,安装 TiDB Operator:

helm install /home/tidb/tidb-operator --namespace=tidb-admin --name=tidb-operator -f /home/tidb/tidb-operator/values.yaml

可以通过下面命令验证 TiDB Operator 运行状态:

kubectl get pods -n tidb-admin
NAME                                       READY   STATUS    RESTARTS   AGE
tidb-controller-manager-85d8d498bf-2n8km   1/1     Running   0          19s
tidb-scheduler-7c67d6c77b-qd54r            2/2     Running   0          19s

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

results matching ""

    No results matching ""