OpenShift Hands-On Lab - Creating an Openshift Application from an Image
OpenShift Learning Path for Cloud and DevOps Engineers
Table of contents
- 📝Introduction
- 📝Set Up CodeReady Containers (For those who choose to run it locally)
- 📝Set Up an OpenShift Sandbox (For those who choose to run it online on the Cloud. This was my choice).
- 📝Log In to the Cluster (Option 1)
- 📝Log In to the Cluster (Option 2)
- 📝Create the New Application
- 📝Create a New Route by Exposing the Service
- 📝Test the New Application Database
📝Introduction
In this lab, we will be creating a new OpenShift application from an image. The new application will be running a MySQL database.
These are the objectives of this lab:
- Create the New Project
📌Note: This section only applies to students using CodeReady Containers. Skip this section if you are using Red Hat OpenShift sandbox.
Create the New Application
Create a New Route by Exposing the Service
Test the New Application Database
📌Note: Due to resource limitations in my Cloud Playground environment, I used the below Red Hat free online resources available for this lab.
To complete this lab, you must first register for a Red Hat Developer login on theRed Hat login page.
Once registered, you may complete the lab steps usingRed Hat's CodeReady Containers or the Red Hat-provided OpenShift sandbox.
Solutions for CodeReady Containers and the Red Hat OpenShift sandbox can be used in this lab. However, I decided to use the free Red Hat-provided OpenShift sandbox available for 30 days.
📝Set Up CodeReady Containers (For those who choose to run it locally)
📌Note: CodeReady Containers has specific hardware and operating system requirements. To ensure your environment is compatible, seeRed Hat's minimum system requirements for CodeReady Containers.
Navigate and log in to your Red Hat Developer account.
Under the Local tab, select your OS and click Download CodeReady Containers.
Click Copy Pull Secret for later use.
Install CodeReady Containers.
📌*Note:For OS-specific installation instructions, see [Red Hat's CodeReady Containers installation guide](access.redhat.com/documentation/en-us/red_h..).*
Open a terminal session and set up CodeReady Containers:
crc setup
When prompted, enter
yes
to enable telemetry data collection orno
to disable telemetry data collection.Start the VM:
crc start
📌Note: The cluster takes a minimum of 4 minutes to start before serving a request.
When prompted, paste in the previously copied pull request and press Enter.
Print the command needed to add the cached
oc
executable to your$PATH
:crc oc-env
Run the printed command.
Log in as the developer user:
oc login -u developer https://api.crc.testing:6443
📝Set Up an OpenShift Sandbox (For those who choose to run it online on the Cloud. This was my choice).
To log in through the web UI directly, navigate to the Red Hat CodeReady Containers page and log in to your Red Hat Developer account with the credentials you created.
Select Try OpenShift in our free sandbox.
Click Launch your Developer Sandbox for Red Hat OpenShift.
To confirm your account, enter a valid phone number and click Send Code.
Enter the verification code you received and click Verify Code.
Click Start using your sandbox.
Under Log in with, click DevSandbox.
Select the I have read and agree to the Red Hat OpenShift Online Services Agreement and click SUBMIT.
At the top right, click the shell icon (>_).
Leave the default project selected and click Start. Note: You will not be able to create new projects in the sandbox. For any steps in the lab stating you need to create a new project, skip that step and ensure you use one of the existing projects: <username>-dev, or <username>-stage.
📌Note: Type
q
to exit a menu or log.
📝Log In to the Cluster (Option 1)
📌Note: This section only applies to students using CodeReady Containers. Otherwise, skip to the next objective.
Log in to the cluster:
oc login -u developer https://api.crc.testing:6443
In a new browser, log in to the cluster using the console URL.
Use the
developer
credentials to log in.
📝Log In to the Cluster (Option 2)
📌Note: To complete a login using oc
in the OpenShift sandbox, click on the shell (>_) icon. Alternatively, you can download the binary and log in from a remote system (e.g. VSCode). Please review the Using Red Hat OpenShift Sandbox video detailing the necessary steps for that process.
📝Create the New Application
Create a new MySQL application in the project with the specified configuration:
oc new-app mysql -e MYSQL_ROOT_PASSWORD=rootpass -e MYSQL_USER=demo -e MYSQL_PASSWORD=demo123 -e MYSQL_DATABASE=demodb --name=demo-mysql -l app=demo-app
Clear your screen:
clear
📝Create a New Route by Exposing the Service
CodeReady Containers
- Check the status:
oc status
clear
your screen.Retrieve the resources:
oc get all
Create a new route for the application by exposing the
service/demo-mysql
service:oc expose service/demo-mysql
clear
your screen.Retrieve your resources:
oc get all
Retrieve the routes:
oc get route
OpenShift Sandbox
Retrieve the service name:
oc get service
Expose the
demo-mysql
service:oc expose service demo-mysql
Retrieve the routes:
oc get route
clear
your screen.
📝Test the New Application Database
CodeReady Containers
Open up a new shell window in your system.
Retrieve the pods:
oc get pods
Forward the port
3306
from the pod to the local host:oc port-forward <POD_NAME> 3306:3306
Move back to the original shell.
Use MySQL to connect to the local host port
3306
to forward the traffic into the pod and connect to the database:mysql -udemo -pdemo123 --protocol tcp -h localhost
Once you're logged in to the database, view the databases:
show databases;
Exit the database:
quit
Move back to the second shell.
Shut down the forwarding port by pressing Control + C.
Move back to the original shell.
Log in to the pod:
oc rsh <POD_NAME>
Verify MySQL is running:
ps -ef
Log in to the MySQL application:
mysql -udemo -pdemo123
View the databases:
show databases;
Exit the database:
quit
OpenShift Sandbox
Retrieve the pods:
oc get pods
Open a remote shell session to a container:
oc rsh <POD_NAME>
Connect to the pod:
mysql -udemo -pdemo123
View the databases:
show databases;
Exit out of the database:
exit
Exit out of the container:
exit
clear
your screen.View of the OpenShift Console:
Delete the associated resources:
oc delete deployment.apps/demo-mysql service/demo-mysql route.route.openshift.io/demo-mysql
📌Note - At the end of each hands-on Lab, always clean up all previous resources created to avoid being charged if you used a Cloud Provider to provision them.
Congratulations — you have completed this hands-on lab covering the basics of Creating an Openshift Application from an Image.
Thank you for reading. I hope you understood and learned something helpful from my blog.
Please follow me on CloudDevOpsToLearn and LinkedIn, franciscojblsouza