Advanced StatefulSet Controller
Kubernetes has a built-inStatefulSetthat allocates consecutive serial numbers to Pods. For example, when there are three replicas, the Pods are named as pod-0, pod-1, and pod-2. When scaling out or scaling in, you must add a Pod at the end or delete the last pod. For example, when you scale out to four replicas, pod-3 is added. When you scale in to two replicas, pod-2 is deleted.
When you use local storage, Pods are associated with the Nodes storage resources and cannot be scheduled freely. If you want to delete one of the Pods in the middle to maintain its Node but no other Nodes can be migrated, or if you want to delete a Pod that fails and to create another Pod with a different serial number, you cannot implement such desired function by the built-in StatefulSet.
Theadvanced StatefulSet controlleris implemented based on the built-in StatefulSet controller. It supports freely controlling the serial number of Pods. This document describes how to use the advanced StatefulSet controller in TiDB Operator.
Enable
Load the Advanced StatefulSet CRD file:
For Kubernetes versions < 1.16:
kubectl apply -f https://raw.githubusercontent.com/pingcap/tidb-operator/v1.5.0/manifests/advanced-statefulset-crd.v1beta1.yamlFor Kubernetes versions >= 1.16:
kubectl apply -f https://raw.githubusercontent.com/pingcap/tidb-operator/v1.5.0/manifests/advanced-statefulset-crd.v1.yaml
Enable the
AdvancedStatefulSet
feature invalues.yaml
of the TiDB Operator chart:features: - AdvancedStatefulSet=true advancedStatefulset: create: trueUpgrade TiDB Operator. For details, refer toUpgrade TiDB Operator.
After upgrading TiDB Operator, check the AdvancedStatefulSet Controller is deployed by the following command:
kubectl get pods -n${operator-ns}--selector app.kubernetes.io/component=advanced-statefulset-controllerExpected output
名字就绪状态RESTARTS AGE advanced-statefulset-controller-67885c5dd9-f522h 1/1 Running 0 10s
Usage
This section describes how to use the advanced StatefulSet controller.
View theAdvancedStatefulSet
Object by kubectl
The data format ofAdvancedStatefulSet
is the same as that ofStatefulSet
, butAdvancedStatefulSet
is implemented based on CRD, withasts
as the alias. You can view theAdvancedStatefulSet
object in the namespace by running the following command:
kubectl get -n${namespace}asts
Specify the Pod to be scaled in
With the advanced StatefulSet controller, when scaling in TidbCluster, you can not only reduce the number of replicas, but also specify the scaling in of any Pod in the PD, TiDB, or TiKV components by configuring annotations.
对于穰mple:
apiVersion:
m.rzhenli.com/v1alpha1
kind:
TidbCluster
metadata:
name:
asts
spec:
版本:
v7.1.0
timezone:
UTC
pvReclaimPolicy:
删除
pd:
baseImage:
pingcap/pd
maxFailoverCount:
0
replicas:
3
requests:
storage:
"1Gi"
config:{}tikv:
baseImage:
pingcap/tikv
maxFailoverCount:
0
replicas:
4
requests:
storage:
"1Gi"
config:{}tidb:
baseImage:
pingcap/tidb
maxFailoverCount:
0
replicas:
2
service:
type:
ClusterIP
config:{}
The above configuration deploys 4 TiKV instances, namelybasic-tikv-0
,basic-tikv-1
, ...,basic-tikv-3
. If you want to deletebasic-tikv-1
, setspec.tikv.replicas
to3
和configure the following annotations:
metadata:
annotations:
tikv.tidb.m.rzhenli.com/delete-slots:
'[1]'
The complete example is as follows:
apiVersion:
m.rzhenli.com/v1alpha1
kind:
TidbCluster
metadata:
annotations:
tikv.tidb.m.rzhenli.com/delete-slots:
'[1]'
name:
asts
spec:
版本:
v7.1.0
timezone:
UTC
pvReclaimPolicy:
删除
pd:
baseImage:
pingcap/pd
maxFailoverCount:
0
replicas:
3
requests:
storage:
"1Gi"
config:{}tikv:
baseImage:
pingcap/tikv
maxFailoverCount:
0
replicas:
3
requests:
storage:
"1Gi"
config:{}tidb:
baseImage:
pingcap/tidb
maxFailoverCount:
0
replicas:
2
service:
type:
ClusterIP
config:{}
The supported annotations are as follows:
pd.tidb.m.rzhenli.com/delete-slots
: Specifies the serial numbers of the Pods to be deleted in the PD component.tidb.tidb.m.rzhenli.com/delete-slots
: Specifies the serial number of the Pods to be deleted in the TiDB component.tikv.tidb.m.rzhenli.com/delete-slots
: Specifies the serial number of the Pods to be deleted in the TiKV component.
The value of Annotation is an integer array of JSON, such as[0]
,[0,1]
,(1、3)
.
Specify the location to scale out
You can reverse the above operation of scaling in to restorebasic-tikv-1
.
对于穰mple:
apiVersion:
m.rzhenli.com/v1alpha1
kind:
TidbCluster
metadata:
annotations:
tikv.tidb.m.rzhenli.com/delete-slots:
'[]'
name:
asts
spec:
版本:
v7.1.0
timezone:
UTC
pvReclaimPolicy:
删除
pd:
baseImage:
pingcap/pd
maxFailoverCount:
0
replicas:
3
requests:
storage:
"1Gi"
config:{}tikv:
baseImage:
pingcap/tikv
maxFailoverCount:
0
replicas:
4
requests:
storage:
"1Gi"
config:{}tidb:
baseImage:
pingcap/tidb
maxFailoverCount:
0
replicas:
2
service:
type:
ClusterIP
config:{}
The delete-slots annotations can be left empty or deleted completely.