How to Configure Load Balancer in a multiple applications

Goal

This guide will explain how to configure a simple load balancing setup in a Single Application at Platform.sh.

The critical point is that this Load balancer configuration happens transparently, that is, the application does not know about the LB.

For that to work, it is very important that your application follows good practices. For example, ensure that your application is stateless. Being stateless is a good practice for having your application in the cloud.

One of the advantages of stateless services is that you can bring up multiple instances of your application. Since there’s no per-instance state, any instance can handle any request. This is a natural fit for load balancers, which can be leveraged to help scale the service. They’re also readily available on cloud platforms.

Assumptions

  • You either have java Applications, and you want to run at Platform.sh or you already have a Java application running at Platform.sh

  • A text editor of your choice.

Steps

Given that you already have a multi-application running in with .platform/applications.yaml The next step is “copy/paste” the information that you want the replicated machine. In this sample, we’ll only overwrite the name.

- &appdef
    name: app
    type: 'java:8'
    disk: 1024
    source:
        root: app
    hooks:
        build: mvn clean install
    mounts:
        'server/':
            source: local
            source_path: server_source
    web:
        commands:
            start: |
                cp target/dependency/webapp-runner.jar server/webapp-runner.jar
                cp target/tomcat.war server/tomcat.war
                cd server && java -jar -Xmx$(jq .info.limits.memory /run/config.json)m -XX:+ExitOnOutOfMemoryError webapp-runner.jar --port $PORT tomcat.war

-
   <<: *appdef
   name: applb

-
    &app2def
        <<: *appdef
        name: app2
        source:
            root: app2
        web:
          commands:
            start: |
              cp target/dependency/webapp-runner.jar server/webapp-runner.jar
              cp target/tomcat.war server/tomcat.war
              cd server && java -jar -Xmx$(jq .info.limits.memory /run/config.json)m webapp-runner.jar --port $PORT  --path app2 tomcat.war

-
    <<: *app2def
    name: app2lb

In the .platform/services.yaml file we’ll create one varnish as load balancer to each application.

lb1:
    type: varnish:6.0
    relationships:
        server1: 'app:http'
        server2: 'applb:http'
    configuration:
        vcl: !include
            type: string
            path: config.vcl

lb2:
    type: varnish:6.0
    relationships:
        server1: 'app2:http'
        server2: 'app2lb:http'
    configuration:
        vcl: !include
            type: string
            path: config.vcl

Yes, you can have one load balance strategy to each application, but be aware of creating one CVL template file and strategy and updating it the path parameter in the .platform/services.yaml

The route:

"https://{default}/":
  type: upstream
  upstream: "lb1:http"

"https://{default}/app2":
  type: upstream
  upstream: "lb2:http"

"https://www.{default}/":
  type: redirect
  to: "https://{default}/"

You can check the different types of load-balancing thatwe support on this post.

References: