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.
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:
- An account on the Redhat Openshift platform. You can visit the previous article to understand how to create the account.
- 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.
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.
Step- 3: Click on From Catalog(third) tile visible on the dashboard for the developer. It should appear something like this.
Step- 4: On this page, type mysql in Filter by keyword search box, and click on Databases in the left side.
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).
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.
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.
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.
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.
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
Step- 4: If the password is correct, it should display something like in the image below.
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.
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.
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.
- 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.
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.
5. Once the oc expose service
command is executed, you can return to the web console, and the route is available to be used.
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.
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!