Creating, deploying and exposing a MySQL container on Openshift

Creating, Deploying and Exposing a MySQL Database container on Redhat Openshift

We’ll learn how to deploy a MySQL container on Redhat’s Openshift along with the creation of tables and databases.

Pratik Singhal
Fnplus Club

--

MySQL + Openshift

In the previous article, we deployed a spring-boot application on the Openshift platform. In this article, we’ll understand how to easily create a working MySQL container within 2 minutes without any hassles.

Pre-requisite:

  1. An account on the Redhat Openshift platform. You can visit the previous article to understand how to create the account.
  2. A project created in the web-console after logging inside the console.
    Note: To log in the console, use your RedHat email-id and password as credentials and you can easily log into the web console.

With the pre-requisites being satisfied, let’s begin.

Deploying a MySQL container:

Step- 1: Click on the project you just created. Make sure the role is selected as Administrator as shown in the image below.

The red circled part should show the Administrator. If it doesn’t, just click on it and select Administrator.

Step- 2: After the project opens up in the web console, change the account/user type from Administrator to Developer, from the top-left drop-down button.

This is how your screen looks like when you select the role as Developer.

Step- 3: Click on From Catalog(third) tile visible on the dashboard for the developer. It should appear something like this.

The Catalog screen.

Step- 4: On this page, type mysql in Filter by keyword search box, and click on Databases in the left side.

Search for mysql and click on Databases.

Step- 5: Click on the MySQL tile from the grid of tiles that appears and click on Instantiate Template.
Note: Do not select Ephemeral, select the normal one (third tile in the image above).

Click on Instantiate Template.

Step- 6: Provide a connection username, connection password, mysql root password(optional, you can leave it empty), and a database name of your choice and click on Create.
Note: Connection username and password will be used to connect to mysql service in the terminal. Please remember it or jot it down.

Provide details like username, password, database name and root password.

Step- 7: On the screen that appears, click on Topology from the left panel. Then click on the big icon that appears at the centre of the screen. It should open up a panel from right-side where you can see a pod being created and running.

MySQL container is deployed.

Now, we have successfully deployed a MySQL container, let us create the database and table as per our requirement.

Creating Database and Tables:

Step-1: Click on the pod that is created under the Pods section in the panel.
Example: For me the pod name is mysql-1-jp7rd. Pod names are randomly generated by Openshift, hence it can be different for you. Nothing to worry about.

This screen appears after you click on the pod.

Step- 2: Click on Terminal tab from the Tabbar as visible on the screen. It should display a terminal something like in the image below.

The Terminal tab.

Step- 3: Type the following commands inside the terminal meanwhile replacing the values of username and password. Replace pratik with the connection username you provided and press enter. It’ll prompt you for a password. Provide the connection password that you entered while creating the container and press enter.
Note: While typing the password, no characters/ asterisks will be visible since it is a Linux based terminal. Just type the password and press Enter.

sh-4.2$ mysql -u pratik -p
Type the password and press Enter.

Step- 4: If the password is correct, it should display something like in the image below.

On successful authentication, the mysql terminal becomes accessible.

Step-5: Now, we will use the database created, and create tables for the database.
Note: If you forget the name of your database, you can simply type show databases; and it’ll display all the databases available in your system.

show databases command shows the databases available in the system.

To use the database type use trialdb;. Replace trialdb by the name of your database. This makes sure that the table you create will be inside your desired database.

I have created a sample table as shown in the image below after changing my database.

Created a sample table inside trialdb.

After finishing all the SQL queries, type exit to exit from the MySQL console.
With this, we have successfully created and deployed a MySQL container on RedHat Openshift Platform.

Creating a route to expose the MySQL service:

Now, there can be situations when we don’t want to have other containers in the same environment as the database environment. Hence, we need to expose a route through which this database service can be accessed on the internet.

To expose this service, we need the OC CLI tool installed in the system.
Here’s the link to download OC CLI tool for Windows, Linux, Mac.

Once the OC CLI tool is set up in your path, do the following steps carefully.

  1. Open the web console, and click at the top right corner where you can see your name and from the drop-down that opens, select Copy Login Command option.

2. A new tab opens up, which displays a hyperlink Display token. Click on the hyperlink, and it displays your API Token and two different commands.
Copy the command under the section Login with this token.
Note: I cannot show the token and the command for security reasons. But the command starts with “oc login ..”

3. Open a terminal/ command line on your local system and just paste the command that you copied. On successful login, it’ll log you in to the web console through CLI. Now select the project using oc project <project-name> command.

This command is only necessary only if you multiple projects in your web console.

4. Now after successful login, type the following to expose a route for your service.
oc expose service <service-name>
To confirm the service name, check under the Services section, as shown in the image below.

The service name is mysql for my case.

5. Once the oc expose service command is executed, you can return to the web console, and the route is available to be used.

The URL under the Routes section can now be used to access database service outside the container env.

6. Now to create a communication channel between this container and other containers in the same environment, we need to find the cluster IP for the MySQL container. To get the cluster IP, return to the OC CLI tool, and type the following: oc get svc . This would list down the running services in your Openshift environment. Since we need the MySQL service cluster IP, we’ll find the cluster IP corresponding to the service name of the mysql.

Cannot show the IP because of security reasons.

7. Copy the cluster IP and it can be used to access MySQL service from other containers in the same environment.
Practical example: Accessing the MySQL container in Springboot application
spring.datasource.url=jdbc:mysql://<cluster-ip>:<port>/<database name>

And with this, we have successfully, created, deployed and exposed a MySQL service within a short time.

Hope this article helps. Feel free to comment on doubts.
And hey! Do you know you can clap for an article 50 times? Damn, that’s awesome!

--

--

Pratik Singhal
Fnplus Club

Fluttering all the way in to tech world | Flutter Enthusiast | Final Year Engineering Student at S.R.M. I.S.T. | Always Available |