Bookmark this page

Lab: Managing Red Hat OpenShift Builds

Manage and troubleshoot application builds in Red Hat OpenShift

Outcomes

  • Create, start, and rebuild application builds in Red Hat OpenShift

  • Debug and fix failed OpenShift builds

As the student user on the workstation machine, use the lab command to prepare your system for this exercise.

[student@workstation ~]$ lab start builds-review

Instructions

Your task is to deploy the expense-service application to Red Hat OpenShift. Because your OpenShift cluster does not contain an S2I base image that is compatible with the application, your colleague provided you with the source code for the application and a Dockerfile file.

Use the following information to deploy the application into the OpenShift cluster.

Application name expense-service
Source code location https://git.ocp4.example.com/developer/DO288-apps
Source code directory apps/builds-review/expense-service
Build strategy Docker
Application URL http://expense-service-builds-review.apps.ocp4.example.com
  1. Log in to Red Hat OpenShift and select the builds-review project.

    1. Log in to OpenShift.

      [student@workstation ~]$ oc login -u developer -p developer \
      https://api.ocp4.example.com:6443
      Login successful.
      ...output omitted...
    2. Ensure that you use the builds-review project.

      [student@workstation ~]$ oc project builds-review
      Already on project "builds-review" on server "https://api.ocp4.example.com:6443".
  2. Explore and build the expense-service application.

    Use the following parameters for the build:

    • Application name: expense-service

    • Build strategy: docker

    • Source code: https://git.ocp4.example.com/developer/DO288-apps

    The application is in the apps/builds-review/expense-service directory in the repository.

    You can explore the application locally in the ~/DO288/DO288-apps/apps/builds-review/expense-service directory.

    1. Navigate to the ~/DO288/DO288-apps/apps/builds-review/expense-service directory.

      [student@workstation ~]$ cd ~/DO288/DO288-apps/apps/builds-review/expense-service
      ...no output expected...
    2. Explore the Dockerfile file to see the application container build process.

      FROM registry.ocp4.example.com:8443/redhattraining/ocpdev-ubi8-openjdk-17-base:1.16
      
      COPY pom.xml .
      RUN mvn dependency:go-offline
      
      COPY src .
      RUN mvn clean package
      
      CMD ["java", "-jar", "target/expense-service-1.0.0-SNAPSHOT-runner.jar"]
    3. Execute a build in OpenShift.

      [student@workstation expense-service]$ oc new-app --name expense-service \
      --strategy Docker \
      --context-dir apps/builds-review/expense-service \
      https://git.ocp4.example.com/developer/DO288-apps
      --> Found image 11c20bc (23 months old) in image stream ...output omitted...
    4. Follow the build logs to verify that the build finishes.

      [student@workstation expense-service]$ oc logs -f buildconfig/expense-service
      Cloning "https://git.ocp4.example.com/developer/DO288-apps" ...
      ...output omitted...
      Push successful
    5. Verify the application pods.

      [student@workstation expense-service]$ oc get po
      NAME                               READY   STATUS             RESTARTS     AGE
      expense-service-1-build            0/1     Completed          0            6m49s
      expense-service-76597cbd4d-p8kkr   0/1     CrashLoopBackOff   1 (4s ago)   14s
  3. Find why the application is failing and fix the problem.

    1. Check the application logs.

      [student@workstation expense-service]$ oc logs deploy/expense-service
      Error: Unable to access jarfile target/expense-service-1.0.0-SNAPSHOT-runner.jar
    2. Verify that the target/expense-service-1.0.0-SNAPSHOT-runner.jar file exists in the build output by building the application locally.

      [student@workstation expense-service]$ mvn -Dmaven.compiler.release=11 clean \
      package
      ...output omitted...
      [INFO] [io.quarkus.deployment.pkg.steps.JarResultBuildStep] Building uber jar: /home/student/DO288/DO288-apps/apps/builds-review/expense-service/target/expense-service-1.0.0-SNAPSHOT-runner.jar
      [INFO] [io.quarkus.deployment.QuarkusAugmentor] Quarkus augmentation completed in 5349ms
      [INFO] ------------------------------------------------------------------------
      [INFO] BUILD SUCCESS
      ...output omitted...

      Optionally, you can verify that the file exists in the target directory.

      [student@workstation expense-service]$ ls target/*runner*
      target/expense-service-1.0.0-SNAPSHOT-runner.jar
    3. Verify the state of the container file system.

      [student@workstation expense-service]$ oc debug deploy/expense-service
      Warning: ...output omitted...
      If you don't see a command prompt, try pressing enter.
      sh-4.4$

      List the files in the target directory.

      sh-4.4$ ls target/
      classes  expense-service-1.0.0-SNAPSHOT.jar  maven-archiver  quarkus-app  quarkus-artifact.properties

      List the files in the current directory.

      sh-4.4$ ls
      main  pom.xml  target

      The container does not include the src directory. Consequently, the application does not read the src/main/resources/application.properties file.

      Finally, exit the debug shell session.

      sh-4.4$ exit
      exit
      
      Removing debug pod ...
    4. Correct the Dockerfile file. Open the Dockerfile file and modify the COPY src . instruction to create the src directory.

      ...output omitted...
      RUN mvn dependency:go-offline
      
      COPY src src
      RUN mvn clean package
      
      CMD ["java", "-jar", "target/expense-service-1.0.0-SNAPSHOT-runner.jar"]
    5. Commit changes into the git repository.

      [student@workstation expense-service]$ git commit -am "fix: correct dockerfile"
      Committer: Student User <student@workstation.lab.example.com>
      ...output omitted...
      1 file changed, 1 insertion(+), 1 deletion(-)
    6. Push the changes into the remote git server.

      [student@workstation expense-service]$ git push
      Username for 'https://git.ocp4.example.com': developer
      Password for 'https://developer@git.ocp4.example.com': developer
      warning: redirecting to https://git.ocp4.example.com/developer/DO288-apps.git/
      Enumerating objects: 24, done.
      ...output omitted...

      Note

      Git might prompt for credentials by opening a desktop window. If you prefer to enter the credentials in the terminal, then close the window.

  4. Redeploy the application.

    1. Start a build in OpenShift.

      [student@workstation expense-service]$ oc start-build bc/expense-service --follow
      build.build.openshift.io/expense-service-2 started
      Cloning "https://git.ocp4.example.com/developer/DO288-apps" ...
      ...output omitted...
      Push successful
    2. Verify that the application pods start.

      [student@workstation expense-service]$ oc get po
      expense-service-1-build            0/1     Completed   0          3m32s
      expense-service-2-build            0/1     Completed   0          71s
      expense-service-5649479dbc-n7dpq   1/1     Running     0          9s
  5. Expose the application for external requests. Use the default expense-service-builds-review.apps.ocp4.example.com hostname for the application.

    [student@workstation expense-service]$ oc expose svc expense-service
    route.route.openshift.io/expense-service exposed
  6. Verify that the application works.

    [student@workstation expense-service]$ curl -s \
    expense-service-builds-review.apps.ocp4.example.com/expenses | jq
    [
      {
        "uuid": "725bbdb0-fcb1-4d5f-a140-7c2a77b02177",
        "name": "Quarkus for Spring Developers",
        "creationDate": "2023-07-19T11:11:08.539309973",
        "paymentMethod": "DEBIT_CARD",
        "amount": 10.00
      }
    ...output omitted...

Evaluation

As the student user on the workstation machine, use the lab command to grade your work. Correct any reported failures and rerun the command until successful.

[student@workstation ~]$ lab grade builds-review

Finish

As the student user on the workstation machine, use the lab command to complete this exercise. This step is important to ensure that resources from previous exercises do not impact upcoming exercises.

[student@workstation ~]$ lab finish builds-review

Revision: do288-4.12-0d49506