Skip to content

etcd defragmentation#

The underlying etcd database can become fragmented over time, as keys are created, updated, and deleted. As a result, the disk space from deleted entries is not immediately reclaimed, which causes the on-disk size of etcd to increase beyond the actual size of the data set.

Defragmentation rewrites the database to reclaim the wasted space, which reduces disk usage and improves I/O efficiency. As such, to reclaim space and maintain cluster performance, you should periodically perform this maintenance operation.

Important

  • Kubernetes event cleanup and etcd compaction operations must be run prior to defragmentation, as the defragmentation process operates in the space that is freed up by the cleanup and compaction operations.
  • Defragmentation is a blocking operation that can temporarily impact etcd performance, and thus it should only be scheduled to run during maintenance windows.

MKE 4 provides defragmentation utilities through the etcd maintenance service. Currently, only manual defragmentation calls are supported.

To defragment an etcd cluster

  1. SSH to any controller node:

    ssh -i <path_to_ssh_key> user@<controller_node_ip> 
    
  2. Verify that the etcd maintenance service is running:

    sudo systemctl status etcdmaintenance
    

    The output shows active (running) when the service is running.

  3. Verify that no defragmentation operation is in progress:

    sudo /usr/local/bin/etcd-maintenance status
    

    The status subcommand reports the state of each etcd member and whether a defragmentation operation is in progress. If DefragInProgress is true, wait for the current operation to complete before you continue.

    Example response:

    {
      "DefragInProgress": false,
      "members": {
        "172.31.0.149": {
          "DBSize": "89 MB",
          "DBSizeInUse": "31 MB",
          "alarms": null,
          "etcdVersion": "3.5.18",
          "isLeader": false,
          "memberID": 1141391576588418377
        },
        "172.31.0.175": {
          "DBSize": "89 MB",
          "DBSizeInUse": "31 MB",
          "alarms": null,
          "etcdVersion": "3.5.18",
          "isLeader": true,
          "memberID": -3067575474720874990
        },
        "172.31.0.191": {
          "DBSize": "89 MB",
          "DBSizeInUse": "31 MB",
          "alarms": null,
          "etcdVersion": "3.5.18",
          "isLeader": false,
          "memberID": 5368207591176096260
        }
      }
    }
    

    If the response includes an unreachableMembers list, the etcd maintenance service did not respond on the listed controller nodes. The defrag subcommand does not run until every member responds.

  4. Start the cluster defragmentation:

    sudo /usr/local/bin/etcd-maintenance defrag --timeout-seconds 600 --pause-seconds 60
    

    The defrag subcommand defragments the etcd members one at a time. Use the following flags to control the operation:

    • --timeout-seconds: Maximum time allowed for each member defragmentation. The example sets 600 seconds (10 minutes).
    • --pause-seconds: Pause between member defragmentations, which lets each defragmented member catch up with the cluster. The example sets 60 seconds (1 minute).

    Adjust both values based on the size of your etcd database and the time elapsed since the last defragmentation.

    Example response:

    Defrag initiated by leader 172.31.0.175: Defrag started by leader
    

    The command returns as soon as the defragmentation starts. If a defragmentation is already in progress, the command exits with code 75 and does not start another one.

  5. Monitor the state of the defragmentation operation.

    You can use the etcd maintenance service logs to monitor the defragmentation, or you can check the cluster status as follows:

    sudo /usr/local/bin/etcd-maintenance status
    

    Example response:

    {
      "DefragInProgress": true,
      "members": {
        "172.31.0.149": {
          "DBSize": "22 MB",
          "DBSizeInUse": "22 MB",
          "alarms": null,
          "etcdVersion": "3.5.18",
          "isLeader": false,
          "memberID": 1141391576588418377
        },
        "172.31.0.175": {
          "DBSize": "22 MB",
          "DBSizeInUse": "22 MB",
          "alarms": null,
          "etcdVersion": "3.5.18",
          "isLeader": true,
          "memberID": -3067575474720874990
        },
        "172.31.0.191": {
          "DBSize": "22 MB",
          "DBSizeInUse": "22 MB",
          "alarms": null,
          "etcdVersion": "3.5.18",
          "isLeader": false,
          "memberID": 5368207591176096260
        }
      }
    }
    

Clearing the NOSPACE alarm

Defragmentation clears the etcd NOSPACE alarm only if it reclaims space. If defragmentation frees no space, the alarm remains active.

Defragmentation Locking

To maintain etcd cluster availability, MKE 4 uses a lock file to prevent you from simultaneously triggering multiple defragmentations. The host service automatically removes this lock file at the end of each defragmentation operation.

If a node that is running a defragmentation operation goes offline or has some other unforeseeable issue, the removal of the lock file may not occur, which will prevent defragmentation operations going forward. In such an event, you can run the following command on the affected node to manually remove the lock file:

rm /var/lib/etcd-maintenance/defrag.lock

Be aware that you should only run this command in adverse situations as a last resort.