OpenShift Hands-On Lab - Creating an Openshift Application from an Image

OpenShift Hands-On Lab - Creating an Openshift Application from an Image

OpenShift Learning Path for Cloud and DevOps Engineers

📝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.

  1. To complete this lab, you must first register for a Red Hat Developer login on theRed Hat login page.

  2. 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.

  1. Navigate and log in to your Red Hat Developer account.

  2. Under the Local tab, select your OS and click Download CodeReady Containers.

  3. Click Copy Pull Secret for later use.

  4. 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..).*

  5. Open a terminal session and set up CodeReady Containers:

     crc setup
    
  6. When prompted, enter yes to enable telemetry data collection or no to disable telemetry data collection.

  7. Start the VM:

     crc start
    

    📌Note: The cluster takes a minimum of 4 minutes to start before serving a request.

  8. When prompted, paste in the previously copied pull request and press Enter.

  9. Print the command needed to add the cached oc executable to your $PATH:

     crc oc-env
    
  10. Run the printed command.

  11. 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).

  1. 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.

  2. Select Try OpenShift in our free sandbox.

  3. Click Launch your Developer Sandbox for Red Hat OpenShift.

  4. To confirm your account, enter a valid phone number and click Send Code.

  5. Enter the verification code you received and click Verify Code.

  6. Click Start using your sandbox.

  7. Under Log in with, click DevSandbox.

  8. Select the I have read and agree to the Red Hat OpenShift Online Services Agreement and click SUBMIT.

  9. At the top right, click the shell icon (>_).

  10. 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.

  1. Log in to the cluster:

     oc login -u developer https://api.crc.testing:6443
    
  2. In a new browser, log in to the cluster using the console URL.

  3. Use thedeveloper 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

  1. 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
    
  2. Clear your screen:

     clear
    

📝Create a New Route by Exposing the Service

CodeReady Containers

  1. Check the status:
oc status
  1. clear your screen.

  2. Retrieve the resources:

oc get all
  1. Create a new route for the application by exposing the service/demo-mysql service:

     oc expose service/demo-mysql
    
  2. clear your screen.

  3. Retrieve your resources:

     oc get all
    

    Retrieve the routes:

     oc get route
    

OpenShift Sandbox

  1. Retrieve the service name:

     oc get service
    
  2. Expose the demo-mysql service:

     oc expose service demo-mysql
    
  3. Retrieve the routes:

     oc get route
    

    clear your screen.

📝Test the New Application Database

CodeReady Containers

  1. Open up a new shell window in your system.

  2. Retrieve the pods:

     oc get pods
    
  3. Forward the port 3306 from the pod to the local host:

     oc port-forward <POD_NAME> 3306:3306
    
  4. Move back to the original shell.

  5. 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
    
  6. Once you're logged in to the database, view the databases:

     show databases;
    

    Exit the database:

     quit
    
  7. Move back to the second shell.

  8. Shut down the forwarding port by pressing Control + C.

  9. Move back to the original shell.

  10. Log in to the pod:

    oc rsh <POD_NAME>
    
  11. Verify MySQL is running:

    ps -ef
    
  12. Log in to the MySQL application:

    mysql -udemo -pdemo123
    
  13. View the databases:

    show databases;
    
  14. Exit the database:

    quit
    

OpenShift Sandbox

  1. Retrieve the pods:

     oc get pods
    
  2. Open a remote shell session to a container:

     oc rsh <POD_NAME>
    
  3. Connect to the pod:

     mysql -udemo -pdemo123
    
  4. View the databases:

     show databases;
    
  5. Exit out of the database:

     exit
    
  6. Exit out of the container:

     exit
    
  7. clear your screen.

  8. View of the OpenShift Console:

  9. 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