Bookmark this page

Lab: Deploying Multi-container Applications

Create a Helm chart to deploy a multi-container application.

Customize a multi-container application for different environments with Kustomize.

Outcomes

  • Create a Helm chart from manifest files.

  • Use Kustomize for local updates on a Helm chart.

As the student user on the workstation machine, use the lab command to prepare your environment for this exercise, and to ensure that all required resources are available.

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

Instructions

The lab script copies the Helm chart files for the famous-quotes application, which uses a Redis data store.

In this lab, you are asked to create a Helm chart by using the files that the exercise provides and later using Kustomize to apply customization to the rendered chart. You are expected to work with Helm locally, without the need for a Helm repository.

  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 multicontainer-review project.

      [student@workstation ~]$ oc project multicontainer-review
      Already on project "multicontainer-review" on server "https://api.ocp4.example.com:6443".
  2. Create a Helm chart from the files in the ~/DO288/labs/multicontainer-review/famous-quotes directory.

    1. In a terminal window, change to the famous-quotes application directory.

      [student@workstation ~]$ cd ~/DO288/labs/multicontainer-review/famous-quotes
    2. Move the files in the famous-quotes directory according to the Helm chart layout.

      First, move the chart template files to the templates directory.

      [student@workstation famous-quotes]$ mv \
       configmap.yaml \
       deployment.yaml \
       route.yaml \
       service.yaml \
      templates/

      Then, move the pre-downloaded chart dependency to the charts directory.

      [student@workstation famous-quotes]$ mv redis-persistent-0.0.1.tgz charts/

      When you run the tree command you must get the following output:

      [student@workstation famous-quotes]$ tree ./
      ./
      ├── charts
      │   └── redis-persistent-0.0.1.tgz
      ├── Chart.yaml
      ├── templates
      │   ├── configmap.yaml
      │   ├── deployment.yaml
      │   ├── route.yaml
      │   └── service.yaml
      └── values.yaml
      
      3 directories, 7 files
    3. Verify that the famous-quotes templates and the Redis chart are in the correct location.

      [student@workstation famous-quotes]$ helm template ./ | grep  "# Source:" | sort
      # Source: famous-quotes/charts/redis/templates/deploymentconfig.yaml
      # Source: famous-quotes/charts/redis/templates/persistentvolumeclaim.yaml
      # Source: famous-quotes/charts/redis/templates/secret.yaml
      # Source: famous-quotes/charts/redis/templates/service.yaml
      # Source: famous-quotes/charts/redis/templates/tests/test-redis-connection.yaml
      # Source: famous-quotes/templates/configmap.yaml
      # Source: famous-quotes/templates/deployment.yaml
      # Source: famous-quotes/templates/route.yaml
      # Source: famous-quotes/templates/service.yaml
  3. Update the Helm templates to use the values in the quotes key of the values.yaml file.

    Add the following features to the chart:

    • Parameterize the famous-quotes image name.

    • Use the quotes.import.enabled value to optionally add the volume and the import configuration to the famous-quotes deployment.

    • Use the quotes.import.enabled value to optionally create the configuration map that contains the import data.

    1. Edit the templates/deployment.yaml template to use the quotes.image value.

      ...output omitted...
          spec:
            containers:
            - name: famous-quotes
              image: {{ .Values.quotes.image }}
      ...output omitted...
    2. Edit the templates/deployment.yaml template to make the data import configuration and volume optional.

      ...output omitted...
              - name: DATASTORE_PASS
                valueFrom:
                  secretKeyRef:
                    key: database-password
                    name: {{ .Values.redis.database_service_name }}
            {{- if .Values.quotes.import.enabled }}
              - name: QUOTES_IMPORT_PATH
                value: /tmp/quotes/import_quotes.csv
              volumeMounts:
                - name: import-volume
                  mountPath: /tmp/quotes
            volumes:
              - name: import-volume
                configMap:
                  name: quotes-import-data
            {{- end }}
    3. Verify that the updates to the templates/deployment.yaml file render correctly.

      Verify that the image value displays and that there is no data import configuration.

      [student@workstation famous-quotes]$ helm template ./ \
      --set quotes.import.enabled=false \
      | grep -A 28 "famous-quotes/templates/deployment.yaml"
      # Source: famous-quotes/templates/deployment.yaml
      apiVersion: apps/v1
      kind: Deployment
      metadata:
        name: famous-quotes
      ...output omitted...
            containers:
            - name: famous-quotes
              image: registry.ocp4.example.com:8443/redhattraining/ocpdev-redis-quotes:4.12
              imagePullPolicy: Always
              env:
              - name: DATASTORE_HOST
                value: quotes-store
              - name: DATASTORE_PASS
                valueFrom:
                  secretKeyRef:
                    key: database-password
                    name: quotes-store
      ---
      # Source: famous-quotes/charts/redis/templates/deploymentconfig.yaml
    4. Edit the templates/configmap.yaml template to use the quotes.import.enabled value to conditionally create the quotes-import-data configuration map.

      {{- if .Values.quotes.import.enabled }}
      apiVersion: v1
      kind: ConfigMap
      metadata:
        name: quotes-import-data
      data:
        import_quotes.csv: |-
          id|quote|author
      ...output omitted...
          8|Those who can imagine anything, can create the impossible.|Alan Turing
      {{- end }}
    5. Run the helm template with the quotes.import.enabled parameter set to false to verify that the configuration map does not render.

      [student@workstation famous-quotes]$ helm template ./ \
      --set quotes.import.enabled=false \
      | grep "# Source\|templates/configmap.yaml"
      # Source: famous-quotes/charts/redis/templates/secret.yaml
      # Source: famous-quotes/charts/redis/templates/persistentvolumeclaim.yaml
      # Source: famous-quotes/charts/redis/templates/service.yaml
      # Source: famous-quotes/templates/service.yaml
      # Source: famous-quotes/templates/deployment.yaml
      # Source: famous-quotes/charts/redis/templates/deploymentconfig.yaml
      # Source: famous-quotes/templates/route.yaml
      # Source: famous-quotes/charts/redis/templates/tests/test-redis-connection.yaml

      Optionally, run the previous command with the quotes.import.enabled=true parameter to verify that the configuration map shows in the output.

  4. Use the output of the local famous-quotes chart render to create the ~/DO288/labs/multicontainer-review/kustomized-quotes/base/app.yaml file.

    Use the --skip-tests option for the helm template command to avoid rendering the chart in the app.yaml file.

    [student@workstation famous-quotes]$ helm template \
    --skip-tests ./ > ../kustomized-quotes/base/app.yaml
  5. Add the required files to the kustomized-quotes directory to have working staging and production customizations for the famous-quotes application.

    The ~/DO288/labs/multicontainer-review/kustomized-quotes directory contains the base and overlays directory convention, but is missing some files that Kustomize requires.

    kustomized-quotes
    ├── base
    │   └── app.yaml # Generated in the Helm part of the exercise
    └── overlays
        ├── production
        │   └── app_dimensioning.yaml
        └── staging
            └── app_dimensioning.yaml
    1. Change to the kustomized-quotes directory.

      [student@workstation famous-quotes]$ cd \
      ~/DO288/labs/multicontainer-review/kustomized-quotes
    2. Create the base/kustomization.yaml file with the following content:

      resources:
      - app.yaml
    3. Verify that you can run oc kustomize with the base directory.

      [student@workstation kustomized-quotes]$ oc kustomize base/
      apiVersion: v1
      data:
        import_quotes.csv: |-
      ...output omitted...
    4. Create the overlays/staging/kustomization.yaml file with the following content:

      bases:
      - ../../base
      patches:
      - staging_dimensioning.yaml
    5. Verify that you can run oc kustomize with the overlays/staging directory.

      [student@workstation kustomized-quotes]$ oc kustomize overlays/staging
      apiVersion: v1
      data:
        import_quotes.csv: |-
          id|quote|author
      ...output omitted...
    6. Create the overlays/production/kustomization.yaml file with the following content:

      bases:
      - ../../base
      patches:
      - prod_dimensioning.yaml
    7. Verify that you can run oc kustomize with the overlays/production directory.

      [student@workstation kustomized-quotes]$ oc kustomize overlays/production
      apiVersion: v1
      data:
        import_quotes.csv: |-
          id|quote|author
      ...output omitted...
    8. Verify the directory structure.

      [student@workstation kustomized-quotes]$ tree ./
      ./
      ├── base
      │   ├── app.yaml
      │   └── kustomization.yaml
      └── overlays
          ├── production
          │   ├── kustomization.yaml
          │   └── prod_dimensioning.yaml
          └── staging
              ├── kustomization.yaml
              └── staging_dimensioning.yaml
      
      4 directories, 6 files
  6. Deploy the staging version of the application.

    1. Run the following command to deploy the staging version of the application.

      [student@workstation kustomized-quotes]$ oc apply -k overlays/staging
    2. Verify that the application and the Redis pods are running.

      [student@workstation kustomized-quotes]$ oc get po
      NAME                            READY   STATUS      RESTARTS      AGE
      famous-quotes-d64ffc75f-kwzgw   1/1     Running     2 (36s ago)   59s
      quotes-store-1-deploy           0/1     Completed   0             58s
      quotes-store-1-sjv7p            1/1     Running     0             55s
    3. Use the curl command to test that the application works.

      [student@workstation kustomized-quotes]$ curl \
      famous-quotes-multicontainer-review.apps.ocp4.example.com/quotes/5
      {
        "quote" : "Imagination is more important than knowledge.",
        "author" : "Albert Einstein",
        "_links" : {
          "self" : {
            "href" : "http://famous-quotes-multicontainer-review.apps.ocp4.example.com/quotes/5"
          },
          "quote" : {
            "href" : "http://famous-quotes-multicontainer-review.apps.ocp4.example.com/quotes/5"
          }
        }
      }

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

Revision: do288-4.12-0d49506