|
| 1 | +--- |
| 2 | +title: Maintain Kubernetes Nodes That Hold the TiDB Cluster |
| 3 | +summary: Learn how to maintain Kubernetes nodes that hold the TiDB cluster. |
| 4 | +--- |
| 5 | + |
| 6 | +# Maintain Kubernetes Nodes That Hold the TiDB Cluster |
| 7 | + |
| 8 | +TiDB is a highly available database that can run smoothly when some of the database nodes go offline. Therefore, you can safely shut down and maintain the Kubernetes nodes that host TiDB clusters. |
| 9 | + |
| 10 | +This document describes how to perform maintenance operations on Kubernetes nodes based on maintenance duration and storage type. |
| 11 | + |
| 12 | +## Prerequisites |
| 13 | + |
| 14 | +- Install [`kubectl`](https://kubernetes.io/docs/tasks/tools/). |
| 15 | + |
| 16 | +> **Note:** |
| 17 | +> |
| 18 | +> Before you maintain a node, make sure that the remaining resources in the Kubernetes cluster are enough for running the TiDB cluster. |
| 19 | +
|
| 20 | +## Maintain a node |
| 21 | + |
| 22 | +### Step 1: Preparation |
| 23 | + |
| 24 | +1. Use the `kubectl cordon` command to mark the node to be maintained as unschedulable to prevent new Pods from being scheduled to this node: |
| 25 | + |
| 26 | + ```shell |
| 27 | + kubectl cordon ${node_name} |
| 28 | + ``` |
| 29 | + |
| 30 | +2. Check whether any TiDB cluster component Pods are running on the node to be maintained: |
| 31 | + |
| 32 | + ```shell |
| 33 | + kubectl get pod --all-namespaces -o wide -l pingcap.com/managed-by=tidb-operator | grep ${node_name} |
| 34 | + ``` |
| 35 | + |
| 36 | + - If the node has TiDB cluster component Pods, follow the subsequent steps in this document to migrate these Pods. |
| 37 | + - If the node does not have any TiDB cluster component Pods, there is no need to migrate Pods, and you can proceed directly with node maintenance. |
| 38 | + |
| 39 | +### Step 2: Migrate TiDB cluster component Pods |
| 40 | + |
| 41 | +Based on the storage type of the Kubernetes node, choose the corresponding Pod migration strategy: |
| 42 | + |
| 43 | +- **Automatically migratable storage**: use [Method 1: Reschedule Pods](#method-1-reschedule-pods-for-automatically-migratable-storage). |
| 44 | +- **Non-automatically migratable storage**: use [Method 2: Recreate instances](#method-2-recreate-instances-for-local-storage). |
| 45 | + |
| 46 | +#### Method 1: Reschedule Pods (for automatically migratable storage) |
| 47 | + |
| 48 | +If you use storage that supports automatic migration (such as [Amazon EBS](https://aws.amazon.com/ebs/)), you can reschedule component Pods by following [Perform a graceful restart of a single Pod in a component](restart-a-tidb-cluster.md#perform-a-graceful-restart-of-a-single-pod-in-a-component). The following instructions take rescheduling PD Pods as an example: |
| 49 | + |
| 50 | +1. Check the PD Pod on the node to be maintained: |
| 51 | + |
| 52 | + ```shell |
| 53 | + kubectl get pod --all-namespaces -o wide -l pingcap.com/component=pd | grep ${node_name} |
| 54 | + ``` |
| 55 | + |
| 56 | +2. Get the instance name of the PD Pod: |
| 57 | + |
| 58 | + ```shell |
| 59 | + kubectl get pod -n ${namespace} ${pod_name} -o jsonpath='{.metadata.labels.pingcap\.com/instance}' |
| 60 | + ``` |
| 61 | + |
| 62 | +3. Add a new label to the PD instance to trigger rescheduling: |
| 63 | + |
| 64 | + ```shell |
| 65 | + kubectl label pd -n ${namespace} ${pd_instance_name} pingcap.com/restartedAt=2025-06-30T12:00 |
| 66 | + ``` |
| 67 | + |
| 68 | +4. Confirm that the PD Pod is successfully scheduled to another node: |
| 69 | + |
| 70 | + ```shell |
| 71 | + watch kubectl -n ${namespace} get pod -o wide |
| 72 | + ``` |
| 73 | + |
| 74 | +5. Follow the same steps to migrate Pods of other components such as TiKV and TiDB until all TiDB cluster component Pods on the node are migrated. |
| 75 | + |
| 76 | +#### Method 2: Recreate instances (for local storage) |
| 77 | + |
| 78 | +If the node uses storage that cannot be automatically migrated (such as local storage), you need to recreate instances. |
| 79 | + |
| 80 | +> **Warning:** |
| 81 | +> |
| 82 | +> Recreating instances causes data loss. For stateful components such as TiKV, ensure that the cluster has sufficient replicas to guarantee data safety. |
| 83 | + |
| 84 | +The following instructions take recreating a TiKV instance as an example: |
| 85 | + |
| 86 | +1. Delete the CR of the TiKV instance. TiDB Operator automatically deletes the associated PVC and ConfigMap resources, and creates a new instance: |
| 87 | + |
| 88 | + ```shell |
| 89 | + kubectl delete -n ${namespace} tikv ${tikv_instance_name} |
| 90 | + ``` |
| 91 | + |
| 92 | +2. Wait for the status of the newly created TiKV instance to become `Ready`: |
| 93 | + |
| 94 | + ```shell |
| 95 | + kubectl get -n ${namespace} tikv ${tikv_instance_name} |
| 96 | + ``` |
| 97 | + |
| 98 | +3. After you confirm that the TiDB cluster status is normal and data synchronization is completed, continue to maintain other components. |
| 99 | + |
| 100 | +### Step 3: Confirm migration completion |
| 101 | + |
| 102 | +After you complete Pod migration, only the Pods managed by DaemonSet (such as network plugins and monitoring agents) should be running on the node: |
| 103 | + |
| 104 | +```shell |
| 105 | +kubectl get pod --all-namespaces -o wide | grep ${node_name} |
| 106 | +``` |
| 107 | + |
| 108 | +### Step 4: Perform node maintenance |
| 109 | + |
| 110 | +You can now safely perform maintenance operations on the node, such as restarting, updating the operating system, or performing hardware maintenance. |
| 111 | + |
| 112 | +### Step 5: Recover after maintenance (for temporary maintenance only) |
| 113 | + |
| 114 | +If you plan to perform long-term maintenance or permanently take the node offline, skip this step. |
| 115 | + |
| 116 | +For temporary maintenance, perform the following recovery operations after the node maintenance is completed: |
| 117 | + |
| 118 | +1. Check the node health status: |
| 119 | + |
| 120 | + ```shell |
| 121 | + watch kubectl get node ${node_name} |
| 122 | + ``` |
| 123 | + |
| 124 | + When the node status becomes `Ready`, continue to the next step. |
| 125 | + |
| 126 | +2. Use the `kubectl uncordon` command to remove the scheduling restriction on the node: |
| 127 | + |
| 128 | + ```shell |
| 129 | + kubectl uncordon ${node_name} |
| 130 | + ``` |
| 131 | + |
| 132 | +3. Check whether all Pods are running normally: |
| 133 | + |
| 134 | + ```shell |
| 135 | + kubectl get pod --all-namespaces -o wide | grep ${node_name} |
| 136 | + ``` |
| 137 | + |
| 138 | + When all Pods are running normally, the maintenance operation is completed. |
0 commit comments