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:

    1. kubectl get crd
  • View TidbCluster:

    1. kubectl -n ${namespace} get tc ${name}
  • View TidbMonitor:

    1. kubectl -n ${namespace} get tidbmonitor ${name}
  • View Backup:

    1. kubectl -n ${namespace} get bk ${name}
  • View BackupSchedule:

    1. kubectl -n ${namespace} get bks ${name}
  • View Restore:

    1. kubectl -n ${namespace} get restore ${name}
  • View TidbClusterAutoScaler:

    1. kubectl -n ${namespace} get tidbclusterautoscaler ${name}
  • View TidbInitializer:

    1. kubectl -n ${namespace} get tidbinitializer ${name}
  • View Advanced StatefulSet:

    1. kubectl -n ${namespace} get asts ${name}
  • View a Pod:

    1. kubectl -n ${namespace} get pod ${name}

    View a TiKV Pod:

    1. kubectl -n ${namespace} get pod -l app.kubernetes.io/component=tikv

    View the continuous status change of a Pod:

    1. watch kubectl -n ${namespace} get pod

    View the detailed information of a Pod:

    1. kubectl -n ${namespace} describe pod ${name}
  • View the node on which Pods are located:

    1. 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:

    1. kubectl -n ${namespace} get service ${name}
  • View ConfigMap:

    1. kubectl -n ${namespace} get cm ${name}
  • View a PersistentVolume (PV):

    1. kubectl -n ${namespace} get pv ${name}

    View the PV used by the cluster:

    1. 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):

    1. kubectl -n ${namespace} get pvc ${name}
  • View StorageClass:

    1. kubectl -n ${namespace} get sc
  • View StatefulSet:

    1. kubectl -n ${namespace} get sts ${name}

    View the detailed information of StatefulSet:

    1. kubectl -n ${namespace} describe sts ${name}

Update resources

  • Add an annotation for TiDBCluster:

    1. kubectl -n ${namespace} annotate tc ${cluster_name} ${key}=${value}

    Add a force-upgrade annotation for TiDBCluster:

    1. kubectl -n ${namespace} annotate --overwrite tc ${cluster_name} tidb.pingcap.com/force-upgrade=true

    Delete a force-upgrade annotation for TiDBCluster:

    1. kubectl -n ${namespace} annotate tc ${cluster_name} tidb.pingcap.com/force-upgrade-

    Enable the debug mode for Pods:

    1. kubectl -n ${namespace} annotate pod ${pod_name} runmode=debug

Edit resources

  • Edit TidbCluster:

    1. kubectl -n ${namespace} edit tc ${name}

Patch Resources

  • Patch TidbCluster:

    1. kubectl -n ${namespace} patch tc ${name} --type merge -p '${json_path}'
  • Patch PV ReclaimPolicy:

    1. kubectl patch pv ${name} -p '{"spec":{"persistentVolumeReclaimPolicy":"Delete"}}'
  • Patch a PVC:

    1. kubectl -n ${namespace} patch pvc ${name} -p '{"spec": {"resources": {"requests": {"storage": "100Gi"}}}'
  • Patch StorageClass:

    1. kubectl patch storageclass ${name} -p '{"allowVolumeExpansion": true}'

Create resources

  • Create a cluster using the YAML file:

    1. kubectl -n ${namespace} apply -f ${file}
  • Create Namespace:

    1. kubectl create ns ${namespace}
  • Create Secret:

    Create Secret of the certificate:

    1. 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:

    1. 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:

    1. kubectl -n ${namespace} -it exec ${pod_name} -- cat /etc/pd/pd.toml
  • View the TiDB configuration file:

    1. kubectl -n ${namespace} -it exec ${pod_name} -- cat /etc/tidb/tidb.toml
  • View the TiKV configuration file:

    1. kubectl -n ${namespace} -it exec ${pod_name} -- cat /etc/tikv/tikv.toml
  • View Pod logs:

    1. kubectl -n ${namespace} logs ${pod_name} -f

    View logs of the previous container:

    1. kubectl -n ${namespace} logs ${pod_name} -p

    If there are multiple containers in a Pod, view logs of one container:

    1. kubectl -n ${namespace} logs ${pod_name} -c ${container_name}
  • Expose services:

    1. kubectl -n ${namespace} port-forward svc/${service_name} ${local_port}:${port_in_pod}

    Expose PD services:

    1. kubectl -n ${namespace} port-forward svc/${cluster_name}-pd 2379:2379

Interact with nodes

  • Mark the node as non-schedulable:

    1. kubectl cordon ${node_name}
  • Mark the node as schedulable:

    1. kubectl uncordon ${node_name}

Delete resources

  • Delete a Pod:

    1. kubectl delete -n ${namespace} pod ${pod_name}
  • Delete a PVC:

    1. kubectl delete -n ${namespace} pvc ${pvc_name}
  • Delete TidbCluster:

    1. kubectl delete -n ${namespace} tc ${tc_name}
  • Delete TidbMonitor:

    1. kubectl delete -n ${namespace} tidbmonitor ${tidb_monitor_name}
  • Delete TidbClusterAutoScaler:

    1. kubectl -n ${namespace} delete tidbclusterautoscaler ${name}

More

See kubectl Cheat Sheet for more kubectl usage.

Helm

Add Helm repository

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

Update Helm repository

  1. helm repo update

View available Helm chart

  • View charts in Helm Hub:

    1. helm search hub ${chart_name}

    For example:

    1. helm search hub mysql
  • View charts in other repositories:

    1. helm search repo ${chart_name} -l --devel

    For example:

    1. helm search repo tidb-operator -l --devel

Get the default values.yaml of the Helm chart

  1. helm inspect values ${chart_name} --version=${chart_version} > values.yaml

For example:

  1. helm inspect values pingcap/tidb-operator --version=v1.3.2 > values-tidb-operator.yaml

Deploy using Helm chart

  1. helm install ${name} ${chart_name} --namespace=${namespace} --version=${chart_version} -f ${values_file}

For example:

  1. helm install tidb-operator pingcap/tidb-operator --namespace=tidb-admin --version=v1.3.2 -f values-tidb-operator.yaml

View the deployed Helm release

  1. helm ls

Update Helm release

  1. helm upgrade ${name} ${chart_name} --version=${chart_version} -f ${values_file}

For example:

  1. helm upgrade tidb-operator pingcap/tidb-operator --version=v1.3.2 -f values-tidb-operator.yaml

Delete Helm release

  1. helm uninstall ${name} -n ${namespace}

For example:

  1. helm uninstall tidb-operator -n tidb-admin

More

See Helm Commands for more Helm usage.