> For the complete documentation index, see [llms.txt](https://audomsak.gitbook.io/developer-advocacy-2022/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://audomsak.gitbook.io/developer-advocacy-2022/application-deployment/microservices-deployment/villain-microservice.md).

# Villain Microservice

## Create a ConfigMap object to store application configurations

1. Click on ![add](https://1365375534-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYKKHX3vDr97cJAqTjZDZ%2Fuploads%2Fgit-blob-2053c9da0b621063f466689f6d54afaaac341907%2Fadd-icon.png?alt=media) icon located at top right corner of web console.
2. Copy this YAML snippet to the editor and click **Create** button.

   *YAML snippet:*

   ```yaml
   apiVersion: v1
   kind: ConfigMap
   metadata:
     labels:
       app: rest-villains
       application: villains-service
       system: quarkus-super-heroes
     name: rest-villains-config
   data:
     quarkus.hibernate-orm.database.generation: validate
     quarkus.hibernate-orm.sql-load-script: no-file
     quarkus.datasource.jdbc.url: jdbc:otel:postgresql://villains-db:5432/villains_database
     quarkus.opentelemetry.tracer.exporter.otlp.endpoint: http://otel-collector:4317
     quarkus.http.access-log.enabled: 'false'
     quarkus.http.access-log.pattern: >
       %h %l %u %t "%r" %s %b "User-Agent: %{i,User-Agent}" "x-request-id:
       %{i,x-request-id}" "x-b3-traceid: %{i,x-b3-traceid}"  " Referer:
       %{i,Referer}"
   ```

   ![Create a ConfigMap object](https://1365375534-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYKKHX3vDr97cJAqTjZDZ%2Fuploads%2Fgit-blob-d9bd5e6bf3dfb7bb71c5e5adb1ff49f1b0397e06%2Fdeploy-1.png?alt=media)
3. A **rest-villains-config** ConfigMap object should be created.

   ![Create a ConfigMap object](https://1365375534-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYKKHX3vDr97cJAqTjZDZ%2Fuploads%2Fgit-blob-b9ae725f3a4adf2af8719bb9182f68bc82998a7f%2Fdeploy-2.png?alt=media)

## Create a Secret object to store the secret configurations

1. Click on ![add](https://1365375534-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYKKHX3vDr97cJAqTjZDZ%2Fuploads%2Fgit-blob-2053c9da0b621063f466689f6d54afaaac341907%2Fadd-icon.png?alt=media) icon located at top right corner of web console.
2. Copy this YAML snippet to the editor and click **Create** button.

   *YAML snippet:*

   ```yaml
   apiVersion: v1
   kind: Secret
   metadata:
     labels:
       app: rest-villains
       application: villains-service
       system: quarkus-super-heroes
     name: rest-villains-config-creds
   data:
     quarkus.datasource.username: c3VwZXJiYWQ=
     quarkus.datasource.password: c3VwZXJiYWQ=
   type: Opaque
   ```

   ![Create a Secret object](https://1365375534-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYKKHX3vDr97cJAqTjZDZ%2Fuploads%2Fgit-blob-7f2a285b5adf19c7deb2fd53b5b0c0d2a25e0ae9%2Fdeploy-3.png?alt=media)
3. A **rest-villains-config-creds** Secret object should be created.

   ![Create a Secret object](https://1365375534-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYKKHX3vDr97cJAqTjZDZ%2Fuploads%2Fgit-blob-982957e65afd197fecd46e1449602dcdfb06cc5e%2Fdeploy-4.png?alt=media)

## Deploy application using OpenShift Build - Source-to-Image (S2I)

1. Go to **Topology** menu, right click on view area then select **Add to Project -> Import from Git** menu.

   ![Deploy application container](https://1365375534-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYKKHX3vDr97cJAqTjZDZ%2Fuploads%2Fgit-blob-4b9aba24008616f88a78be9fd30a85ecd2c40db5%2Fdeploy-5.png?alt=media)
2. Click **Show advanced Git options** link then enter following inputs. OpenShift Build (S2I) will check the project e.g. source code, framework and select builder image for build/compile application for you.

   * **Git Repo URL:** `https://gitlab.com/s.audomsak/developer-advocacy-2022`
   * **Context dir:** `code/rest-villains`
   * **Application:** `villains-service`
   * **Name:** `rest-villains`

   ![Deploy application container](https://1365375534-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYKKHX3vDr97cJAqTjZDZ%2Fuploads%2Fgit-blob-fff414bfc48beecaaa8eafb7c6d7c34738161426%2Fdeploy-6.png?alt=media)
3. Scroll down to the bottom of the page, select **8080** option for **Target port**, uncheck the **Create a route** checkbox, and then click **Health checks** link.

   ![Deploy application container](https://1365375534-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYKKHX3vDr97cJAqTjZDZ%2Fuploads%2Fgit-blob-95b0a2bd87fb9bbba861ffb1985aba8654f123fe%2Fdeploy-7.png?alt=media)
4. Click **Add Readiness probe** link.

   ![Deploy application container](https://1365375534-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYKKHX3vDr97cJAqTjZDZ%2Fuploads%2Fgit-blob-614a6b075ae171daddc64228618fcfdb38c1d555%2Fdeploy-8.png?alt=media)
5. Enter following inputs then click :heavy\_check\_mark: icon.

   * **Path:** `/q/health/ready`
   * **Port:** `8080`
   * **Period:** `30`
   * **Timeout:** `10`

   ![Deploy application container](https://1365375534-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYKKHX3vDr97cJAqTjZDZ%2Fuploads%2Fgit-blob-d4359bd442b715d4ebbb115426a28d26d966ba75%2Fdeploy-9.png?alt=media)
6. Click **Add Liveness probe** link.

   ![Deploy application container](https://1365375534-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYKKHX3vDr97cJAqTjZDZ%2Fuploads%2Fgit-blob-9e51701d8e05ce63be85f4e2f68618347af9f50f%2Fdeploy-10.png?alt=media)
7. Enter following inputs then click :heavy\_check\_mark: icon.

   * **Path:** `/q/health/live`
   * **Port:** `8080`
   * **Period:** `30`
   * **Timeout:** `10`

   ![Deploy application container](https://1365375534-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYKKHX3vDr97cJAqTjZDZ%2Fuploads%2Fgit-blob-3418b2ed344c3ee1bfdc3f0b572473fac8bef21b%2Fdeploy-11.png?alt=media)
8. Click **Build configuration** link.

   ![Deploy application container](https://1365375534-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYKKHX3vDr97cJAqTjZDZ%2Fuploads%2Fgit-blob-d3a17c54d2f4dfc1445009d3d6104ae8630e0738%2Fdeploy-12.png?alt=media)
9. Enter following environment variable then click **Deployment** link.

   * **Name:** `MAVEN_MIRROR_URL`
   * **Value:** `http://nexus.nexus.svc.cluster.local:8081/repository/maven-public/`

   Note. This will tell Maven in the builder image to use Nexus deployed on the cluster as a Maven Repository instead to speed up build process.

   ![Deploy application container](https://1365375534-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYKKHX3vDr97cJAqTjZDZ%2Fuploads%2Fgit-blob-5763a4b3ea5e8c49ce51c53e1413f271bbdafda0%2Fdeploy-13.png?alt=media)
10. Enter following environment variable. Then click **Resource limits** link.

    * **Name:** `KUBERNETES_NAMESPACE`
    * **Value:** `userX-super-heroes` where `X` is your user number.

    ![Deploy application container](https://1365375534-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYKKHX3vDr97cJAqTjZDZ%2Fuploads%2Fgit-blob-ae02cc5b1423f1d86d0b0a44068b54d45850d5e4%2Fdeploy-14.png?alt=media)
11. Enter following memory request and limit. Then click **Labels** link.

    * **Request:** `256`
    * **Limit:** `768`

    ![Deploy application container](https://1365375534-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYKKHX3vDr97cJAqTjZDZ%2Fuploads%2Fgit-blob-3900512ed348689de9de49ada97f0bc424076e80%2Fdeploy-15.png?alt=media)
12. Enter `system=quarkus-super-heroes` then click **Create** button.

    ![Deploy application container](https://1365375534-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYKKHX3vDr97cJAqTjZDZ%2Fuploads%2Fgit-blob-76f258f058da57e680497c027f549daac166c168%2Fdeploy-16.png?alt=media)

## Add application configurations in ConfigMap and Secret objects to application container

1. Click on **rest-villains** entity in view area, a panel will show on right hand side. Notice that a build process starts automatically to build source code and container image (you can click **View logs** to see build logs). By the way, just let it run in background for now. Then click **rest-villains** link.

   ![Deploy application container](https://1365375534-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYKKHX3vDr97cJAqTjZDZ%2Fuploads%2Fgit-blob-46a5fb90cae454d73b15f06a735993c4401c5903%2Fdeploy-17.png?alt=media)
2. Go to **Environment** tab to add environment variables to application container. Then click **Add all from ConfigMap or Secret** link.

   ![Deploy application container](https://1365375534-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYKKHX3vDr97cJAqTjZDZ%2Fuploads%2Fgit-blob-4ab52737508a6d47e6543f8717f12ed39a626d81%2Fdeploy-18.png?alt=media)
3. Select **rest-villains-config** ConfigMap and **rest-villains-config-creds** Secret objects, then click **Save** button. And then go to **Topology** menu.

   ![Deploy application container](https://1365375534-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYKKHX3vDr97cJAqTjZDZ%2Fuploads%2Fgit-blob-bf6876eca79585260c828e7bef3e444e0750aa0e%2Fdeploy-19.png?alt=media)
4. Wait for a while until you see dark blue ring surrounds the **rest-villains** entity. Hover mouse pointer over it, a bubble should show up and say *1 Running* which means there is 1 pod running.

   ![Deploy application container](https://1365375534-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYKKHX3vDr97cJAqTjZDZ%2Fuploads%2Fgit-blob-574b1418ea35a1c778f594de932d1363de5804fe%2Fdeploy-23.png?alt=media)

## What have you learnt?

1. How to create ConfigMap object with YAML to keep all application configurations.
2. How to create Secret object with YAML to keep all secret application configurations.
3. How to deploy application with existing source codes using OpenShift Build - Source-to-Image (S2I) with following additional configurations:
   * Application health checks e.g. liveness and readiness probes
   * Require and limit resource i.e. memory for application
   * Environment variable for application container
   * Environment variable for S2I build i.e. Maven Repository setting
   * User-defined label
4. Bind configurations from ConfigMap and Secret objects to application container via environment variables.
5. Change network port to be exposed for application container.
