Integrate a Bitnami LAMP PHP Application with a Scalable MariaDB Replication Cluster on Kubernetes

Introduction

When moving a PHP application from development to production, one of the most important things to get right is the design and implementation of your database infrastructure (typically MySQL- or MariaDB-based). Ideally, you want a database setup that is configurable for different use cases, easy to maintain and upgrade, easy to scale and protects your data even in case of unexpected failures.

There are different options available for this:

  • One option is to use a hosted database service such as Amazon Aurora or Azure Database for MySQL. These services are easy to get started with, highly scalable and resilient to data loss, but they typically lack deep customization options, direct log access or full system privileges.

  • Another option is to "roll your own" cluster of virtual (or physical) servers and custom-configure it to meet your scalability, high availability and data protection requirements. This gives you maximum flexibility and control, but requires a significant investment of time and resources to maintain, troubleshoot and upgrade the solution.

This article introduces a third option: the Bitnami MariaDB Helm chart, which gives you a production-ready MariaDB replication cluster on Kubernetes. Kubernetes provides built-in capabilities to monitor health and recover from process/node failure and scale out depending on your decisions about usage patterns. At the same time, the Bitnami Helm chart ensures that the cluster is configured according to current best practices for security and scalability, while still allowing a high degree of customization.

This guide walks you through the process of deploying a MariaDB replication cluster on Kubernetes and then configuring a PHP application to use this Kubernetes deployment as its datastore. By relying on Kubernetes data storage services, this approach avoids a single point of failure, increases the resilience of your PHP application and makes it easier to scale up in future.

Assumptions and prerequisites

This guide makes the following assumptions:

Tip

This article uses the Bitnami LAMP stack on a cloud server, but you could also use the Bitnami MAMP stack or Bitnami WAMP stack, depending on your operating system and platform preferences.

Step 1: Deploy MariaDB on Kubernetes

The first step is to deploy MariaDB on your Kubernetes cluster. The easiest way to do this is with Bitnami's MariaDB Helm chart, which gives you a ready-to-use deployment with minimal effort and within a few minutes.

Use the commands below to deploy MariaDB on your Kubernetes cluster, remembering to replace the MARIADB-PASSWORD placeholder with a unique password for the MariaDB root account:

helm repo add bitnami https://charts.bitnami.com/bitnami
helm install mariadb bitnami/mariadb --set rootUser.password=MARIADB-PASSWORD --set slave.replicas=3 --set service.type=LoadBalancer

This command creates a four-node MariaDB cluster with one master node and three slave nodes. The cluster will be auto-configured for replication and available for access through the LoadBalancer service.

Warning

Using a LoadBalancer service type will typically assign two static IP addresses for the MariaDB deployment, one for the master and another for the slaves. Depending on your cloud provider's policies, you may incur additional charges for these static IP address.

Wait for the deployment to complete and then run the command below to obtain the IP addresses for use with MariaDB:

kubectl get svc --namespace default | grep mariadb

Here is an example of the output you should see:

MariaDB IP addresses

Step 2: Test the cluster and replication

The next step is to confirm that you are able to connect to the new MariaDB deployment from your Apache/PHP development environment (in this case, a Bitnami LAMP server).

  • Log in to the Bitnami LAMP cloud server using SSH.

  • At the Bitnami LAMP server console, use the command below to connect to the MariaDB deployment. Replace the MARIADB-MASTER-IP-ADDRESS placeholder with the IP address of the master node load balancer obtained in Step 1.

    mysql -h MARIADB-MASTER-IP-ADDRESS -u root -p
    

    Enter the password supplied at deployment-time when prompted to do so.

  • At the MariaDB prompt, use the command below to list available databases:

    SHOW DATABASES;
    

    Confirm that you see a new, empty database named my_database, which is created by the Helm chart deployment.

Once you have confirmed that your MariaDB cluster is operational, the next step is to verify data replication between the master and slave nodes. Follow these steps:

  • At the same MariaDB prompt, use the commands below to create a new database table and fill it with some sample data:

    USE my_database;
    CREATE TABLE test (id INT NOT NULL);
    INSERT INTO test (id) VALUES (21), (22);
    
  • Confirm that the table and records have been created by checking the output of the following command:

    SELECT * FROM my_database.test;
    
  • Log out of the master node:

    exit
    
  • At the server console, connect to the slave nodes. Replace the MARIADB-SLAVE-IP-ADDRESS placeholder with the IP address of the slave node load balancer obtained in Step 1.

    mysql -h MARIADB-MASTER-IP-ADDRESS -u root -p
    

    Enter the password supplied at deployment-time when prompted to do so.

  • At the MariaDB prompt, verify that the database table and records created on the master node are also available on the slave node:

    SELECT * FROM my_database.test;
    

    Here is an example of what you should see:

MariaDB replication

This implies that cluster node replication is operational and working as it should.

  • Log out once done:

    exit
    

Step 3: Integrate a PHP application with the MariaDB cluster

Now that your MariaDB cluster is deployed on Kubernetes, the final step is to connect your PHP application to it. This gives your application the benefits of Kubernetes' built-in scalability and node failure recovery features and brings it closer to production readiness.

The process to configure a PHP application for database integration varies by application but typically involves editing a configuration file and updating the database server's host name or IP address, user name, password and port. For demonstration purposes, this guide will walk you through the process of configuring a new CakePHP application.

For security reasons, you will typically begin by create a separate database for your PHP application in the MariaDB cluster and control access to it through a separate username/password combination. Follow the steps below to achieve this:

  • At the Bitnami LAMP server console, use the command below to connect to the MariaDB deployment again. As before, replace the MARIADB-MASTER-IP-ADDRESS placeholder with the IP address of the master node load balancer obtained in Step 1 and enter the password supplied at deployment-time when prompted.

    mysql -h MARIADB-MASTER-IP-ADDRESS -u root -p
    
  • Use the command below to create a new database named myapp for your application:

    CREATE DATABASE myapp;
    
  • Next, create a new user account named myapp and give it access to only the new database. Replace the USER-PASSWORD placeholder with a password for the new user account.

    GRANT ALL ON myapp.* TO myapp@'%' IDENTIFIED BY 'USER-PASSWORD';
    FLUSH PRIVILEGES;
    
  • Log out of the master node:

    exit
    

With a database configured and access granted, you can now proceed to create a new CakePHP application. The easiest way to do this is with Composer, the PHP dependency manager, which comes pre-installed on Bitnami LAMP stacks.

  • At the LAMP server console, change to the Apache server root. This is the location where you will create the CakePHP application.

    cd /opt/bitnami/apache2/htdocs
    
  • Run the command below to create a new CakePHP application:

    composer create-project --prefer-dist cakephp/app myapp
    

    Composer will download all the required components and initialize a new CakePHP application. This process will take a few minutes.

  • Browse to http://SERVER-IP-ADDRESS/myapp and confirm that you see the CakePHP default welcome page:

CakePHP welcome page

You will notice, from the previous image, that CakePHP displays an error message stating that it is unable to connect to the database. Your next step will be to resolve this error by configuring the CakePHP application with the necessary IP address and access credentials for the MariaDB cluster.

By default, CakePHP stores database configuration in the application's config/app.php file. Follow the steps below to update this configuration:

  • At the LAMP server console, edit the myapp/config/app.php file using a text editor. Locate the Datasources['default'] array and update it to look like the code snippet below. Replace the MARIADB-MASTER-IP-ADDRESS placeholder with the IP address of the master node load balancer obtained in Step 1 and the USER-PASSWORD placeholder with the password set at the end of Step 2.

    'Datasources' => [
        'default' => [
            'className' => Connection::class,
            'driver' => Mysql::class,
            'persistent' => false,
            'host' => 'MARIADB-MASTER-IP-ADDRESS',
            'username' => 'myapp',
            'password' => 'USER-PASSWORD',
            'database' => 'myapp',
            'timezone' => 'UTC',
            ...
      ]
    ]
    
  • Save the changes. Browse to http://SERVER-IP-ADDRESS/myapp again and confirm that the error previously seen on the CakePHP default welcome page is no longer present.

CakePHP welcome page

Your CakePHP application running on the Bitnami LAMP stack is now configured to work with your MariaDB replication cluster on Kubernetes. You can now continue developing and customizing your application.

WarningThe manner in which database access credentials are configured differs from one application to another. The steps described above are indicative and only valid for standard CakePHP applications. Refer to your application or framework's documentation for details on how to configure database access for your specific scenario.

Useful links

To learn more about the topics discussed in this article, use the links below: