Page
Deploy a new microservice from an existing image and connect all of the services
In this lesson, you will create a microservice from an existing image, then link it with other services by setting environment variables for the deployment.
Prerequisites:
- Terminal session on your computer.
- A Developer Sandbox for Red Hat OpenShift account.
- OpenShift
oc
CLI installed on your computer (Getting started with the OpenShift CLI - OpenShift CLI (oc) | CLI tools | OpenShift Container Platform 4.16).
In this lesson, you will:
- Create the service
urlshortener-redirector
service using an existing image. - Link all of the URL Shortener application pieces by setting environment variables.
Create the redirection service
Existing images stored in registries can be used to create apps in OpenShift. Run the following command to create the service urlshortener-redirector
in your OpenShift cluster:
oc new-app --name=urlshortener-redirector quay.io/rhdevelopers/urlshortener-redirector:latest
Run the following command to make the redirector available to the frontend website:
oc expose svc/urlshortener-redirector --port=8080
Link all of the URL Shortener application pieces
Run the following command to set the environment variable for the redirector so it can connect to the Postgres database service:
oc set env deploy/urlshortener-redirector POSTGRES_SERVER=shorties
Run the following command to get the route to the redirector service:
oc get routes
Using the URl for the route name urlshortener-redirector
, prefix with the https://
scheme and run the following command so the front-end website can connect to the redirector service:
oc set env deploy/urlshortener-front REDIRECTOR_URL=http://$REDIRECTOR_SERVICE_ROUTE_URL
Here’s an example:
oc set env deploy/urlshortener-front REDIRECTOR_URL=http://urlshortener-redirector-rhn-engineering-dsch-dev.apps.sandbox-m3.1530.p1.openshiftapps.com
Now the application is completed with all layers up and running (Figure 1).
Wrapping up
One database, one website, two services. Up and running in OpenShift. The concepts demonstrated here included deploying a database from template, deploying an application from source code, deploying an image from an image registry, and updating environment variables at the command line. These skills will allow you to deploy your own full-stack JavaScript (and other language) applications to your own Red Hat OpenShift cluster.
Note: The urlshortener-redirector
app is written in Go. As a further self-learning opportunity, you can write the application in Node.js, create an image, and replace the app running in your OpenShift cluster.