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.

Overview: Build and populate a database using Kubernetes init containers

Introduction

Creating a database instance inside Red Hat OpenShift is as simple as one command. Building the database, tables, and initializing any tables with data is a different story. There are multiple ways to perform these important tasks, including logging into a terminal session inside a pod to running commands in a script on your local machine.

Kubernetes, the basis for OpenShift, includes init container objects, which can be used to run important steps before starting an application. These steps include creating a database, creating a table, and populating the table.

Prerequisites

You will need the following in order to complete this activity:

What you’ll be doing

This learning path covers how to prepare the database on your local machine and then instance objects in your OpenShift cluster. The result will be a PostgreSQL database instance, a database (foods), a table (food), data inside the table, and a Quarkus web service to retrieve the data (Figure 1).

A diagram of how this learning path will flow and what it will produce.
Figure 1: A diagram of how this learning path will flow and what it will produce.

Here are the steps:

  1. Get the code from GitHub.
  2. Create the PostgreSQL instance.
  3. Make the files available.
  4. Deploy the application.
  5. View the results.

How long will this activity take?

  • You should budget 45-60 minutes to complete this learning path.

What you will learn

  • When you have finished this activity, you will be able to use init containers to build and populate databases.

Programming languages

  • You don’t need to know any programming languages to complete this task. If you’re interested in viewing the code, the getfood microservice is a Quarkus application written in Java.

If you need help

  • If you get stuck, if something isn’t working, or you simply have questions, you can easily contact us via email at devsandbox@redhat.com.