Bookmark this page

Guided Exercise: Resource Manifests

Deploy and update an application from resource manifests from YAML files that are stored in a Git server.

Outcomes

  • Deploy applications from resource manifests from YAML files that are stored in a GitLab repository.

  • Inspect new manifests for potential update issues.

  • Update application deployments from new YAML manifests.

  • Force the redeployment of pods when necessary.

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

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

Instructions

  1. Log in to the OpenShift cluster and create the declarative-manifests project.

    1. Log in to the cluster as the developer user.

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

      [student@workstation ~]$ oc new-project declarative-manifests
      Now using project "declarative-manifests" on server ...
      ...output omitted...
  2. Clone the declarative-manifest project from the Git repository.

    1. Change your directory to the project labs directory.

      [student@workstation ~]$ cd ~/DO280/labs
    2. Clone the Git repository from https://git.ocp4.example.com/developer/declarative-manifests.git. Use developer for both the username and for the password.

      [student@workstation lab]$ git clone \
        https://git.ocp4.example.com/developer/declarative-manifests.git
      Cloning into 'declarative-manifests'...
      remote: Enumerating objects: 24, done.
      remote: Counting objects: 100% (24/24), done.
      remote: Compressing objects: 100% (21/21), done.
      remote: Total 24 (delta 8), reused 0 (delta 0), pack-reused 0
      Receiving objects: 100% (24/24), done.
      Resolving deltas: 100% (8/8), done.
    3. Go to the declarative-manifest directory.

      [student@workstation lab]$ cd declarative-manifests
      [student@workstation declarative-manifests]$
  3. Inspect the contents of the Git repository.

    1. List the contents of the declarative-manifests directory.

      [student@workstation declarative-manifests]$ ls -lA
      total 12
      -rw-rw-r--. 1 student student 3443 Jun 21 16:39 database.yaml
      -rw-rw-r--. 1 student student 2278 Jun 21 16:39 exoplanets.yaml
      drwxrwxr-x. 8 student student  163 Jun 21 16:39 .git
      -rw-rw-r--. 1 student student    0 Jun 21 16:39 .gitkeep
      -rw-rw-r--. 1 student student   11 Jun 21 16:39 README.md
    2. List the commits, branches, and tags on the Git repository.

      [student@workstation declarative-manifests]$ git log --oneline
      4045336 (HEAD -> main, tag: third, origin/v1.1.1, origin/main, origin/HEAD) Exoplanets v1.1.1 1
      ad455b2 Database v1.1.1
      821420c (tag: second, origin/v1.1.0) Exoplanets v1.1.0 2
      d9abeb0 (tag: first, origin/v1.0) Exoplanets v1.0 3
      a11396e Database v1.0
      e868a90 README
      18ddf3c Initial commit

      1

      The v1.1.1 branch points to the third version of the application.

      2

      The v1.1.0 branch points to the second version of the application.

      3

      The v1.0 branch points to the first version of the application.

  4. Deploy the resource manifests of the first application version.

    1. Switch to the v1.0 branch, which contains the YAML manifests for the first version of the application.

      [student@workstation declarative-manifests]$ git checkout v1.0
      branch 'v1.0' set up to track 'origin/v1.0'.
      Switched to a new branch 'v1.0'
    2. Validate the YAML resource manifest for the application.

      [student@workstation declarative-manifests]$ oc apply -f . \
        --validate=true --dry-run=server
      configmap/database created (server dry run)
      secret/database created (server dry run)
      deployment.apps/database created (server dry run)
      service/database created (server dry run)
      configmap/exoplanets created (server dry run)
      secret/exoplanets created (server dry run)
      deployment.apps/exoplanets created (server dry run)
      service/exoplanets created (server dry run)
      route.route.openshift.io/exoplanets created (server dry run)
    3. Deploy the exoplanets application.

      [student@workstation declarative-manifests]$ oc apply -f .
      configmap/database created
      secret/database created
      deployment.apps/database created
      service/database created
      configmap/exoplanets created
      secret/exoplanets created
      deployment.apps/exoplanets created
      service/exoplanets created
      route.route.openshift.io/exoplanets created
    4. List the deployments and pods. The exoplanets pod can go into a temporary crash loop backoff state if it attempts to access the database before it becomes available. Wait for the pods to be ready. Press Ctrl+C to exit the watch command.

      [student@workstation declarative-manifests]$ watch oc get deployments,pods
      
      Every 2.0s: oc get deployments,pods ...
      
      NAME                       READY   UP-TO-DATE   AVAILABLE   AGE
      deployment.apps/database   1/1     1            1           32s
      deployment.apps/exoplanets 1/1     1            1           32s
      
      NAME                            READY   STATUS    RESTARTS   AGE
      pod/database-6fddbbf94f-2pghj   1/1     Running   0          32s
      pod/exoplanets-64c87f5796-bw8tm 1/1     Running   0          32s
    5. List the route for the exoplanets application.

      [student@workstation declarative-manifests]$ oc get routes -l app=exoplanets
      NAME         HOST/PORT                                                ...
      exoplanets   exoplanets-declarative-manifests.apps.ocp4.example.com   ...
    6. Open the route URL in the web browser. The application version is v1.0.

      http://exoplanets-declarative-manifests.apps.ocp4.example.com/

  5. Deploy the second version of the exoplanets application.

    1. Switch to the v1.1.0 branch of the Git repository.

      [student@workstation declarative-manifests]$ git checkout v1.1.0
      branch 'v1.1.0' set up to track 'origin/v1.1.0'.
      Switched to a new branch 'v1.1.0'
    2. Inspect the changes from the new manifests.

      [student@workstation declarative-manifests]$ oc diff -f .
      ...output omitted...
               - secretRef:
                   name: exoplanets
      -        image: registry.ocp4.example.com:8443/redhattraining/exoplanets:v1.0
      +        image: registry.ocp4.example.com:8443/redhattraining/exoplanets:v1.1.0
               imagePullPolicy: Always
               livenessProbe:
                 failureThreshold: 3

      The new version changes the image that is deployed to the cluster. Because the change is in the deployment, the new manifest produces new pods for the application.

    3. Apply the changes from the new manifests.

      [student@workstation declarative-manifests]$ oc apply -f .
      configmap/database unchanged
      secret/database configured
      deployment.apps/database configured
      service/database configured
      configmap/exoplanets unchanged
      secret/exoplanets configured
      deployment.apps/exoplanets configured
      service/exoplanets unchanged
      route.route.openshift.io/exoplanets configured
    4. List the deployments and pods. Wait for the application pod to be ready. Press Ctrl+C to exit the watch command.

      [student@workstation declarative-manifests]$ watch oc get deployments,pods
      Every 2.0s: oc get deployments,pods ...
      
      NAME                       READY   UP-TO-DATE   AVAILABLE   AGE
      deployment.apps/database   1/1     1            1           6m32s
      deployment.apps/exoplanets 1/1     1            1           6m33s
      
      NAME                            READY   STATUS    RESTARTS   AGE
      pod/database-6fddbbf94f-2pghj   1/1     Running   0          6m33s
      pod/exoplanets-74c85f5796-tw8tf 1/1     Running   0          32s
    5. List the route for the exoplanets application.

      [student@workstation declarative-manifests]$ oc get routes -l app=exoplanets
      NAME         HOST/PORT                                                ...
      exoplanets   exoplanets-declarative-manifests.apps.ocp4.example.com   ...
    6. Open the route URL in the web browser. The application version is v1.1.0.

      http://exoplanets-declarative-manifests.apps.ocp4.example.com/

  6. Deploy the third version of the exoplanets application.

    1. Switch to the v1.1.1 branch of the Git repository.

      [student@workstation declarative-manifests]$ 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. View the differences between the currently deployed version of the application and the updated resource manifests.

      [student@workstation declarative-manifests]$ oc diff -f .
      ...output omitted...
       kind: Secret 1
       metadata:
         annotations:
      ...output omitted...
      -  DB_USER: '*** (before)' 2
      +  DB_USER: '*** (after)'
       kind: Secret
       metadata:
         annotations:

      1

      The secret resource is changed.

      2

      The DB_USER field of the secret resource is changed.

    3. Inspect the current application pods.

      [student@workstation declarative-manifests]$ oc get pods
      NAME                          READY   STATUS    RESTARTS   AGE
      database-6fddbbf94f-brlj6     1/1     Running   0          44m
      exoplanets-674cc57b5d-mv8kd   1/1     Running   0          18m
    4. Deploy the new version of the application.

      [student@workstation declarative-manifests]$ oc apply -f .
      configmap/database unchanged
      secret/database configured
      deployment.apps/database configured
      service/database configured
      configmap/exoplanets unchanged
      secret/exoplanets configured
      deployment.apps/exoplanets unchanged
      service/exoplanets unchanged
      route.route.openshift.io/exoplanets configured
    5. Inspect the current application pods again

      [student@workstation declarative-manifests]$ oc get pods
      NAME                          READY   STATUS             RESTARTS     AGE
      database-6fddbbf94f-brlj6     1/1     Running            0            10m
      exoplanets-674cc57b5d-mv8kd   0/1     CrashLoopBackOff   4 (14s ago)  2m

      Although the secret is updated, the deployed application pods are not changed. These non-updated pods are a problem, because the pods load secrets and configuration maps at startup. Currently, the pods have stale values from the previous configuration, and therefore could crash.

  7. Force the exoplanets application to restart, to flush out any stale configuration data.

    1. Use the oc get deployments command to confirm the deployments.

      [student@workstation declarative-manifests]$ oc get deployments
      NAME         READY   UP-TO-DATE   AVAILABLE   AGE
      database     1/1     1            1           32m
      exoplanets   0/1     1            0           32m
    2. Use the oc rollout command to restart the database deployment.

      [student@workstation declarative-manifests]$ oc rollout restart \
        deployment/database
      deployment.apps/database restarted
    3. Use the oc rollout command to restart the exoplanets deployment.

      [student@workstation declarative-manifests]$ oc rollout restart \
        deployment/exoplanets
      deployment.apps/exoplanets restarted
    4. List the pods. The exoplanets pod can go into a temporary crash loop backoff state if it attempts to access the database before it becomes available. Wait for the application pod to be ready. Press Ctrl+C to exit the watch command.

      [student@workstation declarative-manifests]$ watch oc get pods
      Every 2.0s: oc get deployments,pods ...
      
      NAME                          READY   STATUS    RESTARTS   AGEE
      database-7c767c4bd7-m72nk     1/1     Running   0          32s
      exoplanets-64c87f5796-bw8tm   1/1     Running   0          32s
    5. Use the oc get deployment command with the -o yaml option to view the last-applied-configuration annotation.

      [student@workstation declarative-manifests]$ oc get deployment \
        exoplanets -o yaml
      apiVersion: apps/v1
      kind: Deployment
      metadata:
        annotations:
          deployment.kubernetes.io/revision: "3"
          description: Defines how to deploy the application server
          kubectl.kubernetes.io/last-applied-configuration: |
            {"apiVersion":"apps/v1","kind":"Deployment","metadata":{"annotations":...
          template.alpha.openshift.io/wait-for-ready: "true"
      ...output omitted...
    6. Open the route URL in the web browser. The application version is v1.1.1.

      http://exoplanets-declarative-manifests.apps.ocp4.example.com/

  8. Clean up the resources.

    1. Delete the application resources.

      [student@workstation declarative-manifests]$ oc delete -f .
      configmap "database" deleted
      secret "database" deleted
      deployment.apps "database" deleted
      service "database" deleted
      configmap "exoplanets" deleted
      secret "exoplanets" deleted
      deployment.apps "exoplanets" deleted
      service "exoplanets" deleted
      route.route.openshift.io "exoplanets" deleted
    2. Change to the student HOME directory.

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

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

Revision: do280-4.14-08d11e1