In this lab exercise we will learn how to use OpenShift Web Console to spin up a mariadb database using templates. We will then add a frontend PHP application that accesses this database using the service exposed by the database. We will provide a route (a URL) to access the frontend application from a browser.
Step 1: Create a Project
Based on the experience gained from the previous exercise, log into the OpenShift Web Console. Click on the Home tab and select the Projects subtab. You will see the list of Projects.
Using the Create Project button, create a new project. We will call it consoleproject-UserName. Replace by your username to make this project unique. Type in a Display Name and Description of your choice.
Press the Create button to complete the project creation from the Web
Console. This will accomplish the same job as oc new-project
from CLI.
Next click on the Administrator menu and select the Developer option.
Step 2: Create a MariaDB database using the template
Click on Topology → From Catolog button
In the "Filter by keyword" field type 'mariadb'
Find MariaDB (Ephemeral) template from the list and click Select.
Click the Instantiate Template button
Edit the values to use the following values:
Database Service Name: mariadb MariaDB Connection Username: pricelist MariaDB Connection Password: pricelist MariaDB root user Password: pricelist MariaDB Database Name: pricelist
Click the "Create" button
Naviagete back to the Toplogy page and soon you will see an image of a running pod.
Step 3: Add data to MaraiaDB database
Let us see how we can enter the pod and access the database.
From the command line, change the project to the
<your_project>
using the following command:
$ oc project <your_project>
Based on what you learned previously get the list of running pods by running:
$ oc get pods
You will find one pod for mariadb running. Take a note of it’s name.
Enter the pod by running the following command. Substitute the pod name with your mariadb pod name.
$ oc rsh mariadb-1-x8hpk bash-4.2$
You will be taken to a bash prompt inside the running pod.
mysql -h127.0.0.1 -P3306 -upricelist -p
You will be taken to the MariaDB
prompt. See the list of databases. You
will notice that the sample database that you requested is added and
available.
mariadb> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| pricelist |
+--------------------+
2 rows in set (0.00 sec)
Change over to use the pricelist
database.
mariadb> use pricelist;
Database changed
Let’s us create two tables and add some records using the commands shown below.
Create the table:
create table products (
id int(11) NOT NULL auto_increment,
name varchar(32) NOT NULL,
description text NOT NULL,
price int(11) NOT NULL,
category_id int(11) NOT NULL,
created datetime NOT NULL,
modified timestamp NOT NULL default CURRENT_TIMESTAMP,
PRIMARY KEY (id)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=14 ;
create table categories (
id int(11) NOT NULL auto_increment,
name varchar(256) NOT NULL,
created datetime NOT NULL,
modified timestamp NOT NULL default CURRENT_TIMESTAMP,
PRIMARY KEY (id)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=4 ;
Now we add some data:
insert into categories VALUES(1, 'Fashion', '2014-06-01 00:35:07', '2014-05-31 09:34:33');
insert into categories VALUES(2, 'Electronics', '2014-06-01 00:35:07', '2014-05-31 09:34:33');
insert into categories VALUES(3, 'Motors', '2014-06-01 00:35:07', '2014-05-31 09:34:54');
insert into categories VALUES(4, 'Miscellaneous', '2014-06-01 00:35:07', '2014-05-31 09:34:54');
Verify that the records you added are there:
select * from categories;
Now exit
mariadb client and exit
out of the pod.
Step 4: Add a PHP frontend to talk to this database
In this step we will add a PHP frontend application that talks to recently created database. The code is available on github at https://github.com/RedHatWorkshops/php-pricelist
Browse through the code in this repository. If you take a look at the
database configuration file (config/database.php
) you will see that it reads the values from environment varables.
To add a new application click on +Add in the upper left corner of the console window.
Next click on the "From Catalog" button.
In the Filter by keyword field type PHP, then select the one titled PHP
Enter the following values for each parameters:
* Name: pricelist * Git Repository URL: https://github.com/RedHatWorkshops/php-pricelist
At the bottom of the page, in the Advanced Options section, click on Deployment.
On the Build Configuration page add the following Environment Variables to the application:
Add the following Environment variables to the application:
MYSQL_SERVICE_HOST : mariadb MYSQL_SERVICE_PORT : 3306 MYSQL_DATABASE : pricelist MYSQL_USER : pricelist MYSQL_PASSWORD : pricelist
Click Create to add this application.
Navigate back to the Topology page and you will see the new service is now available and exposed as a route.
You will also notice that a build starts running very soon. Once the build completes, the application image created from the source code will be uploaded to the docker repository.
You can check the build logs using the following command:
$ oc logs build/pricelist-1 Cloning "https://github.com/RedHatWorkshops/php-pricelist" ... Commit: 2aa50442e8432c48beedc1503cd3d05dcb834515 (Added db connection) Author: Christian Hernandez <christian.hernandez@yahoo.com> Date: Thu Jun 29 10:52:43 2017 -0700 ---> Installing application source... Pushing image docker-registry.default.svc:5000/consoleproject-christian/pricelist:latest ... Pushed 0/6 layers, 2% complete Pushed 1/6 layers, 26% complete Pushed 2/6 layers, 42% complete Pushed 3/6 layers, 56% complete Pushed 4/6 layers, 75% complete Pushed 5/6 layers, 97% complete Pushed 6/6 layers, 100% complete Push successful
Once the build completes, OpenShift initiates a deploy process. Once the deployment is complete, the frontend pod starts running.
When complete click on the Route for the application.
The application should display in the Browser. Click the +Create Record button and create a new Record.
Then click on the Read Records button to return to the main page, you should see your new record.