Skip to content

Commit

Permalink
zh: update auto-scaling doc for master branch (#1049)
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielZhangQD authored Jan 15, 2021
1 parent f4cd26e commit b45f053
Showing 1 changed file with 73 additions and 157 deletions.
230 changes: 73 additions & 157 deletions zh/enable-tidb-cluster-auto-scaling.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,44 +6,36 @@ aliases: ['/docs-cn/tidb-in-kubernetes/dev/enable-tidb-cluster-auto-scaling/']

# 启用 TidbCluster 弹性伸缩

在 Kubernetes 平台上,有着基于 CPU 利用率进行负载的原生 API: [Horizontal Pod Autoscaler](https://kubernetes.io/zh/docs/tasks/run-application/horizontal-pod-autoscale/)。基于 Kubernetes,TiDB 4.0 起支持了全新的弹性调度算法。与之相应的,在 TiDB Operator 1.1 及以上版本中,TiDB 集群可以凭借 Kubernetes 平台本身的特性来开启弹性调度的能力。本篇文章将会介绍如何开启并使用 TidbCluster 的弹性伸缩能力。
在 Kubernetes 平台上,有原生的弹性伸缩 API: [Horizontal Pod Autoscaler](https://kubernetes.io/zh/docs/tasks/run-application/horizontal-pod-autoscale/)。基于 Kubernetes,TiDB 5.0 起支持了全新的弹性调度算法。与之相应的,在 TiDB Operator 1.2 及以上版本中,TiDB 集群可以凭借 Kubernetes 平台本身的特性来开启弹性调度的能力。本篇文章将会介绍如何开启并使用 TidbCluster 的弹性伸缩能力。

## 开启弹性伸缩特性

> **警告:**
>
> * TidbCluster 弹性伸缩目前仍处于 Alpha 阶段,我们极其不推荐在关键、生产环境开启这个特性
> * 我们推荐你在测试、内网环境对这个特性进行体验,并反馈相关的建议与问题给我们,帮助我们更好地提高这一特性能力。
> * TidbCluster 弹性伸缩目前仍处于 Alpha 阶段,我们极其不推荐在关键、生产环境开启这个特性。
> * 我们推荐你在测试环境对这个特性进行体验,并反馈相关的建议与问题给我们,帮助我们更好地提高这一特性能力。
> * 目前仅支持基于 CPU 利用率的弹性伸缩。
开启弹性伸缩特性需要主动开启 Operator 相关配置,默认情况下 Operator 的弹性伸缩特性是关闭的。你可以通过以下方式来开启弹性调度特性:
开启弹性伸缩特性需要主动开启 TiDB Operator 相关配置,默认情况下 TiDB Operator 的弹性伸缩特性是关闭的。

1. 修改 Operator 的 `values.yaml`
你可以通过以下方式来开启弹性调度特性:

1. 修改 TiDB Operator 的 `values.yaml`

`features` 选项中开启 AutoScaling:

```yaml
features:
- AutoScaling=true
```
开启 Operator Webhook 特性:
```yaml
admissionWebhook:
create: true
mutation:
pods: true
- AutoScaling=true
```
关于 Operator Webhook,请参考[开启 TiDB Operator 准入控制器](enable-admission-webhook.md)
2. 安装/更新 TiDB Operator
修改完 `values.yaml` 文件中上述配置项以后进行 TiDB Operator 部署或者更新。安装与更新 Operator 请参考[在 Kubernetes 上部署 TiDB Operator](deploy-tidb-operator.md)。

3. 确认目标 TiDB 集群资源设置

目标 TiDB 集群在使用弹性伸缩前,首先需要设置好对应组件的 CPU 设置。以 TiKV 为例,你需要申明 `spec.tikv.requests.cpu`:
目标 TiDB 集群在使用弹性伸缩前,首先需要设置好对应组件的 CPU 设置。以 TiKV 为例,你需要配置 `spec.tikv.requests.cpu`:

```yaml
spec:
Expand All @@ -57,7 +49,9 @@ aliases: ['/docs-cn/tidb-in-kubernetes/dev/enable-tidb-cluster-auto-scaling/']

## 了解 TidbClusterAutoScaler

我们通过 `TidbClusterAutoScaler` CR 对象来控制 TiDB 集群的弹性伸缩行为,如果你曾经使用过 [Horizontal Pod Autoscaler](https://kubernetes.io/zh/docs/tasks/run-application/horizontal-pod-autoscale/),那么你一定会对这个概念感到非常熟悉。以下是一个 TiKV 的弹性伸缩例子。
你可以通过 `TidbClusterAutoScaler` CR 对象来控制 TiDB 集群的弹性伸缩行为。

以下是一个例子:

```yaml
apiVersion: pingcap.com/v1alpha1
Expand All @@ -67,42 +61,67 @@ metadata:
spec:
cluster:
name: auto-scaling-demo
namespace: default
monitor:
name: auto-scaling-demo
namespace: default
tikv:
minReplicas: 3
maxReplicas: 4
metrics:
- type: "Resource"
resource:
name: "cpu"
target:
type: "Utilization"
averageUtilization: 80
resources:
storage_small:
cpu: 1000m
memory: 2Gi
storage: 10Gi
count: 3
rules:
cpu:
max_threshold: 0.8
min_threshold: 0.2
resource_types:
- storage_small
scaleInIntervalSeconds: 500
scaleOutIntervalSeconds: 300
tidb:
resources:
compute_small:
cpu: 1000m
memory: 2Gi
count: 3
rules:
cpu:
max_threshold: 0.8
min_threshold: 0.2
resource_types:
- compute_small
```

对于 TiDB 组件,你可以通过 `spec.tidb` 来进行配置,目前 TiKV 与 TiDB 的弹性伸缩 API 相同。
### 原理介绍

在 `TidbClusterAutoScaler` 对象中,`cluster` 属性代表了需要被弹性调度的 TiDB 集群,通过 name 与 namespace 来标识。由于 `TidbClusterAutoScaler` 组件需要通过指标采集组件抓取相关资源使用情况,我们需要提供对应的指标采集与查询服务给 `TidbClusterAutoScaler`。`monitor` 属性则代表了与之相关连的 TidbMonitor 对象。如果你不了解 TidbMonitor,可以参考 [TiDB 集群监控与告警](monitor-a-tidb-cluster.md)。
TiDB Operator 会根据 TidbClusterAutoScaler CR 的配置,向 PD 发起请求,查询扩缩容结果,并根据 PD 返回的结果,利用[异构集群](deploy-heterogeneous-tidb-cluster.md)特性,创建、更新或者删除异构 TiDB 集群(只配置 TiDB 组件或者只配置 TiKV 组件),实现 TiDB 集群的弹性伸缩

对于非 `TidbMonitor` 的外部 `Prometheus`, 你可以通过 `spec.metricsUrl` 来填写这个服务的 Host,从而指定该 TiDB 集群的监控指标采集服务。对于使用 `Helm` 部署 TiDB 集群监控的情况,可以通过以下方式来指定 `spec.metricsUrl`。
### 字段介绍

```yaml
apiVersion: pingcap.com/v1alpha1
kind: TidbClusterAutoScaler
metadata:
name: auto-scaling-demo
spec:
cluster:
name: auto-scaling-demo
namespace: default
metricsUrl: "http://${release_name}-prometheus.${namespace}.svc:9090"
......
```
* `spec.cluster`:需要被弹性调度的 TiDB 集群。

* `name`:TiDB 集群名称。
* `namespace`:TiDB 集群所在 namespace,如果没有配置 namespace,会默认设置为和 TidbClusterAutoScaler CR 相同的 namespace。

## 例子
* `spec.tikv`:TiKV 弹性调度相关配置。
* `spec.tikv.resources`:TiKV 弹性调度可以选择的资源配置类型,如果没有配置,会默认设置为 `spec.cluster` 对应的 TidbCluster CR 中的 `spec.tikv.requests` 资源配置。

* `cpu`:CPU 配置。
* `memory`:内存配置。
* `storage`:存储配置。
* `count`:当前资源配置可以使用的数量,如果不配置,则认为没有限制。

* `spec.tikv.rules`:TiKV 弹性调度规则,目前只支持 CPU 规则。

* `max_threshold`:所有 Pod CPU 平均利用率超过 `max_threshold` 会触发扩容操作。
* `min_threshold`:所有 Pod CPU 平均利用率低于 `min_threshold` 会触发缩容操作。
* `resource_types`:配置根据 CPU 规则弹性伸缩时可以使用的资源类型,对应 `spec.tikv.resources[]` 中的 `key`,如果没有配置,默认会设置为 `spec.tikv.resources[]` 中所有的 `key`。

* `spec.tikv.scaleInIntervalSeconds`:缩容距离上一次伸缩(扩容或者缩容)的冷却时间,如果没有配置,默认设置为 `500`,即 `500s`。
* `spec.tikv.scaleOutIntervalSeconds`:扩容距离上一次伸缩(扩容或者缩容)的冷却时间,如果没有配置,默认设置为 `300`,即 `300s`。
* `spec.tidb`:TiDB 弹性调度相关配置,下面的各个字段和 `spec.tikv` 一样。

更多配置字段可以参考 [API 文档](https://github.com/pingcap/tidb-operator/blob/master/docs/api-references/docs.md#basicautoscalerspec)。

## 演示示例

1. 执行以下命令在 Kubernetes 集群上快速安装一个 1 PD、3 TiKV、2 TiDB,并带有监控与弹性伸缩能力的 TiDB 集群。

Expand All @@ -124,34 +143,12 @@ spec:
$ kubectl apply -f https://raw.githubusercontent.com/pingcap/tidb-operator/master/examples/auto-scale/tidb-cluster-auto-scaler.yaml -n ${namespace}
```

2. 当 TiDB 集群创建完毕以后,使用以下方式暴露 TiDB 集群服务到本地。

{{< copyable "shell-regular" >}}

```shell
kubectl port-forward svc/auto-scaling-demo-tidb 4000:4000 &
```

将以下内容复制到本地的 sysbench.config 文件中:

```config
mysql-host=127.0.0.1
mysql-port=4000
mysql-user=root
mysql-password=
mysql-db=test
time=120
threads=20
report-interval=5
db-driver=mysql
```

3. 使用 [sysbench](https://github.com/akopytov/sysbench) 工具准备数据并进行压测。
2. 使用 [sysbench](https://github.com/akopytov/sysbench) 工具准备数据。

将以下内容复制到本地的 sysbench.config 文件中:
将以下内容复制到 sysbench.config 文件中:

```config
mysql-host=127.0.0.1
mysql-host=${tidb_service_ip}
mysql-port=4000
mysql-user=root
mysql-password=
Expand All @@ -167,15 +164,15 @@ spec:
{{< copyable "shell-regular" >}}

```shell
sysbench --config-file=${path-to-file}/sysbench.config oltp_point_select --tables=1 --table-size=20000 prepare
sysbench --config-file=${path}/sysbench.config oltp_point_select --tables=1 --table-size=20000 prepare
```

通过以下命令开始进行压测:
3. 通过以下命令开始进行压测:

{{< copyable "shell-regular" >}}

```shell
sysbench --config-file=${path-to-file}/sysbench.config oltp_point_select --tables=1 --table-size=20000 run
sysbench --config-file=${path}/sysbench.config oltp_point_select --tables=1 --table-size=20000 run
```

上述命令执行完毕后,出现如下输出:
Expand Down Expand Up @@ -219,84 +216,3 @@ spec:
kubectl delete tidbmonitor auto-scaling-demo -n ${namespace}
kubectl delete tidbclusterautoscaler auto-scaling-demo -n ${namespace}
```

## 配置 TidbClusterAutoScaler

1. 设置弹性伸缩间隔

相比无状态的 Web 服务,一个分布式数据库软件对于实例的伸缩往往是非常敏感的。我们需要保证每次弹性伸缩之间存在一定的间隔,从而避免引起频繁的弹性伸缩。
你可以通过 `spec.tikv.scaleInIntervalSeconds` 和 `spec.tikv.ScaleOutIntervalSeconds` 来配置每两次弹性伸缩之间的时间间隔(秒),对于 TiDB 也同样如此。

```yaml
apiVersion: pingcap.com/v1alpha1
kind: TidbClusterAutoScaler
metadata:
name: auto-sclaer
spec:
tidb:
scaleInIntervalSeconds: 500
ScaleOutIntervalSeconds: 300
tikv:
scaleInIntervalSeconds: 500
ScaleOutIntervalSeconds: 300
```

2. 设置最大最小值

就像 [Horizontal Pod Autoscaler](https://kubernetes.io/zh/docs/tasks/run-application/horizontal-pod-autoscale/),在 `TidbClusterAutoScaler` 中你也可以设置给每个组件最大最小值来控制 `TiDB`、`TiKV` 的伸缩范围。

```yaml
apiVersion: pingcap.com/v1alpha1
kind: TidbClusterAutoScaler
metadata:
name: auto-scaling-demo
spec:
tikv:
minReplicas: 3
maxReplicas: 4
tidb:
minReplicas: 2
maxReplicas: 3
```

3. 配置 CPU 弹性伸缩

目前 `TidbClusterAutoScaler` 仅支持基于 CPU 负载的弹性伸缩,CPU 负载的描述性 API 如下所示。`averageUtilization` 则代表了 CPU 负载利用率的阈值。如果当前 CPU 利用率超过 80%,则会触发弹性扩容。

```yaml
apiVersion: pingcap.com/v1alpha1
kind: TidbClusterAutoScaler
metadata:
name: auto-scaling-demo
spec:
tikv:
minReplicas: 3
maxReplicas: 4
metrics:
- type: "Resource"
resource:
name: "cpu"
target:
type: "Utilization"
averageUtilization: 80
```

4. 配置指标时间窗口

目前基于 CPU 负载的弹性调度,`TidbClusterAutoScaler` 会在所指定的监控系统中获取 `TiDB`、`TiKV` 的 CPU 指标,你可以指定采集指标的时间窗口。

```yaml
apiVersion: pingcap.com/v1alpha1
kind: TidbClusterAutoScaler
metadata:
name: basic
tidb:
metricsTimeDuration: "1m"
metrics:
- type: "Resource"
resource:
name: "cpu"
target:
type: "Utilization"
averageUtilization: 60
```

0 comments on commit b45f053

Please sign in to comment.