Skip to content
Docs

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:

  1. MariaDBInstance
  2. MariaDBDatabase
  3. MariaDBUser

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.

A minimal MariaDBInstance needs only a disk size and an instance type:

apiVersion: database.entigo.com/v1alpha1
kind: MariaDBInstance
metadata:
name: example-mariadb
spec:
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:

Terminal window
kubectl get mariadbinstances.database.entigo.com example-mariadb -w
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/v1alpha1
kind: MariaDBInstance
metadata:
name: example-mariadb
spec:
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-reboot

For every field, see the MariaDBInstance API reference.

A MariaDBDatabase is required if you create a MariaDBUser, because every user is granted privileges on a specific database.

apiVersion: database.entigo.com/v1alpha1
kind: MariaDBDatabase
metadata:
name: example-database
spec:
instanceRef:
name: example-mariadb
defaultCharacterSet: utf8mb4 # optional
defaultCollation: utf8mb4_unicode_ci # optional

deletionProtection is true by default on a database too.

Terminal window
kubectl get mariadbdatabases.database.entigo.com example-database -w

For every field, see the MariaDBDatabase API reference.

A MariaDBUser needs spec.instanceRef, spec.databaseRef and spec.privileges:

apiVersion: database.entigo.com/v1alpha1
kind: MariaDBUser
metadata:
name: example-user
spec:
instanceRef:
name: example-mariadb
databaseRef:
name: example-database
privileges:
- SELECT
- INSERT
- UPDATE
- DELETE

Each user’s credentials are stored in the Kubernetes Secret <spec.instanceRef.name>-<metadata.name>, for example example-mariadb-example-user.

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/v1alpha1
kind: MariaDBUser
metadata:
name: example-owner
spec:
instanceRef:
name: example-mariadb
databaseRef:
name: example-database
table: '*'
privileges:
- ALL PRIVILEGES
maxConnectionsPerHour: 100
maxQueriesPerHour: 1000
maxUpdatesPerHour: 500
maxUserConnections: 10
grant:
users:
- example-user
Terminal window
kubectl get mariadbusers.database.entigo.com example-user -w
kubectl get mariadbusers.database.entigo.com example-owner -w

For every field, see the MariaDBUser API reference.

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.

# Example
apiVersion: v1
kind: Pod
metadata:
name: mariadb
spec:
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: password

All three resources show SYNCED and READY as True:

Terminal window
$ kubectl get mariadbinstances.database.entigo.com
NAME SYNCED READY COMPOSITION AGE
example-mariadb True True mariadbinstances.database.entigo.com 19m
$ kubectl get mariadbdatabases.database.entigo.com
NAME SYNCED READY COMPOSITION AGE
example-database True True mariadbdatabases.database.entigo.com 17m
$ kubectl get mariadbusers.database.entigo.com
NAME SYNCED READY COMPOSITION AGE
example-owner True True mariadbusers.database.entigo.com 18m
example-user True True mariadbusers.database.entigo.com 18m

One Secret per user, plus dbadmin:

$ kubectl get secret
NAME TYPE DATA AGE
example-mariadb-dbadmin Opaque 4 14m
example-mariadb-example-owner connection.crossplane.io/v1alpha1 4 14m
example-mariadb-example-user connection.crossplane.io/v1alpha1 4 14m
$ kubectl get secret example-mariadb-dbadmin -o yaml
apiVersion: v1
kind: Secret
metadata:
name: example-mariadb-dbadmin
namespace: <namespace>
type: Opaque
data:
endpoint: <base64-encoded-endpoint>
port: <base64-encoded-port>
username: <base64-encoded-username>
password: <base64-encoded-password>
$ kubectl get secret example-mariadb-example-user -o yaml
apiVersion: v1
kind: Secret
metadata:
name: example-mariadb-example-user
namespace: <namespace>
type: Opaque
data:
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.

Open a terminal in the pod and connect to the database using the mounted credentials.

Terminal window
$ kubectl get pod
NAME READY STATUS RESTARTS AGE
mariadb 1/1 Running 0 24s
$ kubectl exec -it mariadb -- bash
mariadb:/# env | grep ^MYSQL
MYSQL_TCP_PORT=3306
MYSQL_PWD=...
MYSQL_USER=example-user
MYSQL_HOST=example-mariadb-instance-...rds.amazonaws.com
mariadb:/# mariadb -h "$MYSQL_HOST" -P "$MYSQL_TCP_PORT" -u "$MYSQL_USER" example-database

Verify 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` |
+--------------------------------------------------------------------------------+

Delete the resources in the reverse order of creation:

  1. MariaDBUser
  2. MariaDBDatabase
  3. 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:

Terminal window
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:

Terminal window
kubectl delete mariadbusers.database.entigo.com example-user example-owner
kubectl delete mariadbdatabases.database.entigo.com example-database
kubectl delete mariadbinstances.database.entigo.com example-mariadb

Deleting 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.

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.

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.

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.

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.

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