Deployment

The main deployment tool used for My Thai Star is be Docker.

docker

It is a tool to run application in isolated environments. Those isolated environments will be what we call Docker containers. For instance, it won’t be necessary any installation of nginx or Apache tomcat or anything necessary to deploy, because there will be some containers that actually have those technologies inside.

Where Docker containers will be running?

Of course, it is necessary to have an external Deployment Server. Every Docker process will run in it. It will be accessed from Production Line pipelines via SSH. Thus, the pipeline itself will manage the scenario of, if every previous process like testing passes as OK, stop actual containers and create new ones.

This external server will be located in http://de-mucdevondepl01 .

Container Schema

3 Docker containers are being used for the deployment of My Thai Star:

  1. nginx for the Reverse Proxy

  2. tomcat for the Java Server

  3. nginx for the Angular Client

The usage of the Reverse Proxy will allow the client to call via /api every single Java Server’s REST operation. Moreover, there will only be 1 port in usage in the remote Docker host, the one mapped for the Reverse Proxy: 8080. Besides the deployment itself using nginx and tomcat, both client and server are previously built using nodejs and maven images. Artifacts produced by them will be pasted in servers' containers using multi-stage docker builds. It will all follow this schema:

36028242 8998f41c 0d9e 11e8 93b3 6bfe50152bf8

This orchestration of all 3 containers will be done by using a docker-compose.yml file. To redirect traffic from one container to another (i.e. reverse-proxy to angular client or angular client to java server) will be done by using, as host names, the service name docker-compose defines for each of them, followed by the internally exposed port:

A implementation using Traefik as reverse proxy instead of NGINX is also available.

Run My Thai Star

The steps to run My Thai Star are:

  1. Clone the repository $ git clone https://github.com/devonfw/my-thai-star.git

  2. Run the docker compose command: $ docker-compose up

Deployment Pipelines

As PL does not support deployments, we have created separate pipelines for this purpose. Those pipelines are: MyThaiStar_REVERSE-PROXY_DEPLOY, MyThaiStar_FRONTEND_DEPLOY and MyThaiStar_SERVER_DEPLOY.

The application will be deployed using docker on a remote machine. The architecture is as follows:

deployment arch

The parts to be deployed are: an NGINX reverse proxy, the java application and the angular application.

MyThaiStar_SERVER_DEPLOY Pipeline

Deploys on the server the Java part of My Thai Star.

Parameters
  • registryUrl: The url to the docker registry where the image is stored.

  • registryCredentialsId: Credentials to publish/download images from registry.

  • dockerNetwork: Network of your My Thai Star application. You can deploy several versions of MTS in the same server by changing the dockerNetwork.

  • VERSION: The version that you can to deploy.

Pipeline steps
  • Create docker network: Create the docker network with the name provided as parameter.

  • Deploy new image: Deploy a new java container. If it already exists, first it delete the previous one.

MyThaiStar_FRONTEND_DEPLOY

Deploys on the server the Angular part of My Thai Star

Parameters
  • registryUrl: The url to the docker registry where the image is stored.

  • registryCredentialsId: Credentials to publish/download images from registry.

  • dockerNetwork: Network of your My Thai Star application. You can deploy several versions of MTS in the same server by changing the dockerNetwork.

  • VERSION: The version that you can to deploy.

Pipeline steps
  • Create docker network: Create the docker network with the name provided as parameter.

  • Deploy new image: Deploy a new angular container. If it already exists, first it delete the previous one.

MyThaiStar_REVERSE-PROXY_DEPLOY Pipeline
As reverse proxy connects to the Java and Angular application, both must be deployed before you execute this pipeline.

The MyThaiStar_REVERSE-PROXY_DEPLOY pipeline will deploy the My Thai Star reverse proxy into a remote machine using docker.

Parameters
  • registryUrl: The url to the docker registry where the image is stored.

  • registryCredentialsId: Credentials to publish/download images from registry.

  • buildReverseProxy: If yes, it will build and publish a new version of reverse-proxy.

  • port: Port of the MTS application. You must ensure that those port is available in the deployment machine.

  • dockerNetwork: Network of your My Thai Star application. You can deploy several versions of MTS in the same server by changing the port and the dockerNetwork.

  • VERSION: The version that you can to deploy.

Pipeline steps
  • Create docker network: Create the docker network with the name provided as parameter.

  • Create the Docker image: If buildReverseProxy is enabled, this step will create a new docker image and publish it to the docker registry.

  • Deploy new image: Deploy a new reverse proxy container. If it already exists, first it delete the previous one.

Deployment Strategies

In this chapter different way of deploying My Thai Star are explained. Everything will be based in Docker.

Independent Docker containers

The first way of deployment will use isolated Docker containers. That means that if the client-side container is deployed, it does not affect the server-side container’s life cycle and vice versa.

Let’s show how the containers will behave during their life cycle.

  • 0) Copy everything you need into the Deployment Server directory

  • 1) Remove existing container (Nginx or Tomcat)

    container1
  • 2) Run new one from the Docker images collection of the external Deployment Server.

    container2
  • 3) Add the artifact /dist to the "deployable" folder of the Docker container (/usr/share/nginx/html/)

    container3

    Now, let’s see how it’s being executed in the command line (simplified due to documentation purposes). The next block of code represents what is inside of the last stage of the Pipeline.

    sshagent (credentials: ['my_ssh_token']) {
        sh """
            // Copy artifact from workspace to deployment server
    
            // Manage container:
            docker rm -f [mts-container]
            docker run -itd --name=[mts-container] [base_image]
            docker exec [mts-container] bash -C \\"rm [container_deployment_folder]/*\\"
            docker cp [artifact] [mts-container]:[container_deployment_folder]
        """
    }

    For every operation performed in the external Deployment Server, it is necessary to define where those commands are going to be executed. So, for each one of previous docker commands, this should appear before:

    ssh -o StrictHostKeyChecking=no root@10.40.235.244
Docker Compose

The second way of deployment will be by orchestrating both elements of the application: The Angular client-side and the Java server-side. Both elements will be running in Docker containers as well, but in this case they won’t be independent anymore. Docker Compose will be in charge of keeping both containers up, or to put them down.

Project adjustment

In order to perform this second way of deployment, some files will be created in the project. The first one is the Dockerfile for the Angular client-side. This file will pull (if necessary) an nginx Docker image and copy the Angular artifact (/dist folder) inside of the deployment folder of the image. It will be located in the main directory of the Angular client-side project.

dockerfile angular

The second file is the Dockerfile for the Java server-side. Its function will be quite similar to the Angular one. It will run a tomcat Docker image and copy the Java artifact (mythaistar.war file) in its deployment folder.

dockerfile java

Finally, as long as the docker-compose is being used, a file containing its configuration will be necessary as well. A new folder one the main My That Star’s directory is created, and it’s called /docker. Inside there is just a docker-compose.yml file. It contains all the information needed to orchestrate the deployment process. For example, which port both containers are going to be published on, and so on. This way of deployment will allow the application to be published or not just with one action.

docker-compose rm -f            # down
docker-compose up --build -d    # up fresh containers
docker compose

Let’s have a look at the file itself:

version: '3'
services:
  client_compose:
    build: "angular"
    ports:
      - "8091:80"
    depends_on:
      - server_compose
  server_compose:
    build: "java"
    ports:
      - "9091:8080"

This Orchestrated Deployment will offer some interesting possibilities for the future of the application.

Future Deployment

The My Thai Star project is going to be built in many technologies. Thus, let’s think about one deployment schema that allow the Angular client to communicate to all three back ends: Java, Node and .NET.

As long as Docker containers are being used, it shouldn’t be that hard to deal with this "distributed" deployment. The schema represents 6 Docker containers that will have client-side(s) and server-side(s). Each of 3 Angular client containers (those in red) are going to communicate with different back-ends. So, when the deployment is finished, it would be possible to use all three server-sides just by changing the "port" in the URL.

Let’s see how it would look like:

deployment schema
Reverse proxy strategy using Traefik

This implementation is the same as described at My Thai Star deployment wiki page. The only thing that changes is that Traefik is used instead of NGINX.

Using Traefik as reverse proxy, we can define the routes using labels in the docker containers instead of using a nginx.conf file. With this, it is not necessary to modify the reverse proxy container for each application. In addition, as Traefik is listening to the docker daemon, it can detect new containers and create routes for them without rebooting.

Example of labels:

labels:
    - "traefik.http.routers.angular.rule=PathPrefix(`/`)"
    - "traefik.http.services.angular.loadBalancer.healthcheck.path=/health"
    - "traefik.http.services.angular.loadBalancer.healthcheck.interval=10s"
    - "traefik.http.services.angular.loadBalancer.healthcheck.scheme=http"
How to use it

If you want to build the images from code, change to My Thai Star root folder and execute:

$ docker-compose -f docker-compose.traefik.yml up -d --build

If you want to build the images from artifacts, change to traefik folder (reverse-proxy/traefik) and execute:

$ docker-compose up -d --build

After a seconds, when the healthcheck detects that containers are running, your application will be available at http://localhost:8090. Also, the Traefik dashboard is available at http://localhost:8080.

If you want to check the behaviour of the application when you scale up the backend, you can execute:

$ docker-compose scale java=5

With this, the access to the java backend will be using the load balacing method: Weighted Round Robin.

Last updated 2021-10-21 17:17:16 UTC