Build and populate a database using Kubernetes init containers

You can build and populate a database that is running in Red Hat OpenShift in a number of ways. Kubernetes init containers are one way that makes it easy to work with a new database.

This learning path, created by Don Schenck, demonstrates the use of init containers.

Step 4: Deploy the application

While not necessary, you’ll get the greatest impact from this next step if you can organize your screen (or screens) so that you can view the command line and the OpenShift dashboard (in Developer/Topology view) at the same time. Figure 2 shows  an example of this view.

Get the greatest impact from this next step if you organize your screen so you can view the command line and the OpenShift dashboard at the same time
Figure 2: View the command line and dashboard at the same time.

Deploy the application—and, by association, run the init containers—by running the following command:

oc create -f mydeploy.yaml

Here’s an example of the expected output:

> oc create -f .\mydeploy.yaml

deployment.apps/getfood created

service/getfood created

route.route.openshift.io/getfood created

When the application is up and running, you will see a dark-blue ring around the outside of the icon in your dashboard (Figure 3).

When the application is up and running, you will see a dark-blue ring around the outside of the icon in your dashboard.
Figure 3: The application is up and running.

Step 5: View the results

Click the Open URL arrow at the upper-right corner of the application icon in your OpenShift dashboard. This will open the Quarkus application in your browser (Figure 4).

When you click the Open URL arrow, it opens the Quarkus application in your browser.
Figure 4: The Quarkus application in your browser.

Append /foods to the URL to see the JSON array returned, containing every row from the database (Figure 5).

Append /foods to the URL to see the JSON array which contains every row from the database.
Figure 5: JSON array of foods.

Congratulations. You have created a database instance inside OpenShift and initialized your tables with data. Here are some notes to help you continue your learning journey.

Developer notes

Move it, remove it, improve it

Here are some ways you can move, remove and improve the skills you learned in this exercise.

Move it

You can download all of the YAML files associated with this application and use them to move it to another OpenShift instance by using the Export Application button in the upper-right corner of the OpenShift dashboard.

Remove it

You can remove parts of this activity by using the following command:

oc delete all -l app.kubernetes.io/part-of=foods

Improve it

Some ideas to improve or alter this activity:

  • Write your own back-end function in a different language.
  • Write a front-end piece
  • Create a second service that allows a search parameter

Info alert:Postgres, PostgreSQL and the Slonik Logo are trademarks or registered trademarks of the PostgreSQL Community Association of Canada, and used with their permission.

Ready to learn more?

Want to learn more about how OpenShift and Quarkus work together? Try this learning path.

Previous resource
Create the PostgreSQL instance, ConfigMap, and Deployment objects