Let’s carry on with The Kitchen Cake with OpenShift and bake up an application that showcases a multi-container tiered architecture with persistent storage. Web client PHP application calls REST service calls a Database. All on a local workstation environment. Yum, just ask Milky. This lab is another in the OpenShift MiniLabs series.
Objective
This lab will step you through setting up the Cat-of-the-Day https://bitbucket.org/emergile/cotd.git application to take the shape as per the schematic below. Incidentally, doing so will also showcase the capability to setup various frameworks and tools and services in a local lab environment within OpenShift (without using a virtual machines) which remains isolated from interfering with your local Operation System configuration. And save the accompanying OpenShift system state as a named profile which can be recovered later.
Setup
Initial Attempt
This tutorial assumes you have completed the OpenShift MiniLabs installation procedure. Then refresh before continuing.
Repeat Attempt
To reset your environment to repeat this tutorial do the following:
$ cd ~/containersascode $ ./oc-cluster-wrapper/oc-cluster up containersascode $ oc login -u developer - p developer $ oc delete project client $ oc delete project rest $ oc login -u system:admin $ oc delete pv cotdvolume $ oc delete pv mysqlvolume $ rm -rf ~/containersascode/volumes/cotdvolume $ rm -rf ~/containersascode/volumes/mysqlvolume
Instructions
Create the COTD Client Web Application
$ cd ~/containersascode $ oc login -u developer -p developer $ oc new-project client --display-name='Client' --description='Client' $ oc project client $ oc new-app --name='cotd' -l name='cotd' php~https://bitbucket.org/emergile/cotd.git $ oc expose service cotd --name=cotd -l name='cotd' $ oc status
Create COTD Persistent Volume
We are going to map the images directory to a persistent local store . In this way we can easily change and add images to our item list. So let’s first create the pv needed. Replace the $HOME by your fully expanded path name, e.g. /usr/johndoe.
$ cd ~/containersascode $ oc login -u system:admin $ oc get pv $ oc create -f - << EOF! apiVersion: v1 kind: PersistentVolume metadata: name: cotdvolume spec: capacity: storage: 1Gi accessModes: - ReadWriteOnce - ReadWriteMany persistentVolumeReclaimPolicy: Recycle hostPath: path: $HOME/containerascode/volumes/cotdvolume EOF! $ oc get pv
Set Up the COTD Volume Claim
$ oc login -u developer -p developer $ oc project client $ oc set volume dc/cotd --add \ --overwrite \ --name=images \ --type=persistentVolumeClaim \ --mount-path=/opt/app-root/src/data/images \ --claim-size=100Mi \ --claim-name=cotdclaim \ --containers=cotd $ cd ~/containersascode/cotd/data/images $ ls -lasi $ cp -r * ~/containersascode/volumes/cotdvolume
Verify cotd Persistence
Visit the local file system location of your cotdvolume and then change a few of the, e.g. cat images. Verify the change is reflected as expected when stepping through the items at: http://cotd-client.127.0.0.1.xip.io/item.php
Create MySQL Persistent Volume
We are going to back the CakePHP MySQL service with a persistent storage claim. So let’s create the pv needed. Replace the $HOME by your fully expanded path name, e.g. /usr/johndoe.
$ cd ~/containersascode $ oc login -u system:admin $ oc get pv $ oc create -f - << EOF! apiVersion: v1 kind: PersistentVolume metadata: name: mysqlvolume spec: capacity: storage: 1Gi accessModes: - ReadWriteOnce - ReadWriteMany persistentVolumeReclaimPolicy: Recycle hostPath: path: $HOME/containerascode/volumes/mysqlvolume EOF! $ oc get pv
Create the Items REST Service
Let’s create a CakePHP REST application using a persistent mysql template. The template includes the instructions to claim the volume.
$ oc login -u system:admin
$ oc project openshift
$ docker pull registry.access.redhat.com/rhscl/php-70-rhel7
$ docker pull registry.access.redhat.com/rhscl/mysql-57-rhel7
$ oc import-image php:7.0 --from=registry.access.redhat.com/rhscl/php-70-rhel7 --confirm
$ oc import-image mysql:5.7 --from=registry.access.redhat.com/rhscl/mysql-57-rhel7 --confirm
$ cd ~/containersascode
$ oc login -u developer -p developer
$ oc new-project rest --display-name='Items REST Service' --description='Items REST Service'
$ oc project rest
$ oc create -f https://bitbucket.org/emergile/items/master/openshift/templates/cakephp-mysql-persistent.json
$ oc create -f https://raw.githubusercontent.com/openshift/cakephp-ex/master/openshift/templates/cakephp-mysql-persistent.json
$ oc new-app --template=cakephp-mysql-persistent -p NAME='items' -p SOURCE_REPOSITORY_URL=https://bitbucket.org/emergile/items.git -p DATABASE_USER=user -p DATABASE_PASSWORD=password
$ oc status
Populate the MySQL Database
$ cd ~/containersascode
$ oc login -u developer -p developer
$ oc project rest
$ export PODID=`oc get pods -n rest --no-headers=true | grep mysql | cut -f 1 -d ' '`
$ oc port-forward $PODID 3306:3306
Now launch your favourite database administration tool, such as e.g. MySQL Workbench and create a new connection with attributes as per below. Click to connect and you are good to go.
Name: Items Connection method: Standard (TCP/IP) Hostname: 127.0.0.1 Port: 3306 Username: user Password: password
You can then create the items table and populate the database by a copy/paste of the sql instructions as supplied at: https://bitbucket.org/emergile/items/tree/master/etc.
Verify MySQL Persistence
Go the https://127.0.0.1:8443/console/project/rest/overview page and scale down and scale up the mysql container. Then visit the REST CakePHP page for Items at http://items-rest.127.0.0.1.xip.io/ and verify that the database remains populated as before.
Wire up cotd to items
The cotd application will look for a REST service as a data source if the environment variable SERVICE is set (refer data/rest.php). SERVICE takes the value of the IP address of the items REST service. In our case it will be a 172.x.y.z address as this local cluster instance does not have a DNS service defined. Update the environment variable as follow which will trigger a redeploy of cotd. ( In an upcoming MiniLab we will revise this design element to leverage a microservice framework. )
$ cd ~/containersascode
$ oc login -u developer -p developer
$ export PODID=`oc get service -n rest --no-headers=true | grep items | cut -c 11-25`
$ oc project client
$ oc env dc/cotd SERVICE=$PODID SELECTOR=cats
Verify Success
Using the REST CakePHP tool at: http://items-rest.127.0.0.1.xip.io/ change around the ranks of two of the items, e.g. swap around ranks for items 1 and 5. Now click the Home button back at: http://cotd-client.127.0.0.1.xip.io/item.php and verify that the ordering has changed as expected.
Trivia
For the latest in OpenShift developments keep checking https://blog.openshift.com. A list of curated OpenShift resources can be inspected at OpenShift Resources