Bookmark this page

Guided Exercise: Managing Application Deployments

Configure and manage application deployments on Red Hat OpenShift.

Outcomes

  • Create deployments.

  • Debug failing deployments.

  • Configure application deployments by using the oc CLI.

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

The lab command creates the deployments-applications and deploys a PostgreSQL database instance by using the postgresql-ephemeral template.

[student@workstation ~]$ lab start deployments-applications

Instructions

  1. Log in to Red Hat OpenShift.

    1. Log in to OpenShift as the developer user.

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

      [student@workstation ~]$ oc project deployments-applications
      Already on project "deployments-applications" on server "https://api.ocp4.example.com:6443".
  2. Verify that the project contains the postgresql secret. This secret contains the login information for the deployed PostgreSQL database.

    [student@workstation ~]$ oc describe secret postgresql
    ...output omitted...
    Data
    ====
    database-name:      8 bytes
    database-password:  16 bytes
    database-user:      7 bytes
  3. Attempt to deploy the expense-service application.

    1. Deploy the registry.ocp4.example.com:8443/redhattraining/ocpdev-expense-service:4.12 container image.

      [student@workstation ~]$ oc new-app --name=expense-service \
      --image=registry.ocp4.example.com:8443/redhattraining/ocpdev-expense-service:4.12
      --> Found container image ...output omitted...

      Note

      You can safely ignore pod security warnings for exercises in this course. OpenShift uses the Security Context Constraints controller to provide safe defaults for pod security.

    2. Verify that the application pods are crashing.

      [student@workstation ~]$ oc get po
      NAME                              READY   STATUS             RESTARTS      AGE
      expense-service-b48dd8ddc-rqsgl   0/1     CrashLoopBackOff   4 (17s ago)   2m5s
      postgresql-1-deploy               0/1     Completed          0             10m
      postgresql-1-lps82                1/1     Running            0             10m

      The application might be in the Error state as well. Alternatively, the application might appear to be running between the pod restarts. In that case, repeat the preceding command.

    3. See the application logs to determine the issue.

      [student@workstation ~]$ oc logs deployment/expense-service | head
      ...output omitted...
      2023-07-25 13:13:25,066 WARN  [io.agr.pool] (agroal-11) Datasource '<default>': FATAL: password authentication failed for user "userNWW"
      2023-07-25 13:13:25,069 WARN  [org.hib.eng.jdb.env.int.JdbcEnvironmentInitiator] (JPA Startup Thread) HHH000342: Could not obtain connection to query metadata: org.postgresql.util.PSQLException: FATAL: password authentication failed for user "userNWW"
      ...output omitted...
  4. Explore the application source code to determine the database configuration.

    1. Navigate to the ~/DO288/labs/deployments-applications/expense-service directory.

      [student@workstation ~]$ cd ~/DO288/labs/deployments-applications/expense-service
      ...no output expected...
    2. Open the src/main/resources/application.properties file.

      ...output omitted...
      quarkus.datasource.username=${DATABASE_USER:userNWW}
      quarkus.datasource.password=${DATABASE_PASSWORD:sr5ps4agHuDlTa5k}
      quarkus.datasource.jdbc.url=jdbc:postgresql://postgresql:5432/${DATABASE_NAME:sampledb}

      The application defaults to a fixed user and password configuration for the PostgreSQL database connection. However, this configuration does not work with your database, because the user and password credentials are automatically generated.

    3. Extract the postgresql secret values to the current directory.

      [student@workstation expense-service]$ oc extract secret/postgresql --to=.
      database-name
      database-password
      database-user
    4. Verify the correct values to connect to the PostgreSQL database. Note that your user and password values might differ from the values listed in the following example.

      [student@workstation expense-service]$ tail -n +1 database*; echo
      ==> database-name <==
      sampledb
      ==> database-password <==
      gV3qRxyeGteqGbCu
      ==> database-user <==
      userTHS

      The database user and password differ from the values configured in the application.

    5. Navigate to your home directory.

      [student@workstation expense-service]$ cd
      ...no output expected...
  5. Overwrite the application properties by using environment variables.

    1. Set the environment variables to use the postgresql secret values.

      [student@workstation ~]$ oc set env deploy/expense-service \
      --from=secret/postgresql
      ...output omitted...
      deployment.apps/expense-service updated
    2. View the changes that the preceding step made to the deployment object.

      [student@workstation ~]$ oc describe deployment
      ...output omitted...
          Ports:       8080/TCP, 8443/TCP
          Host Ports:  0/TCP, 0/TCP
          Environment:
            DATABASE_PASSWORD:  <set to the key 'database-password' in secret 'postgresql'>  Optional: false
            DATABASE_USER:      <set to the key 'database-user' in secret 'postgresql'>      Optional: false
            DATABASE_NAME:      <set to the key 'database-name' in secret 'postgresql'>      Optional: false
          Mounts:               <none>
        Volumes:                <none>
      ...output omitted...

      Optionally, you can view the YAML deployment object:

      [student@workstation ~]$ oc get deployment -o yaml
      ...output omitted...
            spec:
              containers:
              - env:
                - name: DATABASE_PASSWORD
                  valueFrom:
                    secretKeyRef:
                      key: database-password
                      name: postgresql
                - name: DATABASE_USER
                  valueFrom:
                    secretKeyRef:
                      key: database-user
                      name: postgresql
      ...output omitted...
  6. Test the application.

    1. Expose the expense-service service.

      [student@workstation ~]$ oc expose svc expense-service
      route.route.openshift.io/expense-service exposed
    2. Send a request to the route URL.

      [student@workstation ~]$ curl -s \
      expense-service-deployments-applications.apps.ocp4.example.com/expenses | jq
      [
        {
          "id": 5,
          "amount": 15.00,
          "associateId": 1,
          "name": "Phone",
          "paymentMethod": "CASH",
          "uuid": "d9aa8a20-a68c-8686-6e5a-5f5930a5b3b3"
        },
      ...output omitted...

Finish

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 deployments-applications

Revision: do288-4.12-0d49506