Accidental deletion of a DB Instance composition
This document explains how to handle the accidental deletion of a DB Instance composition.
Each DB Instance causes the DB Operator to spin up N dedicated MySQL replicas, with one considered the primary. When the DB Instance object is deleted, the DB Operator deletes all Kubernetes objects related to the deleted DB Instance, such as Stateful Sets, Services, Deployments, etc. The database data stored on the MySQL instance volumes and the Kubernetes secrets containing the management user passwords are not deleted.
When a DB Instance is bootstrapped, a set of management users along with matching credentials are created and stored within the database:
Note
Deleting the DB Instance does not delete this Kubernetes secret or the ps-db-{DATABASE_NAME}-secrets secret that it is created from. If you recreate the DB Instance with the same name, it reuses the existing secrets and the existing data.
The problem described in this document occurs when both secrets are lost, for example deleted manually, while the database data is kept. The users and their passwords stored in these secrets still remain in the database, but the passwords are no longer available in the cluster.
When you recreate the DB Instance, DB Operator creates a new Kubernetes secret and tries to create the users and the passwords in the DB Instance upon bootstrap. This fails because the intended users already exist in the database. A common symptom is the following error message in the readiness probe:
There are two ways to recover from this situation:
- Remove the existing database data, let the
DB Instancebootstrap, then use a backup to restore the data. To remove the data, delete the database files from the node-local paths of the MySQL instances. If the volumes are provided by the storage class, delete thedatadir-ps-db-{DATABASE_NAME}-mysql-{INSTANCE_NUMBER}Persistent Volume Claims instead. - Overwrite the existing passwords during the
DB Instancebootstrap.
The best option depends on factors such as time constraints, backup availability, and the ability to restore from the backup.
The second recovery option is described below. Recovery using an available backup is not covered in this document.
Overwrite the existing passwords during the DB Instance bootstrap
Patch the StatefulSet template of the
DB Instanceto allow access to themysqlcontainer. Then, remove all lifecycle-related helpers from themysqlcontainer configuration, including:lifecyclelivenessProbereadinessProbestartupProbe
To apply this, run the following command:
bashThen, update the
commandfield of themysqlcontainer to keep it running without starting the actual database process:bashFinally, remove the
xtrabackupandheartbeatcontainers:bashDelete all pods of the affected
DB Instance. Kubernetes will re-create the pods in their patched version.Attach to the
mysqlcontainer:bashCreate a file with recovery script called
/tmp/recovery.sh:bashThe script resets internal MySQL user passwords directly within the container. Since the
mysqlprocess is not running (due to the overriddencommand), this allows you to enter the container safely and manually start MySQL with controlled conditions.The
/tmp/recovery.shscript:- Loads user passwords securely from environment variables or mounted secrets.
- Uses these values to update MySQL internal user accounts with new passwords.
- Ensures MySQL permissions are refreshed using
FLUSH PRIVILEGES.
Run the
/tmp/recovery.shscript:bashOnce the log shows a success, send a
TERMsignalOpens in a new tab to themysqldPID to stop the server:bashNote
Repeat steps 3, 4, and 5 for each
MySQLpod in the affectedDB Instance.Set the replica parameter of the
PerconaServerMySQLKubernetes resource for the affectedDB Instanceto0.Delete the StatefulSet of the affected
DB Instance. Once the patched StatefulSet and its related resources are removed (Persistent Volumes are preserved), the operator will recreate a new one using the original configuration.Set the replica parameter of the
PerconaServerMySQLKubernetes resource for the affectedDB Instanceback to the original production value (usually2). TheDB Operatormonitoring this resource will detect the change and recreate the StatefulSet with the default configuration.
At this point, the credentials stored in the database should match those in the corresponding Kubernetes Secret, allowing all components to start functioning correctly.