oci-templates

Get Started with Bitnami Terraform Templates on Oracle Cloud Infrastructure

Introduction

Oracle Cloud Infrastructure offers a collection of cloud services designed to manage applications, services, and databases, in a high-performance cloud computing environment. Its architecture provides the flexibility to scale. In addition to this, Oracle Cloud Infrastructure offers the highly durable storage of public cloud combined with the security, control access, and cost-effectiveness of on-premise infrastructure.

If you’re new to the cloud, the easiest way to get started with Oracle Jump Start Launch is with Bitnami. Bitnami provides pre-packaged application templates for Oracle Jump Start Launch cloud servers, so you can get productive with your new server immediately. Access and launch these images using Terraform, which provides a command-line interface (CLI) to deploy and manage your Oracle Jump Start Launch servers.

This tutorial walks you through the process of using the Terraform Provider plugin and the Terraform CLI to deploy Bitnami MySQL with replication on an Oracle Jump Start Launch server. Find other Terraform Templates for OCI at the GitHub Bitnami repository.

TIP: To learn more about launching servers on Oracle Cloud Infrastructure using Qloudable, refer to this tutorial.

Overview

MySQL has proved to be a fast, reliable, scalable, and easy-to-use open-source relational database system and is one of the most popular databases used by both companies and individuals. MySQL is also used as a component of several renowned applications such as WordPress, Magento, and Redmine. Bitnami offers a ready-to-use MySQL with Replication cluster that allows you to start writing in the master node and has the information synchronized in slaves from the very moment of the deployment.

In this tutorial, you will learn, step by step (using the MySQL Terraform Solution as an example), the complete process of deploying an image and all the required resources on an Oracle Jump Start Launch server using a Terraform template. That way, once the solution has been deployed, you won’t only have the application image running on a server but also the volumes, instances, and network resources needed to operate with a MySQL production-ready cluster.

The following are the steps you’ll complete in this tutorial:

  • Download and install Terraform CLI and the Terraform Provider for OCI
  • Obtain the Bitnami MySQL Terraform Solution and associate it with your Oracle Jump Start Launch account
  • Deploy the Bitnami MySQL Terraform solution on an Oracle Jump Start Launch server
  • Add new nodes to the cluster
  • Operate with the cluster
  • Delete the cluster

NOTE: This guide will show you the steps to deploy a Bitnami Terraform Solution on a Linux operating system.

Step 1: Download and install Terraform CLI and the Terraform Provider plugin for OCI

At the end of this step, you will have installed both the Terraform CLI and the Provider plugin on your local system.

To complete the Bitnami deployment it is necessary to have the Terraform CLI and the Provider plugin installed to work with Oracle Jump Start Launch.

To install the Terraform CLI:

  • Use Terraform official documentation to download the binary package that corresponds to your operative system. Unzip and move it to the /usr/local/bin/ directory of your local system, in case you are using macOS or Linux.

    $ sudo mv terraform /usr/local/bin/
    
  • Download and install the latest release of the Oracle Jump Start Launch provider for Terraform from its GitHub repository. Binary packages are available for Linux, OS X and Windows. Unzip it and create a folder named ~/.terraform.d/plugins. Then, move the binary to that folder as shown below (X.Y.Z are placeholders of the latest version of the provider):

    $  mkdir -p ~/.terraform.d/plugins
    $  mv terraform-provider-oci_v.X.Y.Z ~/.terraform.d/plugins
    

Step 2: Obtain the Bitnami MySQL Terraform Solution and associate it with your Oracle Jump Start Launch account

At the end of this step, your MySQL Terraform Solution will be associated with your OCI account.

Once you have installed the Bitnami Terraform template on your local system, it is necessary to associate the template to your Oracle Jump Start Launch account. That way, when you deploy the solution using the Terraform CLI, it creates a server on OCI in your tenant and in the selected compartment. Follow these instructions:

  • To obtain the Bitnami MySQL Terraform template, clone the bitnami/oci-multi-tier repository as shown below:

    $ git clone https://github.com/bitnami/oci-multi-tier
    $ cd oci-multi-tier/mysql
    

IMPORTANT: All the steps must be performed within the mysql directory.

The next steps are to retrieve OCI account parameters and to associate them with the Bitnami MySQL Terraform Solution. Find instructions below.

Retrieving OCI account parameters

Once the bitnami/oci-multi-tier repository is installed on your local system, you need retrieve the values for the following parameters to associate your OCI account with the Bitnami MySQL Terraform Solution.

  • TF_VAR_tenancy_ocid: OCI tenant ID
  • TF_VAR_user_ocid: OCI user ID
  • TF_VAR_fingerprint: PEM key fingerprints
  • TF_VAR_private_key_path: Path of your private key (*.pem / *.ppk)
  • TF_VAR_region
  • TF_VAR_compartment_id: OCI Compartment ID
  • TF_VAR_ssh_public_key_path and TF_VAR_ssh_private_key_path: Paths to the private and public (*.pub) SSH keys.

Follow these instructions:

  • To obtain the values for the TF_VAR_tenancy_ocid and the TF_VAR_region parameters, click the “Tenancy” link in the top menu.

    Click Tenancy

    • In the “Tenancy” screen, find these values in the “Tenancy Information” section:

      Obtain Tenancy OCID and Region

  • To obtain the values for both the TF_VAR_user_ocid and the TF_VAR_fingerprint parameters, navigate to the “User Settings” section. On the resulting page, you will find the required values:

    User settings

  • To obtain the value for the TF_VAR_compartment_id parameter, navigate to the “Identity -> Compartments” section, and select the compartment OCID where you want to deploy the solution:

    Obtain Compartment OCID

  • You can obtain the values for both the TF_VAR_ssh_public_key_path and the TF_VAR_ssh_private_key_path parameters when creating and uploading the public SSH key to the console. Follow the Oracle official documentation for further information about this.

Associate your OCI account with the Bitnami MySQL Terraform Solution

The last step consists of replacing the values you have obtained for these parameters in the env-vars file:

Edit the env-vars file

Step 3: Deploy the Bitnami MySQL Terraform Solution on an Oracle Jump Start Launch server

At the end of this step, your MySQL Terraform Solution will be running on multiple instances on the Oracle Jump Start Launch console.

The next step is to initialize the Terraform working directory, configure, and deploy the MySQL cluster. Follow these instructions:

  • To start the Terraform CLI and the Provider plugin, execute:

    $ terraform init
    

    It will show an output similar to this:

    Initialize Terraform

  • To apply the changes made in the env-vars file, execute this command:

    $ source env-vars
    
  • Deploy the cluster running this command:

    $ terraform apply
    
  • You will be prompted to specify the cluster configuration details. Enter the following information when it is displayed:

    IMPORTANT: Database and deployment name should include neither dashes nor spaces.

    • Name of the application database
    • Name of the deployment
    • Size of the instance
    • Number of instances to deploy
    • Size of the data volume in GBs

    NOTE: In case you don’t want to enter the cluster configuration values during the deployment, add them previously to the env-vars file. Find the correspondent variable name for each value in the variables.tf file.

The deployment takes about 10 minutes. Once the deployment has finished, you will see an output similar to this:

Deployment finished

As you can see above, this output gives you essential information about your deployment such as the number and name of the nodes or the public IP addresses for each. Another way to retrieve this information is to check each instance in the “Compute -> Instances -> Instance Information” section in the OCI console.

To check the progress of each resource in the OCI console, select your compartment and navigate to the following tabs on the OCI console:

  • “Compute -> Instances”:

    Check instances on the OCI console

    IMPORTANT: Take into account that the public IP address shown in the “Compute -> Instances -> Instance Information” section won’t be available until the entire deployment has finished.

    Check instance details on the OCI console

  • “Networking -> Virtual Cloud Networks”:

    Check networks on the OCI console

  • “Storage -> Block Volumes”:

    Check volumes on the OCI console

Understand the MySQL cluster configuration

The MySQL Terraform Solution deploys multiple virtual machines to replicate the databases from the master node to a configurable number of replicas. Replication in MySQL supports different types of synchronization between master and slaves. Bitnami configures the MySQL cluster using one-way synchronization and asynchronous replication. This means that one server acts as the master, while one or more other servers act as slaves.

That way, it allows you to start writing in the master node and have the information synchronized in slaves from the very moment of the deployment. This topology is illustrated below:

Bitnami MySQL Terraform Solution topology

Step 4: Add new nodes to the cluster

At the end of this step, you will have added a new node to the cluster.

Scaling up your cluster is really easy. You just only need to execute the command below and indicate the total number of nodes you want the cluster to have. In this example, we have added just one node to the default three-node cluster, so you will see four nodes in total: one master and three slaves.

    $ terraform apply -var nodes_count=TOTAL_NODES

The new deployment will take several minutes to finish. Please don’t take any action during this process.

Once it has finished, you will see an output similar to the image below. Note that the number of resources created is less than those in the first deployment. This is because Terraform reuses the already created resources and adds only the resources required for the new nodes.

Add nodes to the cluster

Also check the new nodes in the “Compute -> Instances” section in the OCI console:

Add nodes to the cluster

Step 5: Operate with the cluster

Finding my application password

There are two ways to configure the application password: Either provide a specific password before deployment, or have it generated automatically and retrieve it after deployment.

  • To set a specific password, add the following line to the env-vars file. Remember that PASSWORD is a placeholder, replace it with the password you want to set to the MySQL database:

    app_password = "PASSWORD"
    

    In case you didn’t provide a password before the deployment, it will be randomly generated. This is the password for user root. For security reasons, the application password is not displayed in the output of the deployment. Instead, you can see it labeled as “sensitive”:

    Sensitive password

  • To see the application password, execute the following command:

    $ terraform output ApplicationPassword
    
  • To check the information related to deployment execute the following:

    $ terraform output
    

    That way, you will not only obtain the value for the application password, but also the private and public IP addresses of each node. Execute this command any time you need to obtain that information:

    Password shown

Connecting to the MySQL cluster through SSH

Once you have obtained the public IP address and the application password, you are ready to connect to your MySQL cluster. For security reasons, the Bitnami MySQL solution is not configured to accept remote connections. To connect to the cluster, connect to the server through SSH. Follow the steps below:

TIP: Remember that to display the cluster information execute the terraform output command at any time.

  • To connect to the server through SSH, execute the following command:

    $ ssh -i KEYFILE bitnami@NODE-IP
    

    Remember to replace KEYFILE in the previous commands with the path to your private key file (.pem), and NODE-IP with the public IP address or hostname of the node you want to connect.

  • To connect to the MySQL node, execute the following (in this example, the master node):

    $ mysql -u root -p
    

    You will see an output similar to this:

    Connect to MySQL

  • To see the information related to your MySQL database, execute the show databases command:

    mysql> show databases;
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | bitnamidb          |
    | mysql              |
    | performance_schema |
    | sys                |
    +--------------------+
    5 rows in set (0.00 sec)
    

Opening ports

If you need to access your server remotely using a specific port, you must first open the port. This operation must be executed in each node of the cluster. To do so:

  • Open the network.tf file and add new content to the “ingress_security_rules” section. Remember to replace PORT with the number of the port you want to allow remote connections. This should look like the code block below:

    ingress_security_rules = [
        {
        #Allow 22 from everywhere
        source   = "0.0.0.0/0"
        protocol = "6"
        tcp_options {
        min = 22
        max = 22
        }
        },
        {
        # Allow PORT from everywhere
        source   = "0.0.0.0/0"
        protocol = "6"
    
        tcp_options {
        min = PORT
        max = PORT
    }
    },
    ]
    
  • Execute the terraform apply command to make the changes take effect:

    $ terraform apply
    
  • Connect through SSH to the node in which you must perform the action.

  • Execute the following command for each port that you wish to open. Remember to replace the PORT placeholder with the corresponding port number:

    $ sudo firewall-cmd --zone=public --permanent --add-port=PORT/tcp
    
  • Reload the firewall by running this command:

    $ sudo firewall-cmd --reload
    

Step 6: Delete the cluster

At the end of this step, your MySQL cluster and the associated resources will be deleted.

In case you want to delete the cluster, you only need to execute the destroy command as shown below:

$ terraform destroy

When executing the command above, you will be prompted to re-enter all the values of the cluster you want to delete. At the end of the process, to confirm the action, type “yes” and press the “Enter” key.

Destroy the cluster

Terraform will confirm that all resources have been successfully deleted:

Destroy the cluster

To learn more about the topics discussed in this tutorial, see the links below: