Create MariaDB Resources (RC)
Create a MariaDB instance, databases and users, and mount their credentials in a Pod.
On this page
- Prerequisites
- 1. Create the instance
- Optional settings
- 2. Create a database
- 3. Create a user
- Optional settings
- 4. Mount the credentials in a Pod
- 5. Verify
- Resources are ready
- Credentials are stored
- The Pod can connect
- 6. Delete the resources
- Troubleshooting
- EnvironmentConfig
- backupRetentionPeriod
- mariaDBBackupBeforeDeletion
- Infralib Configuration Example
This guide creates an Amazon RDS for MariaDB instance from Kubernetes, adds a database and a user to it, mounts the connection credentials in a Pod, and deletes the resources again.
You describe the database with three manifests: MariaDBInstance, MariaDBDatabase and MariaDBUser. The platform creates the RDS instance, the security group and security group rules that let Pods reach it, and a Kubernetes Secret with connection credentials for each user. Backup behaviour is set once for the whole cluster, see EnvironmentConfig.
It is good practice to include the manifests in your application’s Helm chart, so the database is deployed together with the application that uses it.
The resources depend on each other. A MariaDBUser references a database through spec.databaseRef, so create them in this order:
- MariaDBInstance
- MariaDBDatabase
- MariaDBUser
Prerequisites
Section titled “Prerequisites”MariaDB is not enabled by default. Your Infralib configuration needs:
| Module | Flag | Default | What it does |
|---|---|---|---|
platform-apis |
platform-apis.mariadb.install |
false |
Installs the MariaDBInstance, MariaDBDatabase and MariaDBUser APIs. Set it to true. |
platform-apis |
platform-apis.postgresql.install |
true |
Also renders the shared database EnvironmentConfig and function that MariaDB uses. Keep it true. |
crossplane-aws |
global.createObservers, global.createNamespacedObservers |
false |
Installs the RDS and EC2 providers and the VPC lookups the instance needs. Set both to true. |
crossplane-sql |
— | — | Creates databases and users inside the instance. Add the module to the argocd-apps step. |
For the base modules every platform API needs, see Infralib Requirements.
1. Create the instance
Section titled “1. Create the instance”A minimal MariaDBInstance needs only a disk size and an instance type:
apiVersion: database.entigo.com/v1alpha1kind: MariaDBInstancemetadata: name: example-mariadbspec: allocatedStorage: 20 engineVersion: '11.4.10' instanceType: 'db.t3.micro'The master user dbadmin is created with the instance. Its credentials are stored in the Kubernetes Secret <metadata.name>-dbadmin and in AWS Secrets Manager.
Creating the instance takes more than 5 minutes. Watch it until READY is True:
kubectl get mariadbinstances.database.entigo.com example-mariadb -wOptional settings
Section titled “Optional settings”| Field | Default | What it does |
|---|---|---|
multiAZ |
false |
Runs a standby copy in a second availability zone. |
deletionProtection |
true |
Blocks deletion. Set to false before you delete the instance. |
iops |
unset | Reserved IOPS. Increasing the disk size is usually cheaper. |
backupRetentionPeriod |
14 |
Days of daily backups to keep, 0–35. Overrides the cluster default. |
backupWindow |
set by region | Daily backup time in UTC, for example '03:00-05:00'. Must not overlap with the maintenance window. |
maintenanceWindow |
set by AWS | Weekly maintenance time, for example 'Mon:00:00-Mon:03:00'. |
autoMinorVersionUpgrade |
true |
Applies minor engine upgrades in the maintenance window. |
allowMajorVersionUpgrade |
false |
Allows changing engineVersion to a new major version. |
parameterGroupName |
unset | Uses an existing RDS parameter group, for example 'default.mariadb11.4'. |
parameterGroupParameters |
unset | Sets individual MariaDB parameters. The reserved key applyMethod (immediate or pending-reboot) controls how they are applied. |
For example, a production instance with a standby and custom parameters:
apiVersion: database.entigo.com/v1alpha1kind: MariaDBInstancemetadata: name: example-mariadbspec: allocatedStorage: 20 engineVersion: '11.4.10' instanceType: 'db.t3.micro' multiAZ: true backupRetentionPeriod: 7 backupWindow: '03:00-05:00' maintenanceWindow: 'Mon:00:00-Mon:03:00' parameterGroupParameters: max_connections: '200' applyMethod: pending-rebootFor every field, see the MariaDBInstance API reference.
2. Create a database
Section titled “2. Create a database”A MariaDBDatabase is required if you create a MariaDBUser, because every user is granted privileges on a specific database.
apiVersion: database.entigo.com/v1alpha1kind: MariaDBDatabasemetadata: name: example-databasespec: instanceRef: name: example-mariadb defaultCharacterSet: utf8mb4 # optional defaultCollation: utf8mb4_unicode_ci # optionaldeletionProtection is true by default on a database too.
kubectl get mariadbdatabases.database.entigo.com example-database -wFor every field, see the MariaDBDatabase API reference.
3. Create a user
Section titled “3. Create a user”A MariaDBUser needs spec.instanceRef, spec.databaseRef and spec.privileges:
apiVersion: database.entigo.com/v1alpha1kind: MariaDBUsermetadata: name: example-userspec: instanceRef: name: example-mariadb databaseRef: name: example-database privileges: - SELECT - INSERT - UPDATE - DELETEEach user’s credentials are stored in the Kubernetes Secret <spec.instanceRef.name>-<metadata.name>, for example example-mariadb-example-user.
Optional settings
Section titled “Optional settings”| Field | Default | What it does |
|---|---|---|
table |
* |
Tables the privileges apply to. |
maxConnectionsPerHour |
5 |
How many times the user can connect per hour. |
maxQueriesPerHour |
20 |
How many queries the user can run per hour. |
maxUpdatesPerHour |
10 |
How many updates the user can run per hour. |
maxUserConnections |
2 |
How many connections the user can hold at the same time. |
grant.users |
unset | Other users whose privileges are granted to this user. |
The connection limits are low by default. For example, an owner with all privileges, higher limits and a grant to another user:
apiVersion: database.entigo.com/v1alpha1kind: MariaDBUsermetadata: name: example-ownerspec: instanceRef: name: example-mariadb databaseRef: name: example-database table: '*' privileges: - ALL PRIVILEGES maxConnectionsPerHour: 100 maxQueriesPerHour: 1000 maxUpdatesPerHour: 500 maxUserConnections: 10 grant: users: - example-userkubectl get mariadbusers.database.entigo.com example-user -wkubectl get mariadbusers.database.entigo.com example-owner -wFor every field, see the MariaDBUser API reference.
4. Mount the credentials in a Pod
Section titled “4. Mount the credentials in a Pod”Read the credentials from the user’s Secret into the standard MYSQL_* environment variables. Use the dbadmin Secret only for administration.
For more information about Secrets in Kubernetes, see Kubernetes documentation.
# ExampleapiVersion: v1kind: Podmetadata: name: mariadbspec: terminationGracePeriodSeconds: 1 containers: - name: mariadb image: mariadb:latest command: ['sleep', 'infinity'] env: - name: MYSQL_HOST valueFrom: secretKeyRef: name: example-mariadb-example-user key: endpoint - name: MYSQL_TCP_PORT valueFrom: secretKeyRef: name: example-mariadb-example-user key: port - name: MYSQL_USER valueFrom: secretKeyRef: name: example-mariadb-example-user key: username - name: MYSQL_PWD valueFrom: secretKeyRef: name: example-mariadb-example-user key: password5. Verify
Section titled “5. Verify”Resources are ready
Section titled “Resources are ready”All three resources show SYNCED and READY as True:
$ kubectl get mariadbinstances.database.entigo.comNAME SYNCED READY COMPOSITION AGEexample-mariadb True True mariadbinstances.database.entigo.com 19m$ kubectl get mariadbdatabases.database.entigo.comNAME SYNCED READY COMPOSITION AGEexample-database True True mariadbdatabases.database.entigo.com 17m$ kubectl get mariadbusers.database.entigo.comNAME SYNCED READY COMPOSITION AGEexample-owner True True mariadbusers.database.entigo.com 18mexample-user True True mariadbusers.database.entigo.com 18mCredentials are stored
Section titled “Credentials are stored”One Secret per user, plus dbadmin:
$ kubectl get secretNAME TYPE DATA AGEexample-mariadb-dbadmin Opaque 4 14mexample-mariadb-example-owner connection.crossplane.io/v1alpha1 4 14mexample-mariadb-example-user connection.crossplane.io/v1alpha1 4 14m
$ kubectl get secret example-mariadb-dbadmin -o yamlapiVersion: v1kind: Secretmetadata: name: example-mariadb-dbadmin namespace: <namespace>type: Opaquedata: endpoint: <base64-encoded-endpoint> port: <base64-encoded-port> username: <base64-encoded-username> password: <base64-encoded-password>
$ kubectl get secret example-mariadb-example-user -o yamlapiVersion: v1kind: Secretmetadata: name: example-mariadb-example-user namespace: <namespace>type: Opaquedata: endpoint: <base64-encoded-endpoint> port: <base64-encoded-port> username: <base64-encoded-username> password: <base64-encoded-password>The dbadmin credentials are also in AWS Secrets Manager.
The Pod can connect
Section titled “The Pod can connect”Open a terminal in the pod and connect to the database using the mounted credentials.
$ kubectl get podNAME READY STATUS RESTARTS AGEmariadb 1/1 Running 0 24s
$ kubectl exec -it mariadb -- bashmariadb:/# env | grep ^MYSQLMYSQL_TCP_PORT=3306MYSQL_PWD=...MYSQL_USER=example-userMYSQL_HOST=example-mariadb-instance-...rds.amazonaws.com
mariadb:/# mariadb -h "$MYSQL_HOST" -P "$MYSQL_TCP_PORT" -u "$MYSQL_USER" example-databaseVerify the configuration is correct.
MariaDB [example-database]> SHOW DATABASES;+--------------------+| Database |+--------------------+| example-database || information_schema |+--------------------+
MariaDB [example-database]> SELECT User, Host FROM mysql.user;+---------------+-----------+| User | Host |+---------------+-----------+| dbadmin | % || example-owner | % || example-user | % |+---------------+-----------+
MariaDB [example-database]> SHOW GRANTS FOR CURRENT_USER();+--------------------------------------------------------------------------------+| Grants for example-user@% |+--------------------------------------------------------------------------------+| GRANT USAGE ON *.* TO `example-user`@`%` || GRANT SELECT, INSERT, UPDATE, DELETE ON `example-database`.* TO `example-user` |+--------------------------------------------------------------------------------+6. Delete the resources
Section titled “6. Delete the resources”Delete the resources in the reverse order of creation:
- MariaDBUser
- MariaDBDatabase
- MariaDBInstance
A MariaDBUser protects both its MariaDBInstance and its MariaDBDatabase, and a MariaDBDatabase protects its MariaDBInstance. While a dependent resource exists, the delete request for the resource it references stays pending until the dependent is removed.
MariaDBInstance and MariaDBDatabase also have deletionProtection enabled by default and cannot be deleted while it is true. Turn it off first:
kubectl patch mariadbdatabases.database.entigo.com example-database --type merge -p '{"spec":{"deletionProtection":false}}'kubectl patch mariadbinstances.database.entigo.com example-mariadb --type merge -p '{"spec":{"deletionProtection":false}}'Then delete in dependency order:
kubectl delete mariadbusers.database.entigo.com example-user example-ownerkubectl delete mariadbdatabases.database.entigo.com example-databasekubectl delete mariadbinstances.database.entigo.com example-mariadbDeleting the MariaDBInstance removes the underlying database and all of its data. If mariaDBBackupBeforeDeletion is enabled for the environment, a final snapshot is taken before the instance is removed, see EnvironmentConfig.
Troubleshooting
Section titled “Troubleshooting”| Symptom | Cause / fix |
|---|---|
no matches for kind "MariaDBInstance" |
The API is not installed. MariaDB is off by default, check the Prerequisites. |
SYNCED is False |
The platform could not create a cloud resource. kubectl describe mariadbinstances.database.entigo.com <name> shows the error under Conditions and Events. |
READY stays False for several minutes after creation |
Normal. RDS takes more than 5 minutes to create an instance. |
A database or user never becomes READY |
The instance must be READY first, and a user also needs its database. If they are, check that the crossplane-sql module is installed. |
| The application hits connection or query limits | MariaDBUser limits are low by default. Raise maxConnectionsPerHour, maxQueriesPerHour, maxUpdatesPerHour and maxUserConnections. |
| A delete request stays pending | A dependent resource still exists, or deletionProtection is true. See Delete the resources. |
EnvironmentConfig
Section titled “EnvironmentConfig”Backup behaviour is not configured on the MariaDBInstance object. It is set once for the whole cluster in the platform-apis EnvironmentConfig, so every database gets the same protection without developers having to think about it.
| Setting | Default | What it does |
|---|---|---|
backupRetentionPeriod |
14 |
How many days of automatic daily backups AWS keeps for every instance. |
mariaDBBackupBeforeDeletion |
true |
Takes a final snapshot before an instance is deleted, so the data can still be recovered. |
tags |
{} |
Cloud tags added to every database resource. See Resource Tagging. |
backupRetentionPeriod
Section titled “backupRetentionPeriod”The number of days AWS keeps automatic daily backups. A higher number means you can restore further back in time, at the cost of extra backup storage.
This is only a default. A single instance can use a different value through spec.backupRetentionPeriod, as shown in Optional settings.
mariaDBBackupBeforeDeletion
Section titled “mariaDBBackupBeforeDeletion”When enabled, deleting a MariaDBInstance takes one last snapshot of the database before the instance disappears. That snapshot stays in AWS and can be used to bring the data back later.
Turning this off means a deleted instance is gone for good. Only do that in throwaway environments where the data does not matter.
Infralib Configuration Example
Section titled “Infralib Configuration Example”steps: ... - name: apps type: argocd-apps modules: ... - name: platform-apis source: platform-apis inputs: platform-apis: mariadb: install: true database: environmentConfig: backupRetentionPeriod: 14 mariaDBBackupBeforeDeletion: true