Setting up MySQL as pods in Red Hat OpenShift
Many applications require a relational database as a backend. The steps here will show you how to spin up and wipe out a MySQL database using Red Hat OpenShift.
Several databases can be hosted in OpenShift, including Microsoft SQL Server, Couchbase, MongoDB, and more. Today we'll use and set up MySQL. The concepts will be the same for other databases.
MySQL
Required: OpenShift 4.x up and running and have logged in
List all available templates
oc get templates --namespace openshift
PS C:\Users\suyash.sambhare> oc get templates --namespace openshift
NAME DESCRIPTION
PARAMETERS OBJECTS
cache-service Red Hat Data Grid is an in-memory, distributed key/value store.
8 (1 blank) 4
cakephp-mysql-example An example CakePHP application with a MySQL database. For more information ab... 21 (4 blank) 8
cakephp-mysql-persistent An example CakePHP application with a MySQL database. For more information ab... 22 (4 blank) 9
dancer-mysql-example An example Dancer application with a MySQL database. For more information abo... 18 (5 blank) 8
dancer-mysql-persistent An example Dancer application with a MySQL database. For more information abo... 19 (5 blank) 9
datagrid-service Red Hat Data Grid is an in-memory, distributed key/value store.
7 (1 blank) 4
django-psql-example An example Django application with a PostgreSQL database. For more informatio... 19 (5 blank) 8
django-psql-persistent An example Django application with a PostgreSQL database. For more informatio... 20 (5 blank) 9
eap-xp3-basic-s2i Example of an application based on JBoss EAP XP. For more information about u... 20 (5 blank) 8
eap-xp4-basic-s2i Example of an application based on JBoss EAP XP. For more information about u... 20 (5 blank) 8
eap74-basic-s2i An example JBoss Enterprise Application Platform application. For more inform... 20 (5 blank) 8
eap74-https-s2i An example JBoss Enterprise Application Platform application configured with... 30 (11 blank) 10
eap74-sso-s2i An example JBoss Enterprise Application Platform application Single Sign-On a... 50 (21 blank) 10
httpd-example An example Apache HTTP Server (httpd) application that serves static content.... 10 (3 blank) 5
jenkins-ephemeral Jenkins service, without persistent storage....
12 (all set) 7
jenkins-ephemeral-monitored Jenkins service, without persistent storage. ...
13 (all set) 8
jenkins-persistent Jenkins service, with persistent storage....
14 (all set) 8
jenkins-persistent-monitored Jenkins service, with persistent storage. ...
15 (all set) 9
jws31-tomcat7-basic-s2i Application template for JWS applications built using S2I.
12 (3 blank) 5
jws31-tomcat7-https-s2i An example JBoss Web Server application configured for use with https. For mo... 17 (5 blank) 7
jws31-tomcat8-basic-s2i An example JBoss Web Server application. For more information about using thi... 12 (3 blank) 5
jws31-tomcat8-https-s2i An example JBoss Web Server application. For more information about using thi... 17 (5 blank) 7
jws56-openjdk11-tomcat9-ubi8-basic-s2i An example JBoss Web Server application. For more information about using thi... 10 (3 blank) 5
jws56-openjdk11-tomcat9-ubi8-https-s2i An example JBoss Web Server application. For more information about using thi... 15 (5 blank) 7
jws56-openjdk8-tomcat9-ubi8-basic-s2i An example JBoss Web Server application. For more information about using thi... 10 (3 blank) 5
jws56-openjdk8-tomcat9-ubi8-https-s2i An example JBoss Web Server application. For more information about using thi... 15 (5 blank) 7
mariadb-ephemeral MariaDB database service, without persistent storage. For more information ab... 8 (3 generated) 3
mariadb-persistent MariaDB database service, with persistent storage. For more information about... 9 (3 generated) 4
mysql-ephemeral MySQL database service, without persistent storage. For more information abou... 8 (3 generated) 3
mysql-persistent MySQL database service, with persistent storage. For more information about u... 9 (3 generated) 4
nginx-example An example Nginx HTTP server and a reverse proxy (nginx) application that ser... 10 (3 blank) 5
nodejs-postgresql-example An example Node.js application with a PostgreSQL database. For more informati... 18 (4 blank) 8
nodejs-postgresql-persistent An example Node.js application with a PostgreSQL database. For more informati... 19 (4 blank) 9
openjdk-web-basic-s2i An example Java application using OpenJDK. For more information about using t... 9 (1 blank) 5
postgresql-ephemeral PostgreSQL database service, without persistent storage. For more information... 7 (2 generated) 3
postgresql-persistent PostgreSQL database service, with persistent storage. For more information ab... 8 (2 generated) 4
rails-pgsql-persistent An example Rails application with a PostgreSQL database. For more information... 21 (4 blank) 9
rails-postgresql-example An example Rails application with a PostgreSQL database. For more information... 20 (4 blank) 8
react-web-app-example Build a basic React Web Application
9 (1 blank) 5
redis-ephemeral Redis in-memory data structure store, without persistent storage. For more in... 5 (1 generated) 3
redis-persistent Redis in-memory data structure store, with persistent storage. For more infor... 6 (1 generated) 4
s2i-fuse711-spring-boot-2-camel Spring Boot 2 and Camel QuickStart. This example demonstrates how you can use... 18 (3 blank) 3
s2i-fuse711-spring-boot-2-camel-rest-3scale Spring Boot 2, Camel REST DSL and 3Scale QuickStart. This example demonstrate... 19 (3 blank) 5
s2i-fuse711-spring-boot-2-camel-xml Spring Boot 2 and Camel Xml QuickStart. This example demonstrates how you can... 18 (3 blank) 3
sso75-https An example application based on RH-SSO 7.5 on OpenJDK image. For more informa... 27 (16 blank) 6
sso75-ocp4-x509-https An example application based on RH-SSO 7.5 on OpenJDK image. For more informa... 13 (7 blank) 5
sso75-ocp4-x509-postgresql-persistent An example application based on RH-SSO 7.5 on OpenJDK image. For more informa... 21 (9 blank) 8
sso75-postgresql An example application based on RH-SSO 7.5 on OpenJDK image. For more informa... 34 (18 blank) 8
sso75-postgresql-persistent An example application based on RH-SSO 7.5 on OpenJDK image. For more informa... 35 (18 blank) 9
sso76-https An example application based on RH-SSO 7.6 on OpenJDK image. For more informa... 27 (16 blank) 6
sso76-ocp3-x509-https An example application based on RH-SSO 7.6 on OpenJDK image. For more informa... 13 (7 blank) 4
sso76-ocp3-x509-postgresql-persistent An example application based on RH-SSO 7.6 on OpenJDK image. For more informa... 24 (9 blank) 9
sso76-ocp4-x509-https An example application based on RH-SSO 7.6 on OpenJDK image. For more informa... 13 (7 blank) 5
sso76-ocp4-x509-postgresql-persistent An example application based on RH-SSO 7.6 on OpenJDK image. For more informa... 21 (9 blank) 8
sso76-postgresql An example application based on RH-SSO 7.6 on OpenJDK image. For more informa... 34 (18 blank) 8
sso76-postgresql-persistent An example application based on RH-SSO 7.6 on OpenJDK image. For more informa... 35 (18 blank) 9
Check for MySQL: mysql-ephemeral
and mysql-persistent
.
Use the mysql-ephemeral
template and assign a name to it
oc new-app mysql-ephemeral --name mysql
The --name
flag, mysql
, is there to make sure the scripts match
OpenShift created a user and password based on the mysql-ephemeral
template.
PS C:\Users\suyash.sambhare> oc new-app mysql-ephemeral --name mysql
warning: Cannot find git. Ensure that it is installed and in your path. Git is required to work with git repositories.
--> Deploying template "demo-parksmap/mysql-ephemeral" to project demo-parksmap
MySQL (Ephemeral)
---------
MySQL database service, without persistent storage. For more information about using this template, including OpenShift considerations, see https://github.com/sclorg/mysql-container/blob/master/8.0/root/usr/share/container-scripts/mysql/README.md.
WARNING: Any data stored will be lost upon pod destruction. Only use this template for testing
The following service(s) have been created in your project: mysql.
Username: userS2N
Password: UxdwrXTY7vBmRM1t
Database Name: sampledb
Connection URL: mysql://mysql:3306/
For more information about using this template, including OpenShift considerations, see https://github.com/sclorg/mysql-container/blob/master/8.0/root/usr/share/container-scripts/mysql/README.md.
* With parameters:
* Memory Limit=512Mi
* Namespace=openshift
* Database Service Name=mysql
* MySQL Connection Username=userS2N # generated
* MySQL Connection Password=UxdwrXTY7vBmRM1t # generated
* MySQL root user Password=MpuklSsBJRHAUDUM # generated
* MySQL Database Name=sampledb
* Version of MySQL Image=8.0-el8
--> Creating resources ...
secret "mysql" created
service "mysql" created
deploymentconfig.apps.openshift.io "mysql" created
--> Success
Application is not exposed. You can expose services to the outside world by executing one or more of the commands below:
'oc expose service/mysql'
Run 'oc status' to view your app.
Run oc status
to view your app.
In project demo-parksmap on server https://api.ocpcl.suyi.local:6443
svc/mysql - 172.30.207.109:3306
dc/mysql deploys openshift/mysql:8.0-el8
deployment #1 deployed about a minute ago - 1 pod
https://parksmap-demo-parksmap.apps.ocpcl.suyi.local (redirects) to pod port 8080-tcp (svc/parksmap)
deployment/parksmap deploys istag/parksmap:1.3.0
deployment #2 running for 4 months - 1 pod
deployment #1 deployed 4 months ago
svc/postgresql - 172.30.216.65:5432
dc/postgresql deploys openshift/postgresql:10-el8
deployment #2 deployed 4 months ago - 1 pod
deployment #1 deployed 4 months ago
1 info identified, use 'oc status --suggest' to see details.
You can see a list of your MySQL pods by running the following command:
oc get pods --selector app=mysql
Note the name of the pod and copy it to your computer clipboard.
See all of our pods by using the following command:
oc get pods
PS C:\Users\suyash.sambhare> oc get pods
NAME READY STATUS RESTARTS AGE
mysql-1-deploy 0/1 Completed 0 9m2s
mysql-1-tt7c9 1/1 Running 0 9m1s
parksmap-76d874d689-bqx4z 1/1 Running 0 25d
postgresql-2-5ckgp 1/1 Running 0 24d
You will notice two MySQL pods.
One pod is dedicated to building the pod that is running MySQL.
Delete the MySQL pod by running the following commands:
oc delete pod {pod-name}
oc get pods
PS C:\Users\suyash.sambhare> oc get pods
NAME READY STATUS RESTARTS AGE
mysql-1-deploy 0/1 Completed 0 9m2s
mysql-1-tt7c9 1/1 Running 0 9m1s
parksmap-76d874d689-bqx4z 1/1 Running 0 25d
postgresql-2-5ckgp 1/1 Running 0 24d
PS C:\Users\suyash.sambhare> oc delete pod mysql-1-tt7c9
pod "mysql-1-tt7c9" deleted
PS C:\Users\suyash.sambhare> oc get pods
NAME READY STATUS RESTARTS AGE
mysql-1-deploy 0/1 Completed 0 9m46s
mysql-1-zfg5b 0/1 Running 0 7s
parksmap-76d874d689-bqx4z 1/1 Running 0 25d
postgresql-2-5ckgp 1/1 Running 0 24d
A new MySQL pod is now running. This is a demonstration of Kubernetes' self-healing ability, in that it makes sure you have pods up and running. As a developer using a database in a pod, we can use this to our advantage. When we want to start with a clean slate, we can simply delete the pod running MySQL and a new one will appear. Note, however, that this will be a new MySQL pod. All of the previous databases, tables, and data—everything that was stored in the pod—will be gone. This is the "ephemeral" in mysql-ephemeral. The upside is that you know you have zero vestigial artifacts from any previous efforts.
In case you want to save your MySQL in OpenShift and have it not be ephemeral you can use the mysql-persistent template.
Use the oc rsh
command, to get inside the pod running MySQL and use the command-line utility to browse the database.
To do this we need to get the pod name and then run oc rsh {pod_name}
.
Once inside, we can see that database sampledb
is empty.
PS C:\Users\suyash.sambhare> oc rsh mysql-1-zfg5b
sh-4.4$ ls
sh-4.4$ mysql -u root
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 20
Server version: 8.0.32 Source distribution
Copyright (c) 2000, 2023, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sampledb |
| sys |
+--------------------+
5 rows in set (0.00 sec)
mysql> use sampledb;
Database changed
mysql> show tables;
Empty set (0.00 sec)
mysql>
You're left with some scripts and two files: One file containing test data and the other that builds the table. Put all that code under version control (e.g., Git) and you have artifacts that you can hand over to Operations. Under source control. For repeatable action. We are getting closer to being a unicorn every day, and this is an important step.
You can also make this into code that accepts parameters by creating a utility that accepts parameters and builds all my scripts.
Top comments (0)