> For the complete documentation index, see [llms.txt](https://docs.cennso.com/cennso/cennso-db/latest/llms.txt) (all documentation: [llms.txt](https://docs.cennso.com/llms.txt)). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.cennso.com/cennso/cennso-db/latest/deployment-guides/cennso-database.md).

# Deploy Cennso Database

This guide provides description of how to deploy the Cennso Database Operator and Cennso Database Instance to get a ready-to-use MySQL cluster of two instances with [async replication](https://dev.mysql.com/doc/refman/8.0/en/replication.html).

The deployment consists of two stages described in the next two sections:

1. [Deploy Cennso Database Operator](#deploy-cennso-database-operator)
2. [Deploy Cennso Database Instance](#deploy-cennso-database-instance)

After the deployment, you can [validate your MySQL cluster](https://docs.cennso.com/cennso/cennso-db/latest/deployment-guides/cennso-database-validation.md), [create database users](https://docs.cennso.com/cennso/cennso-db/latest/operation-guides/users-creation.md), and start to use your database cluster.

> [!TIP]
> If any issues occur during component deployment, see the [troubleshooting](https://docs.cennso.com/cennso/cennso-db/latest/troubleshooting/troubleshooting.md) guide.

## Deploy Cennso Database Operator

This section shows the example of how to deploy the Cennso Database Operator in the Core Cluster specified by the `{CORE-CLUSTER-NAME}`. After the deployment, the operator Pod runs in the `{OPERATOR-NAMESPACE}` Kubernetes Namespace and manages the MySQL clusters that `DBInstance` resources create in the `{NAMESPACE-WITH-DATABASE-INSTANCES}` Kubernetes Namespace.

To deploy a Cennso Database Operator described above, execute the following steps:

1. Prepare the `DBOperator` Custom Resource (CR) to deploy the Cennso Database Operator in your Management cluster. This CR defines the connection to the Core Cluster and the operator configuration. For example:

   ```yaml
   apiVersion: db.contrib.cennso.com/v1alpha1
   kind: DBOperator
   metadata:
     name: {DATABASE-OPERATOR-CLAIM-NAME}
     namespace: cennso-infra
   spec:
     connectionSecretRef:
       name: {CORE-CLUSTER-NAME}
       namespace: cennso-infra
     watchNamespaces:
       - {NAMESPACE-WITH-DATABASE-INSTANCES}
     replicationSyncWaitTimeOnUpgrade: 5
     operatorNamespace: {OPERATOR-NAMESPACE}
     storage:
       classes:
         - nodePath: /opt/local-path-provisioner/mysql-cluster
           name: mysql-cluster-storage
   ```

   For more information about available configuration options, see the [Cennso Database Operator](https://docs.cennso.com/cennso/cennso-db/latest/concepts/cennso-db-operator.md) and [Cennso Database Operator Custom Resource Definition](https://docs.cennso.com/cennso/cennso-db/latest/technical-reference/configurations/cennso-db/xdboperator_definition.md) documents.

2. Apply the prepared CR to your Management Cluster:

   ```bash
   kubectl apply -f database-operator-cr.yaml
   ```

3. Check your target cluster for the ready Cennso Database Operator Pod:

   ```bash
   kubectl get pods -n {OPERATOR-NAMESPACE}
   ```

   Check the state of the `READY` column in the output. As soon as all the containers within the Cennso Database Operator Pod are ready, you can move to the deployment of the MySQL cluster.

During an upgrade of the Cennso Database Operator or MySQL instances, the operator waits five minutes, as defined by the **spec.replicationSyncWaitTimeOnUpgrade** setting, to ensure that the MySQL instances are synchronised.

> **Deprecated:** The **spec.replicationSyncWaitTimeOnUpgrade** configuration option will be removed in the next release of the Cennso Database extension. Use **spec.binaryDonor** of the `DBInstance` Custom Resource instead. For more information, see [Enable `binary-donor`](https://docs.cennso.com/cennso/cennso-db/latest/deployment-guides/binary-donor.md).

The optional **spec.storage** option creates the `mysql-cluster-storage` storage class with the `/opt/local-path-provisioner/mysql-cluster` path for the MySQL instances [data directories](https://dev.mysql.com/doc/refman/8.0/en/data-directory.html) on the nodes. The `DBInstance` CR in this guide refers to this storage class by name. To deploy more MySQL clusters with separate storage, add more entries to **spec.storage.classes**. You can omit **spec.storage** when the MySQL cluster uses a storage class that already exists on the Core Cluster, as described in the [Storage configuration](https://docs.cennso.com/cennso/cennso-db/latest/concepts/cennso-db-operator.md#storage-configuration) section of the Cennso Database Operator document.

## Deploy TLS certificates for the MySQL cluster

> [!NOTE]
> This is an optional step. If you do not need custom TLS certificates for your MySQL cluster, move directly to the [Deploy Cennso Database Instance](#deploy-cennso-database-instance) section.

By default, each MySQL cluster has the TLS certificates requested from the [cert-manager](https://cert-manager.io/). These certificates are valid for `90` days. To customise the validity time of the TLS certificates for your MySQL cluster, you can use the `DBTLSCertificate` CR. For example:

```yaml
apiVersion: db.contrib.cennso.com/v1alpha1
kind: DBTLSCertificate
metadata:
  name: {MYSQL-CLUSTER-TLS-CERTIFICATE}
  namespace: cennso-infra
spec:
  connectionSecretRef:
    name: {CORE-CLUSTER-NAME}
    namespace: cennso-infra
  namespace: {NAMESPACE-WITH-DATABASE-CLUSTER}
  duration: 26280h # 3 years
  renewBefore: 24h
```

This Custom Resource produces a Kubernetes Secret `cennso-db-{MYSQL-CLUSTER-TLS-CERTIFICATE}-ssl` in the `{NAMESPACE-WITH-DATABASE-CLUSTER}` namespace. After the Kubernetes Secret with TLS certificate and keys is created, you can set the following configuration options in the `DBInstance` Custom Resource to configure your MySQL cluster to use these TLS certificates and keys:

```yaml
spec:
  tlsSecretName: cennso-db-{MYSQL-CLUSTER-TLS-CERTIFICATE}-ssl
```

If you need to update TLS certificates for the already existing deployment of the MySQL cluster, read the [Manage TLS certificates](https://docs.cennso.com/cennso/cennso-db/latest/operation-guides/certificates.md) document.

> [!IMPORTANT]
> Any update or renewal of the TLS certificates leads to the graceful restart of the MySQL database cluster, starting from the secondary instances and ending with the primary.

## Deploy Cennso Database Instance

After you have deployed the Cennso Database Operator, you can deploy a MySQL cluster using the `DBInstance` CR.

This section shows the example of how to deploy the MySQL cluster in the Core Cluster specified by the `{CORE_CLUSTER_NAME}`. After the deployment, the MySQL cluster pods run in the Kubernetes Namespace specified in the `{DATABASE-CLUSTER-NAMESPACE}`. The MySQL cluster uses default TLS certificates and keys generated by the [cert-manager](https://cert-manager.io/), which are valid for `90` days. If you need custom validity of the TLS certificates and keys, read [Deploy TLS certificates for the MySQL cluster](#deploy-tls-certificates-for-the-mysql-cluster) and [Manage TLS certificates](https://docs.cennso.com/cennso/cennso-db/latest/operation-guides/certificates.md) documents.

> [!NOTE]
> The deployment described in this example does not contain information about:
>
> - MySQL exporter which collects metrics from the MySQL instance(s)
> - `binary-donor` which helps to maintain the MySQL instances in sync after restart
>
> To learn how to enable and configure observability for the MySQL cluster and `binary-donor`, read the [Enable metrics collector](https://docs.cennso.com/cennso/cennso-db/latest/deployment-guides/cennso-database-observability.md) and [Enable `binary-donor`](https://docs.cennso.com/cennso/cennso-db/latest/deployment-guides/binary-donor.md) documents.

To deploy a Cennso Database Instance described above, execute the following steps:

1. Ensure you have the Cennso Database Operator deployed in your cluster, and the Kubernetes Namespace you want to use for the MySQL cluster deployment is mentioned in the **spec.watchNamespaces** field of the Cennso Database Operator CR.

2. Prepare the storage for the MySQL instances. The preparation depends on the source of the MySQL instance volumes, as described in the [Storage configuration](https://docs.cennso.com/cennso/cennso-db/latest/concepts/cennso-db-instance.md#storage-configuration) section of the Cennso Database Instance document:

   - **Node-local volumes** (used in this guide). Create the directories for the MySQL instances on the nodes where you plan to deploy them. You can use the following commands to create the directories on your nodes:

     ```bash
     kubectl debug node/mynode1 -it --image=ubuntu
     mkdir -p /host/opt/local-path-provisioner/mysql-cluster/mysql-instance-0
     exit

     kubectl debug node/mynode2 -it --image=ubuntu
     mkdir -p /host/opt/local-path-provisioner/mysql-cluster/mysql-instance-1
     exit
     ```

   - **Volumes provided by the storage class.** If the storage class already exists on the Core Cluster, no preparation is needed. If the storage class is created by the Cennso Database Operator, ensure the [local-path-provisioner](https://github.com/rancher/local-path-provisioner) on your Core Cluster is configured to use the path of the storage class on the nodes where the MySQL instances can run. To do this, check the `local-path-config` ConfigMap and add the path for these nodes if it is missing. You can do this using the following command:

     ```bash
     kubectl -n local-path-provisioner get cm local-path-config -oyaml
     ```

     The output should be similar to the following, where you can see the path of the storage class for the nodes:

     ```yaml
     apiVersion: v1
     data:
        config.json: |-
           {
              "nodePathMap": [
                 {
                    "node": "DEFAULT_PATH_FOR_NON_LISTED_NODES",
                    "paths": [
                       "/opt/local-path-provisioner"
                    ]
                 },
                 {
                    "node":"node-name-1",
                    "paths":["/opt/local-path-provisioner/mysql-cluster"]
                 },
                 {
                    "node":"node-name-2",
                    "paths":["/opt/local-path-provisioner/mysql-cluster"]
                 }
              ]
           }
     ...
     ```

3. Prepare the `DBInstance` Custom Resource (CR) to deploy the Cennso Database Instance in your Management Cluster. This CR defines the connection to the Core Cluster, the storage configuration, and the settings of the MySQL instances. For example:

   ```yaml
   apiVersion: db.contrib.cennso.com/v1alpha1
   kind: DBInstance
   metadata:
     name: {DATABASE-CLUSTER-CLAIM-NAME}
     namespace: cennso-infra
   spec:
     connectionSecretRef:
       name: {CORE-CLUSTER-NAME}
       namespace: cennso-infra
     namespace: {DATABASE-CLUSTER-NAMESPACE}
     mysql:
       size: 2
       startupProbe:
         initialDelaySeconds: 15
         timeoutSeconds: 600
         periodSeconds: 10
         failureThreshold: 5
         successThreshold: 1
     storage:
       capacity: 20Gi
       className: mysql-cluster-storage
     instances:
       0:
         storage:
           node: {NODE-0-NAME}
           path: /opt/local-path-provisioner/mysql-cluster/mysql-instance-0
       1:
         storage:
           node: {NODE-1-NAME}
           path: /opt/local-path-provisioner/mysql-cluster/mysql-instance-1
     metricsExporter:
       enabled: false
     binaryDonor:
       enabled: false
       image:
         registry: registry.cennso.com/cennso/binlog-donor
         tag: v1.0.0
         pullPolicy: Always
       resources:
         requests:
           cpu: 500m
           memory: 500Mi
         limits:
           cpu: 500m
           memory: 500Mi
       env:
         - name: LOG_LEVEL
           value: debug
       serverIdBase: 70000
   ```

   This example uses node-local volumes. To use volumes provided by the storage class instead, set **spec.storage.className** to the name of the storage class and list the MySQL instances in **spec.instances** as empty entries:

   ```yaml
   spec:
     storage:
       capacity: 20Gi
       className: {STORAGE-CLASS-NAME}
     instances:
       0: {}
       1: {}
   ```

   > [!IMPORTANT]
   > The `DBInstance` **metadata.name** must not exceed `15` characters. Longer names cause the generated resource names to be silently truncated, which breaks the HAProxy Service selector and makes the MySQL cluster unreachable through HAProxy. For more information, see the [Naming constraint](https://docs.cennso.com/cennso/cennso-db/latest/concepts/cennso-db-instance.md#naming-constraint) section.

4. Apply the prepared CR to your Management Cluster:

   ```bash
   kubectl apply -f database-cluster-cr.yaml
   ```

5. Check for the ready MySQL cluster pods:

   ```bash
   kubectl get pods -n {DATABASE-CLUSTER-NAMESPACE}
   ```

   Check the state of the `READY` column in the output. As soon as all the containers within all MySQL cluster pods are ready, you can start to use the database.
