Bookmark this page

Lab: Declarative Resource Management

Deploy and update applications from resource manifests that are parameterized for different target environments.

Outcomes

  • Deploy an application by using Kustomize from provided files.

  • Apply an application update that changes a deployment.

  • Deploy an overlay of the application that increases the number of replicas.

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

This command ensures that the cluster API is reachable.

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

Instructions

  1. Clone the v1.1.0 version of the application from the https://git.ocp4.example.com/developer/declarative-review.git URL. Because this repository uses Git branches to represent application versions, you must use the v1.1.0 branch.

    1. Change to the ~/DO280/labs/declarative-review directory.

      [student@workstation ~]$ cd DO280/labs/declarative-review
      [student@workstation declarative-review]$
    2. Clone the initial version of the application.

      [student@workstation declarative-review]$ git clone \
        https://git.ocp4.example.com/developer/declarative-review.git --branch v1.1.0
      Cloning into 'declarative-review'...
      ...output omitted...
    3. Change to the repository directory.

      [student@workstation declarative-review]$ cd declarative-review
  2. Examine the first version of the application.

    1. Use the tree command to review the structure of the repository.

      [student@workstation declarative-review]$ tree
      .
      ├── base
      │   ├── database  1
      │   │   ├── configmap.yaml
      │   │   ├── deployment.yaml
      │   │   ├── kustomization.yaml
      │   │   ├── secret.yaml
      │   │   └── service.yaml
      │   ├── exoplanets  2
      │   │   ├── configmap.yaml
      │   │   ├── deployment.yaml
      │   │   ├── kustomization.yaml
      │   │   ├── route.yaml
      │   │   ├── secret.yaml
      │   │   └── service.yaml
      │   └── kustomization.yaml  3
      ├── overlays  4
      │   └── production
      │       ├── kustomization.yaml
      │       └── patch-replicas.yaml
      └── README.md
      
      5 directories, 15 files

      1

      The database base defines resources to deploy a database.

      2

      The exoplanets base defines resources to deploy an application that uses the database.

      3

      The repository has a kustomization.yaml file at the root, which uses two other bases.

      4

      The repository also has a production overlay.

    2. Examine the base/kustomization.yaml file.

      [student@workstation declarative-review]$ cat base/kustomization.yaml
      kind: Kustomization
      resources:
      - database
      - exoplanets

      The base/kustomization.yaml file uses the other two bases.

  3. Log in to the OpenShift cluster as the developer user with the developer password. Deploy the base directory of the repository to a new declarative-review project. Verify that the v1.1.0 version of the application is available at http://exoplanets-declarative-review.apps.ocp4.example.com.

    1. Log in to the OpenShift cluster as the developer user with the developer password.

      [student@workstation declarative-review]$ oc login -u developer -p developer \
        https://api.ocp4.example.com:6443
      Login successful.
      
      ...output omitted...
    2. Create the declarative-review project.

      [student@workstation declarative-review]$ oc new-project declarative-review
      ...output omitted...
    3. Use the oc apply -k command to deploy the application with Kustomize.

      [student@workstation declarative-review]$ oc apply -k base
      configmap/database created
      configmap/exoplanets created
      secret/database created
      secret/exoplanets created
      service/database created
      service/exoplanets created
      deployment.apps/database created
      deployment.apps/exoplanets created
      route.route.openshift.io/exoplanets created
    4. Use the watch command to wait until the workloads are running.

      [student@workstation declarative-review]$ watch oc get all
      NAME                             READY   STATUS    RESTARTS      AGE
      pod/database-55d6c77787-47649    1/1     Running   0             57s
      pod/exoplanets-d6f57869d-jhkhc   1/1     Running   2 (54s ago)   57s
      
      NAME                 TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)    AGE
      service/database     ClusterIP   172.30.236.123   <none>        5432/TCP   57s
      service/exoplanets   ClusterIP   172.30.248.130   <none>        8080/TCP   57s
      
      NAME                         READY   UP-TO-DATE   AVAILABLE   AGE
      deployment.apps/database     1/1     1            1           57s
      deployment.apps/exoplanets   1/1     1            1           57s
      
      NAME                                   DESIRED   CURRENT   READY   AGE
      replicaset.apps/database-55d6c77787    1         1         1       57s
      replicaset.apps/exoplanets-d6f57869d   1         1         1       57s
      
      NAME                  HOST/PORT                                             ...
      route.../exoplanets   exoplanets-declarative-review.apps.ocp4.example.com   ...

      Press Ctrl+C to exit the watch command.

    5. Open a web browser and navigate to http://exoplanets-declarative-review.apps.ocp4.example.com.

      The browser displays version v1.1.0 of the application.

  4. Change to the v1.1.1 version of the application and examine the changes.

    1. Change to the v1.1.1 branch.

      [student@workstation declarative-review]$ git checkout v1.1.1
      branch 'v1.1.1' set up to track 'origin/v1.1.1'.
      Switched to a new branch 'v1.1.1'
    2. Use the git show command to display the last commit.

      [student@workstation declarative-review]$ git show
      ...output omitted...
      diff --git a/base/exoplanets/deployment.yaml b/base/exoplanets/deployment.yaml
      index 8bc4cf9..8389b69 100644
      --- a/base/exoplanets/deployment.yaml
      +++ b/base/exoplanets/deployment.yaml
      @@ -23,7 +23,7 @@ spec:
                   name: exoplanets
               - secretRef:
                   name: exoplanets
      -        image: registry.ocp4.example.com:8443/redhattraining/exoplanets:v1.1.0
      +        image: registry.ocp4.example.com:8443/redhattraining/exoplanets:v1.1.1
               imagePullPolicy: Always
               livenessProbe:
                 httpGet:

      The v1.1.1 version updates the application to the v1.1.1 image.

  5. Deploy the updated application and verify that the URL now displays the v1.1.1 version.

    1. Use the oc apply -k command to execute the changes.

      [student@workstation declarative-review]$ oc apply -k base
      ...output omitted...
    2. Use the watch command to wait until the application redeploys.

      [student@workstation declarative-review]$ watch oc get all
      NAME                             READY   STATUS    RESTARTS      AGE
      pod/database-55d6c77787-47649    1/1     Running   0             57s
      pod/exoplanets-d6f57869d-jhkhc   1/1     Running   2 (54s ago)   57s
      
      NAME                 TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)    AGE
      service/database     ClusterIP   172.30.236.123   <none>        5432/TCP   57s
      service/exoplanets   ClusterIP   172.30.248.130   <none>        8080/TCP   57s
      
      NAME                         READY   UP-TO-DATE   AVAILABLE   AGE
      deployment.apps/database     1/1     1            1           57s
      deployment.apps/exoplanets   1/1     1            1           57s
      
      NAME                                   DESIRED   CURRENT   READY   AGE
      replicaset.apps/database-55d6c77787    1         1         1       57s
      replicaset.apps/exoplanets-d6f57869d   1         1         1       57s
      
      NAME                  HOST/PORT                                             ...
      route.../exoplanets   exoplanets-declarative-review.apps.ocp4.example.com   ...

      Press Ctrl+C to exit the watch command.

    3. Open a web browser and navigate to http://exoplanets-declarative-review.apps.ocp4.example.com.

      The browser displays version v1.1.1 of the application.

  6. Examine the overlay in the overlays/production path.

    1. Examine the overlays/production/kustomization.yaml file.

      [student@workstation declarative-review]$ cat \
        overlays/production/kustomization.yaml
      kind: Kustomization
      resources:
      - ../../base/
      patches:
      - path: patch-replicas.yaml
        target:
          kind: Deployment
          name: exoplanets

      This overlay applies a patch over the base.

    2. Examine the overlays/production/patch-replicas.yaml file.

      [student@workstation declarative-review]$ cat \
        overlays/production/patch-replicas.yaml
      apiVersion: apps/v1
      kind: Deployment
      metadata:
        name: exoplanets
      spec:
        replicas: 2

      This patch increases the number of replicas of the deployment, so that the production deployment can handle more users.

  7. Deploy the production overlay to a new declarative-review-production project. Verify that the v1.1.1 version of the application is available at http://exoplanets-declarative-review-production.apps.ocp4.example.com with two replicas.

    1. Create the declarative-review-production project.

      [student@workstation declarative-review]$ oc new-project declarative-review-production
      Now using project "declarative-review-production" on server "https://api.ocp4.example.com:6443".
      ...output omitted...
    2. Use the oc apply -k command to deploy the overlay.

      [student@workstation declarative-review]$ oc apply -k overlays/production
      configmap/database created
      configmap/exoplanets created
      secret/database created
      secret/exoplanets created
      service/database created
      service/exoplanets created
      deployment.apps/database created
      deployment.apps/exoplanets created
      route.route.openshift.io/exoplanets created
    3. Use the watch command to wait until the workloads are running.

      [student@workstation declarative-review]$ watch oc get all
      NAME                              READY   STATUS    RESTARTS       AGE
      pod/database-55d6c77787-b5x4n     1/1     Running   0              5m11s
      pod/exoplanets-55666f556f-ndwkz   1/1     Running   2 (5m8s ago)   5m11s
      pod/exoplanets-55666f556f-q7s7j   1/1     Running   2 (5m7s ago)   5m11s
      
      NAME                 TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
      service/database     ClusterIP   172.30.24.165   <none>        5432/TCP   5m11s
      service/exoplanets   ClusterIP   172.30.90.176   <none>        8080/TCP   5m11s
      
      NAME                         READY   UP-TO-DATE   AVAILABLE   AGE
      deployment.apps/database     1/1     1            1           5m11s
      deployment.apps/exoplanets   2/2     2            2           5m11s
      
      NAME                                    DESIRED   CURRENT   READY   AGE
      replicaset.apps/database-55d6c77787     1         1         1       5m11s
      replicaset.apps/exoplanets-55666f556f   2         2         2       5m11s
      
      NAME                HOST/PORT
      route.../exoplanets exoplanets-declarative-review-production.apps.ocp4.example.com

      The exoplanets deployment has two replicas.

    4. Open a web browser and navigate to http://exoplanets-declarative-review-production.apps.ocp4.example.com.

      The browser displays version v1.1.1 of the application.

    5. Change to the home directory.

      [student@workstation declarative-review]$ cd
      [student@workstation ~]$

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 declarative-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 declarative-review

Revision: do280-4.14-08d11e1