mariadb

This post shows how to create a MariaDB database in Kubernetes using the mariadb-operator. The mariadb-operator is a Kubernetes operator that manages MariaDB databases in Kubernetes. It is based on the Operator SDK and uses the MariaDB Operator API to manage MariaDB databases. Or, as the maintainers of the mariadb-operator put it: Run and operate MariaDB in a cloud native way. Declaratively manage your MariaDB using Kubernetes CRDs rather than imperative commands.

Installation

First, we need to install the mariadb-operator. The mariadb-operator is available as a Helm chart. We can install it using the following command:

helm repo add mariadb-operator https://helm.mariadb.com/mariadb-operator
helm install mariadb-operator mariadb-operator/mariadb-operator \
--set metrics.enabled=true --set webhook.cert.certManager.enabled=true

Create a MariaDB database

Before we can create a MariaDB database, we need to create a root password for the MariaDB database. We can do this by creating a Kubernetes secret with the root password. The following is an example of a Kubernetes secret that contains the root password for the MariaDB database:

apiVersion: v1
kind: Secret
metadata:
  name: mariadb-root-password
  namespace: default
type: Opaque
data:
  password: cGFzc3dvcmQ=

Now that we have installed the mariadb-operator, we can create a MariaDB database. We can do this by creating a MariaDB custom resource (CR). The following is an example of a MariaDB CR that creates a MariaDB database with the name database:

apiVersion: k8s.mariadb.com/v1alpha1
kind: MariaDB
metadata:
  name: database
  namespace: default
spec:
  rootPasswordSecretKeyRef:
    name: mariadb
    key: mariadb-root-password
  username: mariadb
  passwordSecretKeyRef:
    name: mariadb
    key: password
  database: mariadb
  image: docker-registry1.mariadb.com/library/mariadb:latest
  imagePullPolicy: IfNotPresent
  port: 3306
  storage:
    size: 8Gi
    storageClassName: standard
  connection:
    secretName: connection-mariadb
    secretTemplate:
      key: dsn
    healthCheck:
      interval: 10s
      retryInterval: 3s
    params:
      parseTime: "true"
  myCnf: |
    [mariadb]
    bind-address=*
    default_storage_engine=InnoDB
    binlog_format=row
    innodb_autoinc_lock_mode=2
    innodb_buffer_pool_size=1024M
    max_allowed_packet=256M
  myCnfConfigMapKeyRef:
    name: mariadb
    key: my.cnf
  resources:
    requests:
      cpu: 100m
      memory: 128Mi
    limits:
      memory: 1Gi
  podSecurityContext:
    runAsUser: 0
  securityContext:
    allowPrivilegeEscalation: false
  livenessProbe:
    exec:
      command:
        - bash
        - -c
        - mariadb -u root -p"${MARIADB_ROOT_PASSWORD}" -e "SELECT 1;"
    initialDelaySeconds: 20
    periodSeconds: 5
    timeoutSeconds: 5
  readinessProbe:
    exec:
      command:
        - bash
        - -c
        - mariadb -u root -p"${MARIADB_ROOT_PASSWORD}" -e "SELECT 1;"
    initialDelaySeconds: 20
    periodSeconds: 5
    timeoutSeconds: 5
  podDisruptionBudget:
    maxUnavailable: 50%
  updateStrategy:
    type: ReplicasFirstPrimaryLast
  service:
    type: ClusterIP
  metrics:
    enabled: true

The MariaDB CR creates a MariaDB database with the following configuration:

  • The root password for the MariaDB database is stored in a Kubernetes secret with the name mariadb-root-password.
  • The username for the MariaDB database is mariadb.
  • The password for the MariaDB database is stored in a Kubernetes secret with the name mariadb and the key password.
  • The name of the MariaDB database is mariadb.
  • The image for the MariaDB database is docker-registry1.mariadb.com/library/mariadb:latest.
  • The port for the MariaDB database is 3306.
  • The storage for the MariaDB database is 8Gi with the storage class standard.
  • The MariaDB endpoint is available at the service database.default.svc.cluster.local.

GitHub repo

More information at the official GitHub repo. GitHub.com/mariadb-operator/mariadb-operator

Leave a Reply

Your email address will not be published. Required fields are marked *