Page
Deploy a database to OpenShift
OpenShift has built-in database templates for Postgres, MariaDB, and MySQL. An ephemeral instance uses no disk space, making it an easy way to test before committing to the final solution.
Prerequisites:
- Terminal session on your computer.
- A Developer Sandbox 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:
- Run one opinionated command to instantiate a Postgres database.
Get a list of templates
OpenShift has scores of built-in templates. To get a list of them, run the following command:
oc get templates -n openshift
You will see a lot of templates. The one to be used is titled postgresql-ephemeral
. If you locate it in the list, you’ll see a short description.
Get information about the postgresql-ephemeral template
When the template is used, several parameter values will be needed. To see specifics, run the following command:
oc get template -n openshift postgresql-ephemeral -o json
The following parameters and values will be used:
DATABASE_SERVICE_NAME=shorties
POSTGRESQL_USER=shorties
POSTGRESQL_PASSWORD=shorties
POSTGRESQL_DATABASE=urls
Create the database
With the parameter values listed, create the database instance by running the following command:
oc new-app --template=postgresql-ephemeral --param DATABASE_SERVICE_NAME=shorties --param POSTGRESQL_USER=shorties --param POSTGRESQL_PASSWORD=shorties --param POSTGRESQL_DATABASE=urls
Note that a route (i.e., external URL) to this is not needed. This is because only other services inside of our OpenShift cluster will be granted access to it. The only thing your services need to know is the service name, shorties. You don’t need a URL, nor do you need an IP address and port. This feature of Kubernetes that allows you to access a service by name alone is known as "service discovery".
Wrapping up
You now have a front-end website and a database. They will be connected by the two remaining backend services yet to be implemented: urlshortener-back
and urlshortener-redirector
.