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
|
Log in to Red Hat OpenShift and select the builds-review project.
Log in to OpenShift.
[student@workstation ~]$ oc login -u developer -p developer \
https://api.ocp4.example.com:6443
Login successful.
...output omitted...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".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.
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...
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"]
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...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 successfulVerify 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) 14sFind why the application is failing and fix the problem.
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.jarVerify 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.jarVerify 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.propertiesList the files in the current directory.
sh-4.4$lsmainpom.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 ...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"]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(-)Push the changes into the remote git server.
[student@workstation expense-service]$git pushUsername for 'https://git.ocp4.example.com':developerPassword for 'https://developer@git.ocp4.example.com':developerwarning: redirecting to https://git.ocp4.example.com/developer/DO288-apps.git/ Enumerating objects: 24, done. ...output omitted...
Git might prompt for credentials by opening a desktop window. If you prefer to enter the credentials in the terminal, then close the window.
Redeploy the application.
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 successfulVerify 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 9sExpose the application for external requests.
Use the default expense-service-builds-review.apps.ocp4.example.com hostname for the application.
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...