[PYTHON] [Code Pattern] How to deploy a sample currency app to OpenShift on IBM Cloud using Red Hat Universal Base Image (UBI)

Hello everyone. This is Aya Tokura.

This time, it's published on the ** IBM Developer ** site as ** Code Pattern ** ["Deploy a sample exchange app to OpenShift using Red Hat Universal Base Image. ]](Https://developer.ibm.com/jp/patterns/deploy-to-openshift-4-redhat-universal-base-image/) **, with operating procedures and additional comments as a Japanese reference translation I will also introduce it.

Overview

This article covers the steps to deploy a sample exchange application written in Python on Red Hat OpenShift on IBM Cloud using Red Hat Universal Base Image (UBI). After completing this operation, you will be able to acquire the following skills.

What is Red Hat Universal Base Image (UBI)?

An image of an operating system for containers with Red Hat Enterprise Linux (RHEL) as the core. In other words, for users who are already using RHEL, it is possible to build an image that combines quality. You can create an image on any platform, redistribute it at will, and run it wherever you like. For more information, please visit the official Red Hat website.

important point

Use a Red Hat OpenShift cluster on IBM Cloud as your cloud environment. If you have obtained an Open Shift on IBM Cloud environment for your workshop, such as at an IBM Developer Dojo OpenShift seminar, you can use it.

Advance preparation

You need to have the following tools and environment ready. -Docker: Environment where Docker can be executed locally --Docker Hub account --IBM Cloud Account-- Pay-As-You-Go (PayG) --Red Hat OpenShift 4 clusters on Red Hat OpenShift on IBM Cloud

Overall flow image

1. Within the sample currency exchange microservice, a consumer of the client API calls the microservice over the Internet. (http (s) request) 2. The Python Flask process acts as a `web server` and accepts REST requests (eg GET / convertCurrency / ZAR / USD / 600.66). 3. The code that routes the request in Flask passes the request to the service module, which calls the External European Currency Exchange API (https://api.exchangeratesapi.io/). 4. For example, the South African Rand (ZAR) exchange rate is acquired and stored. The ZAR value of 600.66 is converted to US dollars (USD). 5. Flask responds by sending a dollar amount ($ 40.59 in this example) to the calling consumer.

Operating procedure

1. Clone the GitHub repository locally

  1. Execute the following command from the terminal to clone the sample application published on GitHub.
git clone https://github.com/IBM/python-ubi-openshift.git
  1. When the clone is completed, move the work location with the following command.
cd python-ubi-openshift

2. Build and run the Docker image locally

  1. For <your-dockerhub-username>, specify your own DockerHub user name and build and execute the Docker image locally.

** For Mac OS **


export DOCKERHUB_USERNAME=<your-dockerhub-username>

docker build -t $DOCKERHUB_USERNAME/currencyexchange-py:v0.0.1 .

** For Windows **


SETX DOCKERHUB_USERNAME "your-dockerhub-username"

docker build -t $DOCKERHUB_USERNAME/currencyexchange-py:v0.0.1 .
** Example of output contents after command execution **
Sending build context to Docker daemon  69.63MB
Step 1/10 : FROM registry.access.redhat.com/ubi8/ubi
 ---> fd73e6738a95

 ...

Collecting flask (from -r requirements.txt (line 13))
  Downloading https://files.pythonhosted.org/packages/9b/93/628509b8d5dc749656a9641f4caf13540e2cdec85276964ff8f43bbb1d3b/Flask-1.1.1-py2.py3-none-any.whl (94kB)

 ...

Successfully built 3b5631170697
Successfully tagged <DOCKERHUB_USERNAME>/currencyexchange-py:v0.0.1

  1. Actually run the built image locally.

docker run -p 7878:7878 $DOCKERHUB_USERNAME/currencyexchange-py:v0.0.1

You can verify that the Forex microservices docker container is running by opening another terminal and running the docker ps command. 3. Access http://127.0.0.1:7878/ from your browser and try the API call from try-it-out. 4. After confirming the operation, press [Control] + [C] at the same time in the terminal to finish.

3. Deploy to an OpenShift 4 cluster

In order to allow changes to this microservice, you need to create a repository in the Docker Cloud (https://hub.docker.com/) so that you can push the newly modified container.

  1. Execute the following commands in order to build the docker image. For <your-dockerhub-username>, specify your Docker Hub username.

export DOCKERHUB_USERNAME=<your-dockerhub-username>

docker build -t $DOCKERHUB_USERNAME/currencyexchange-py:v0.0.1 .

docker login
  1. Then push the image to docker hub.
docker push $DOCKERHUB_USERNAME/currencyexchange-py:v0.0.1

From here, use the oc command to deploy to OpenShift. * Recommended after v4.1.0

** How to check the version of installed oc cli **

oc version
  1. Log in to ** IBM Cloud Dashboard ** from your browser.

  2. Go to your OpenShift cluster page and click the ** [OpenShift web console] ** button.

  3. Click ** [Copy Login Command] ** from the drop-down menu next to your username in the upper right corner of the OpenShift console to get information to log in to OpenShfit from the command.

Please handle the displayed information carefully. This time, copy the command listed under ** [Log in with this token] ** and execute it from the terminal. ▼ Example of displayed credential page 6. Create a new project with the oc command.

oc new-project currencyexchange-py
  1. Execute the following command to check if the project has been created.

oc project
  1. Next, add the application.

oc new-app $DOCKERHUB_USERNAME/currencyexchange-py:v0.0.1

▼ Example of displayed output contents In the yellow highlight section, you can see that ** Red Hat Universal Base Image ** is the base image for your Docker deployment.

  1. Execute the following command to expose the microservice to the outside.

oc expose svc/currencyexchange-py
  1. Check the public URL with the oc status command. The route is now exposed to the outside world and will be visible when you access the URL via the public internet.

oc status

▼ Example of displayed output contents The highlighted part is the URL information that is publicly available.

▼ Example of a site displayed by accessing the URL 

4. Microservices performance and health monitoring

You can use the ** OpenShift Web Console Dashboard ** to check the status of your microservices at any time.

You have now successfully created a Python Flask microservices container image using UBI and optionally deployed it to a RedHat OpenShift 4 cluster on IBM Cloud. This time is over. Thank you for your hard work.

Reference information

★ The latest information is being delivered on Twitter @ayatokura

Recommended Posts

[Code Pattern] How to deploy a sample currency app to OpenShift on IBM Cloud using Red Hat Universal Base Image (UBI)
How to code a drone using image recognition
How to deploy a Django application on Alibaba Cloud
How to deploy a Django app on heroku in just 5 minutes
How to deploy a web application on Alibaba Cloud as a freelancer
How to make a Python package using VS Code