Command Cheat Sheet for TiDB Cluster Management
This document is an overview of the commands used for TiDB cluster management.
kubectl
View resources
View CRD:
kubectl get crdView TidbCluster:
kubectl -n ${namespace} get tc ${name}View TidbMonitor:
kubectl -n ${namespace} get tidbmonitor ${name}View Backup:
kubectl -n ${namespace} get bk ${name}View BackupSchedule:
kubectl -n ${namespace} get bks ${name}View Restore:
kubectl -n ${namespace} get restore ${name}View TidbClusterAutoScaler:
kubectl -n ${namespace} get tidbclusterautoscaler ${name}View TidbInitializer:
kubectl -n ${namespace} get tidbinitializer ${name}View Advanced StatefulSet:
kubectl -n ${namespace} get asts ${name}View a Pod:
kubectl -n ${namespace} get pod ${name}View a TiKV Pod:
kubectl -n ${namespace} get pod -l app.kubernetes.io/component=tikvView the continuous status change of a Pod:
watch kubectl -n ${namespace} get podView the detailed information of a Pod:
kubectl -n ${namespace} describe pod ${name}View the node on which Pods are located:
kubectl -n ${namespace} get pods -l "app.kubernetes.io/component=tidb,app.kubernetes.io/instance=${cluster_name}" -ojsonpath="{range .items[*]}{.spec.nodeName}{'\n'}{end}"View Service:
kubectl -n ${namespace} get service ${name}View ConfigMap:
kubectl -n ${namespace} get cm ${name}View a PersistentVolume (PV):
kubectl -n ${namespace} get pv ${name}View the PV used by the cluster:
kubectl get pv -l app.kubernetes.io/namespace=${namespace},app.kubernetes.io/managed-by=tidb-operator,app.kubernetes.io/instance=${cluster_name}View a PersistentVolumeClaim (PVC):
kubectl -n ${namespace} get pvc ${name}View StorageClass:
kubectl -n ${namespace} get scView StatefulSet:
kubectl -n ${namespace} get sts ${name}View the detailed information of StatefulSet:
kubectl -n ${namespace} describe sts ${name}
Update resources
Add an annotation for TiDBCluster:
kubectl -n ${namespace} annotate tc ${cluster_name} ${key}=${value}Add a force-upgrade annotation for TiDBCluster:
kubectl -n ${namespace} annotate --overwrite tc ${cluster_name} tidb.pingcap.com/force-upgrade=trueDelete a force-upgrade annotation for TiDBCluster:
kubectl -n ${namespace} annotate tc ${cluster_name} tidb.pingcap.com/force-upgrade-Enable the debug mode for Pods:
kubectl -n ${namespace} annotate pod ${pod_name} runmode=debug
Edit resources
Edit TidbCluster:
kubectl -n ${namespace} edit tc ${name}
Patch Resources
Patch TidbCluster:
kubectl -n ${namespace} patch tc ${name} --type merge -p '${json_path}'Patch PV ReclaimPolicy:
kubectl patch pv ${name} -p '{"spec":{"persistentVolumeReclaimPolicy":"Delete"}}'Patch a PVC:
kubectl -n ${namespace} patch pvc ${name} -p '{"spec": {"resources": {"requests": {"storage": "100Gi"}}}'Patch StorageClass:
kubectl patch storageclass ${name} -p '{"allowVolumeExpansion": true}'
Create resources
Create a cluster using the YAML file:
kubectl -n ${namespace} apply -f ${file}Create Namespace:
kubectl create ns ${namespace}Create Secret:
Create Secret of the certificate:
kubectl -n ${namespace} create secret generic ${secret_name} --from-file=tls.crt=${cert_path} --from-file=tls.key=${key_path} --from-file=ca.crt=${ca_path}Create Secret of the user id and password:
kubectl -n ${namespace} create secret generic ${secret_name} --from-literal=user=${user} --from-literal=password=${password}
Interact with running Pods
View the PD configuration file:
kubectl -n ${namespace} -it exec ${pod_name} -- cat /etc/pd/pd.tomlView the TiDB configuration file:
kubectl -n ${namespace} -it exec ${pod_name} -- cat /etc/tidb/tidb.tomlView the TiKV configuration file:
kubectl -n ${namespace} -it exec ${pod_name} -- cat /etc/tikv/tikv.tomlView Pod logs:
kubectl -n ${namespace} logs ${pod_name} -fView logs of the previous container:
kubectl -n ${namespace} logs ${pod_name} -pIf there are multiple containers in a Pod, view logs of one container:
kubectl -n ${namespace} logs ${pod_name} -c ${container_name}Expose services:
kubectl -n ${namespace} port-forward svc/${service_name} ${local_port}:${port_in_pod}Expose PD services:
kubectl -n ${namespace} port-forward svc/${cluster_name}-pd 2379:2379
Interact with nodes
Mark the node as non-schedulable:
kubectl cordon ${node_name}Mark the node as schedulable:
kubectl uncordon ${node_name}
Delete resources
Delete a Pod:
kubectl delete -n ${namespace} pod ${pod_name}Delete a PVC:
kubectl delete -n ${namespace} pvc ${pvc_name}Delete TidbCluster:
kubectl delete -n ${namespace} tc ${tc_name}Delete TidbMonitor:
kubectl delete -n ${namespace} tidbmonitor ${tidb_monitor_name}Delete TidbClusterAutoScaler:
kubectl -n ${namespace} delete tidbclusterautoscaler ${name}
More
See kubectl Cheat Sheet for more kubectl usage.
Helm
Add Helm repository
helm repo add pingcap https://charts.pingcap.org/
Update Helm repository
helm repo update
View available Helm chart
View charts in Helm Hub:
helm search hub ${chart_name}For example:
helm search hub mysqlView charts in other repositories:
helm search repo ${chart_name} -l --develFor example:
helm search repo tidb-operator -l --devel
Get the default values.yaml
of the Helm chart
helm inspect values ${chart_name} --version=${chart_version} > values.yaml
For example:
helm inspect values pingcap/tidb-operator --version=v1.4.7 > values-tidb-operator.yaml
Deploy using Helm chart
helm install ${name} ${chart_name} --namespace=${namespace} --version=${chart_version} -f ${values_file}
For example:
helm install tidb-operator pingcap/tidb-operator --namespace=tidb-admin --version=v1.4.7 -f values-tidb-operator.yaml
View the deployed Helm release
helm ls
Update Helm release
helm upgrade ${name} ${chart_name} --version=${chart_version} -f ${values_file}
For example:
helm upgrade tidb-operator pingcap/tidb-operator --version=v1.4.7 -f values-tidb-operator.yaml
Delete Helm release
helm uninstall ${name} -n ${namespace}
For example:
helm uninstall tidb-operator -n tidb-admin
More
See Helm Commands for more Helm usage.