# Load Balancing

## Overview

In case of high volume application traffic which must be distributed
between multiple VM instances, Zadara Cloud Services supports the definition
of load balancers and target groups.  Load balancers distribute the load
between the VM instances defined in the target group.  This capability
provides the following benefits when compared to working with individual
VM instances:

- Time required to complete a task is reduced.
- Fault tolerance and high availability can be provided in case of VM instance
  failure.

When you create a load balancer, you define a target group of instances
that will share the work of processing requests from an application. The
application then directs its requests to the load balancer, and the
load balancer distributes the work among the instances in the load
balancer’s target group.

See the video demonstrating the basics of creating and configuring
zCompute Load Balancers, Target Groups and Listeners:

<iframe class="embed-responsive-item"
        id="youtubeplayer"
        type="text/html"
        width="640"
        height="390"
        style="border: none"
        src="//www.youtube.com/embed/gq8X5xIjMZI"
        allowfullscreen="allowfullscreen">
</iframe>

## Load Balancer

### Creating a Load Balancer

**To create a load balancer**:

1. Navigate to **Home > Load Balancing > Load Balancers**. The list of
   currently defined load balancers is displayed.
2. To create a new load balancer, click **+ Create** from the top toolbar.
3. In the **Create Load Balancer** dialog which opens, enter the following:
   1. **Name**: The new load balancer’s name.
   2. **Description**: An optional description for the load balancer.
   3. **Type**: The load balancer type determines the basis on which the
      load balancing is done.  The following types are supported:
      * **ALB**: Application Load Balancer does distribution between targets
        based on HTTP/HTTPS sessions.
      * **NLB**: Network Load Balancer does distribution between targets
        based on TCP sessions regardless of the application layer protocol.
   4. **VPC**: From the dropdown, select the VPC to be associated with
      the load balancer.
   5. **Subnet**: From the dropdown, select an existing subnet to be
      associated with the load balancer, or click **+** to define a new
      subnet.
   6. **Elastic IP**: From the dropdown, select an existing Elastic IP
      to be associated with load balancer, or click **+** to define a new
      Elastic IP.
   7. **Security Group**: Determine the Security Groups that are associated
      with the load balancer, on the basis of the following selection:
      * **Standard**: To select Security Groups that are created and
        managed by the user and can be updated at any time.
        It’s the user responsibility to open the listeners’ ports and to
        restrict to specific sources if required.

        From the dropdown, select existing Security Groups to be
        associated with load balancer, or click **+** to define and
        associate a new group.
      * **Managed**: To select Security Groups that are created and
        managed by the Load Balancer service.
        Listeners’ ports will be automatically open to any incoming source.
   8. **Instance Type**: From the dropdown, select the instance type
      (CPU, RAM, boot disk size) to be associated with load balancer and to
      cope with the expected load.
   9. **IP Address**: Optional <place holder>
   10. **High Availability**: Check or uncheck the high availability option.
       When selected, additional load balancers will be created for standby,
       to automatically replace a failed load balancer.
   11. **Tags**: Enter tags that can be used for ease of identifying the
       load balancer and as a filter in searches.
4. Click **Finish**.

<a id="lb-upgrade"></a>

### Upgrading a Load Balancer

From release 25.06.1, zCompute supports TLS 1.3.

When a newer engine revision is available for a load balancer, the load
balancer’s overview shows it as **Upgradable**, and the **Upgrade** button
is visible in the top menu.

Upgrading moves the load balancer to the latest enabled engine revision of
its engine version, and is required to get new engine capabilities such as
TLS 1.3 support.

During the upgrade, the load balancer’s engine instances are rebuilt, and
the load balancer configuration is then applied to them again.
This includes listeners, rules, target groups, and certificates.

**To upgrade a load balancer:**

1. Navigate to **Home > Load Balancing > Load Balancers**, and select the
   load balancer to upgrade.
2. From the load balancer’s actions, select **Upgrade**.

   The load balancer changes to the **Pending** state while the upgrade
   runs, and returns to **Active** when it completes.
3. Verify that the load balancer serves traffic as expected.
   For an HTTPS listener, connect with a TLS 1.3 capable client and
   confirm that TLS 1.3 is negotiated.

## Target Groups

A target group is a group of instances to which a load balancer directs
application traffic. The instances in this group collectively do the
processing work that the application requires.

<a id="lb-creating-target-group"></a>

### Creating a Target Group

**To create a target group:**

1. Navigate to **Home > Load Balancing > Target Groups**.

   The list of currently defined target groups is displayed.
2. To create a new target group, click **+ Create** from the top toolbar.

   The **Create Target Group** dialog will open.
   1. In the **Details** tab, enter the following:
      1. **Name**: The name of the new target group.
      2. **Description**: Optional description of the new target group.
      3. **Protocol**: Select the protocol used by the load balancer to
         access the target group.
         * **HTTP**: For an Application Load Balancer (ALB).
         * **TCP**: For a Network Load Balancer (NLB).
      4. **Default Port**: The target group port used by the load balancer
         to connect to this target group.
      5. **Sticky Session** - By default, a load balancer routes each request
         independently to the registered target group instance with the smallest
         load. However, you can use the sticky session feature to bind a user’s
         session to a specific instance. This ensures that all requests from the
         user during the session are sent to the same instance.
         1. **Duration** - if sticky session is selected, enter session duration
            in seconds during which load balancer should consistently route the
            user’s request to the same VM instance.
   2. Click **Next**.
   3. The **Health Check** tab configurations are used by the load balancer
      to determine whether the target is healthy:
      1. **Port**:
         * **Traffic Port**: The health check connection will be
           established on the port where the target accepts incoming
           traffic.
         * **Custom**: The health check connection will be
           established on a specified port for all targets in the group.

           Enter the Custom port number.
      2. **Interval**: The interval in seconds between health checks of an
         individual VM instance.
         * Minimum: 5 seconds.
         * Maximum: 300 seconds.
      3. **Timeout**: The time in seconds beyond which no response means a
         failed health check. The value must be less than the interval’s
         maximum value.
         * Minimum: 2 seconds.
         * Maximum: 60 seconds.
      4. **Healthy Threshold**: The number of consecutive successful health
         checks needed to transition a VM instance to the healthy state.
         * Minimum: 2 seconds.
         * Maximum: 10 seconds.
      5. **Unhealthy Threshold**: The number of consecutive failed health
         checks needed to transition a VM instance to the failed state.
         * Minimum: 2 seconds.
         * Maximum: 10 seconds.
      6. **HTTP Health Check**: For health checks using the HTTP protocol,
         configure the following:
         1. **Path**: The ping path used by the load balancer to do a
            health check on VM instances in the target group.
         2. **HTTP Code(s)**: The HTTP health check response codes that
            determine a successful health check, and therefore a healthy
            target.

            The following formats are supported:
            * A single HTTP success code, for example, **200**.
            * Multiple HTTP success codes separated by commas, for example
              **200,301**.
            * A range of HTTP success codes, specified with “-” between the
              first and last code in the range, for example, 200-399.
         3. Click **Next**.
   4. In the **Targets** tab, add targets to the group.
      * **Target Type**: Select the Target Type, click **Add**, and enter
        the target’s parameters.

        The following Target Types are supported:
        * **Instance**:

          Enter the Instance type target’s parameters:
          1. **Select Target VM**: From the dropdown, select a target VM instance.
          2. **Port**: Specify the port number on the target for
             communication with the load balancer.
        * **IP**:

          Enter the IP type target’s parameters:
          1. **IP**: Enter the target’s IP address, that is both in your
             region and is reachable from the load balancer.
          2. **Port**: Specify the port number on the target for
             communication with the load balancer.

      1. To configure additional targets in the Target Group click **Add**,
         and enter the target’s parameters according to the Target Type.
      2. Click **Finish**.

## Listeners

A listener is a process that monitors a specified port for incoming
connection requests.
The listener’s rules determine how the load balancer routes these requests
to the targets.

Every load balancer requires at least one listener, each of which must have
one or more rules.

A listener’s rule consists of a condition and a resulting action.
When the condition is met, the load balancer executes the corresponding
action for that rule.

When multiple rules are configured for a listener, they are evaluated
sequentially based on their order.
If the condition of the first rule is not met, the subsequent rules are
assessed in turn.
When a rule’s condition is met and its corresponding action executed, no
further subsequent rule is assessed for the incoming connection request.
The final rule in the sequence includes a default condition indicating that
none of the preceding rules matched the incoming request, and the load
balancer will execute its corresponding action.

The order of all the rules except for the final one can be changed by
dragging and dropping a rule into its preferred position in the sequence.

### Creating a Listener

There are some differences in listener configuration options,
depending on the type of load balancer:

* [Creating a Listener for NLB type Load Balancer](#create-nlb-listener)
* [Creating a Listener for ALB type Load Balancer](#create-alb-listener)

<a id="create-nlb-listener"></a>

### Creating a Listener for NLB type Load Balancer

1. Navigate to **Home > Load Balancing > Target Groups**.

   The list of currently defined target groups is displayed.
2. Select the **NLB** type load balancer for which the listener is being created.
3. In the load balancer’s lower pane’s **Listeners** tab, click **+ Create**.
4. In the **Create Listener** dialog:
   1. **Protocol**: The Protocol is set to **TCP** and cannot be modified.
   2. **Port**: Enter the listener’s port.
   3. **Rule**:
      1. **Action**: The rule’s Action is to **Forward** the incoming
         requests to the Target Group specified in **Forward to** below.
         The Action cannot be modified for an NLB type load balancer’s
         rule.
      2. **Forward to**: Select the destination Target Group from the
         dropdown, or click **+** to create a new one.

         See [Creating a Target Group](#lb-creating-target-group).
   4. Click **Finish**.

<a id="create-alb-listener"></a>

### Creating a Listener for ALB type Load Balancer

1. Navigate to **Home > Load Balancing > Target Groups**.

   The list of currently defined target groups is displayed.
2. Select the **ALB** type load balancer for which the listener is being created.
3. In the load balancer’s lower pane’s **Listeners** tab, click **+ Create**.
4. In the **Create Listener** dialog:
   1. **Protocol**: Select **HTTP** or **HTTPS**.

      For **HTTPS**, select or upload a **Default SSL Certificate**.

      Multiple SSL certificates per target group are supported for LBaaS.
      Each certificate configuration requires a certificate, a private key,
      and a certificate chain.
   2. **Port**: Enter the listener’s port.
   3. **Rules**
      1. Define the listener’s rules, starting with the default rule’s
         action:
      2. To create additional rules, click **+Add Rule**.
         1. Click **+ Add Condition**, select one of the conditions
            from the dropdown and enter its parameter values:

            | **Condition**   | **Parameter values**                                                                                                                          |
            |-----------------|-----------------------------------------------------------------------------------------------------------------------------------------------|
            | **Header**      | Header name and value patterns                                                                                                                |
            | **Path**        | Path patterns                                                                                                                                 |
            | **Host**        | Host names                                                                                                                                    |
            | **Method**      | One or more of the HTTP methods:<br/>* **GET**<br/>* **HEAD**<br/>* **POST**<br/>* **PUT**<br/>* **PATCH**<br/>* **DELETE**<br/>* **OPTIONS** |
            | **IP**          | The CIDR blocks that contain the source IP addresses.<br/><br/>Click **Add CIDR** to add another CIDR block.                                  |
         2. Click **+ Add Condition** to add another condition to the same
            rule.
         3. **Action**: Configure the rule’s action, corresponding to the
            condition.

            Select the applicable action, and enter its parameter values:

            | **Action**         | **Parameter values**                                                                                                                                                                                                                                                                                                                                                                                                                           |
            |--------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
            | **Forward**        | **Forward to**: Select the destination target group                                                                                                                                                                                                                                                                                                                                                                                            |
            | **Fixed Response** | * **Status Code** The HTTP status code value<br/>* **Send Content** (optional).<br/><br/>  If selected, for **Content Type** select one of:<br/>  * **text/plain**<br/>  * **text/css**<br/>  * **text/html**<br/>  * **application/javascript**<br/>  * **application/json**<br/>* **Response body**: Upload the response body file,<br/><br/>  up to a maximum size of 1 KB                                                                  |
            | **Redirect**       | * **Protocol**: Leave it **Unchanged**, or select **HTTP** or  **HTTPS**<br/>* **Port**: Enter the target port number, or select **Unchanged**<br/><br/>  to default to the target’s original configured port<br/>* **Path**: The redirect URL that must be sent to the client’s browser<br/>* **Query**: The query portion of the redirect URL<br/>* **Status Code**:<br/>  * **301** (Moved Permanently)<br/>  * **302** (Moved Temporarily) |
      3. To create an additional rule, click **+Add Rule**.
   4. Click **Finish**.

<a id="lb-configuration-limits"></a>

## Load Balancer Configuration Limits

From version 25.06.1, zCompute Load Balancer supports TLS v1.3 and enforces
limits on the size of a single load balancer’s configuration.
The limits keep each load balancer within a size that the platform can
reliably provision and reconfigure.
The values match the equivalent AWS Elastic Load Balancing quotas, so
existing AWS configurations and expectations carry over.

The limits apply per load balancer, to both Application Load Balancers
(ALB) and Network Load Balancers (NLB), unless stated otherwise.

| Resource                                                         |   ALB | NLB   |
|------------------------------------------------------------------|-------|-------|
| Listeners                                                        |    50 | 50    |
| Rules, excluding default rules                                   |   100 | N/A   |
| Target groups                                                    |   100 | 100   |
| Targets per target group                                         |  1000 | 1000  |
| Total targets across all target groups                           |  1000 | 3000  |
| Additional (SNI) certificates, excluding the default certificate |    25 | N/A   |

Limits are counted as follows:

* **Rules** are counted across all listeners of the load balancer. The
  default rule of each listener is not counted.
* **Target groups** are the distinct target groups that the load
  balancer’s listeners and rules forward to. A target group referenced
  by several listeners or rules is counted once.
* **Total targets** is the sum of the targets in each distinct target
  group attached to the load balancer. A target registered in two target
  groups is counted twice.
* **Additional certificates** are counted across all HTTPS listeners of
  the load balancer.
* A target group can be attached to more than one load balancer.
  Registering targets in such a target group must keep every attached
  load balancer within its limits.

### Exceeding a Limit

The limits are checked when a configuration change is submitted, whether
from the zCompute UI, the Symp CLI, or the AWS-compatible ELB API.

* A request that would exceed any limit is rejected with an HTTP 400
  error that identifies the exceeded limit.
* No part of a rejected request is applied. The load balancer keeps its
  existing configuration and continues to serve traffic.

To stay within the limits, consider the following:

* Consolidate listeners and rules where possible, for example by using
  host-based or path-based conditions on a single ALB listener instead
  of separate listeners.
* Distribute large workloads across multiple load balancers.

### Configuration Validation

Every configuration change to a load balancer is validated before it is
committed. An invalid configuration is rejected with an HTTP 400 error
and rolled back, so an invalid change never reaches the load balancer
and cannot leave it in a broken state.

### Load Balancer States

The **State** column in the load balancer list, and the status on the
load balancer’s overview, reflect the actual health of the load
balancing engine.

A healthy load balancer is shown as **Active**, in green.

The following states are steady states:

| State        | Description                                                                                                                                                                                                                                                                                                             |
|--------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| **Active**   | The load balancer is healthy.<br/>Its engine instances report health, and the configuration applied on<br/>them matches the most recent requested configuration.                                                                                                                                                        |
| **Degraded** | The load balancer serves traffic, but is not fully in sync.<br/>Either the applied configuration lags behind the most recent<br/>requested configuration, or only some of its engine instances<br/>are connected.<br/>The platform retries automatically in the background until the load<br/>balancer is back in sync. |
| **Error**    | The load balancer is unhealthy.<br/>Either its engine stopped reporting health after it was configured,<br/>or none of its engine instances are connected.                                                                                                                                                              |

The following states are transitional, and are shown with a progress
indicator:

| State           | Description                                                                                                          |
|-----------------|----------------------------------------------------------------------------------------------------------------------|
| **Pending**     | A configuration change or an upgrade was requested, and is<br/>waiting to be processed.                              |
| **Spawning**    | The load balancer’s engine instances are being created.                                                              |
| **Configuring** | The configuration is being applied to the engine instances.                                                          |
| **Upgrading**   | The engine instances are being rebuilt to a newer engine revision.<br/>See [Upgrading a Load Balancer](#lb-upgrade). |
| **Deleting**    | The load balancer is being deleted.                                                                                  |
