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
only entities that are not deleted are Persistent Volumes which contain the
database data.
When a DB Instance is bootstrapped, a set of management users along with
matching credentials are created and stored within the database:
When the DB Instance is deleted, the Kubernetes secret (like the one
mentioned above) is also deleted. However, the users and their
passwords stored in that secret will still remain in the database, since the
Persistent Volumes are not removed.
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:
- Delete the database files from the disk, let the
DB Instancebootstrap, then use a backup to restore the data files. - 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:
-
lifecycle -
livenessProbe -
readinessProbe -
startupProbeTo 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:bash
Delete 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:bash
Note
Repeat steps 3, 4, and 5 for each MySQL pod in the affected DB 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.