Skip to content

GCP load balancer with autoscale quick setup

Overview

WCS Google Cloud Platform instances support TCP Network load balancer.

WebSocket connections will be distributed between active load balancer instances. In case a scaling policy is executed (when the policy target – e.g., CPU load on instance - is reached) and new instances are launched, they will be added to the load balancer.

The following components would be required

  • Disk image to use in instance template
  • Instance template to create new server instances while autoscaling
  • Autoscale instance group
  • Load balancer
  • Server health checks

Let's try to deploy CDN for WebRTC streams including one Origin server and a group of Edge servers (from 1 to 3 instances) with CPU load autoscaling.

Prepare server instances

  1. Create one Origin and one Edge server as described here. Reserve a static internal IP address to Origin server. Reserve external static IP address to use in load balancer

  2. Configure CDN on Origin server side

    cdn_enabled                             = true
    cdn_ip                                  = <origin_internal_ip>
    cdn_role                                = origin
    cdn_nodes_resolve_ip                    = false
    

  3. Configure CDN on Edge server side

    cdn_enabled                             = true
    cdn_ip                                  = <edge_internal_ip>
    cdn_point_of_entry                      = <origin_internal_ip>
    cdn_role                                = edge
    cdn_nodes_resolve_ip                    = false
    

  4. Add the following parameter to Edge server settings

    http_enable_root_redirect=false
    

  5. Prepare and import SSL certificates to Origin and Egde servers. It is nor recommeded to use Let'sEncrypt because it requires to update Edge disk image every 3 months.

Create Edge disk image

  1. Stop Edge server instance

  2. In Google Cloud console, go to Compute Engine - Images section and click Create image. Choose Edge instance disk as disk image source and click Create

    Do not delete source Edge instance after disk image creation, it will be necessary for Edge disk image updating.

Create Edge instance template

  1. Go to Compute Engine - Instance templates section and click Create instance template. Choose instance VM configuration

  2. In Boot disk section click Change

    On Custom images tab choose Edge disk image

  3. On Security tab add the public SSH key if you do not have project SSH keys and click Create

Create autoscaling group

  1. Go to Compute Engine - Instance groups section and click Create instance group. Choose instance group region and zone, select Edge instance template

  2. Choose Autoscale mode by CPU utilization metric, set metric target value to 80% and set maximum number of instances to 3

  3. Enable Auto healing and create a health check. Set TCP protocol, port 8081 and request /health-check

    Configure health criteria and click Create to return to group setup

  4. Expand Advanced creation options and enable Do not retry machine creation, then click Create

    Autoscaling instance group will be created, and one instance will be launched

Create load balancer

  1. Go to NetworkLoad balancers section and click Create load balancer. Choose TCP Load Balancing

  2. Choose external load balancer From internet to my VMs and its region

  3. In Backend configuration section, on Select existing instance groups tab select Edge instance group and set session affinity to client IP and protocol

  4. Choose Create health check. Create server health check, set port 8081 and request /

  5. In Frontend configuration section create TCP port configurations for ports 8081, 8080, 8443, 8444 for HTTP(S) and WS(S). Set external static IP address to load balancer

    You can add another port configurations (1935 for RTMP subscribers, 8082, 8445 for HLS etc depending on Edge use case)

  6. Click Create. Load balancer will start

Load balancer testing

  1. Go to Origin web interface and publish test stream in Two Way Streaming example

  2. Go to Edge web interface using load balancer IP address. Play the test stream in Player example

Updating Edge servers settings

To update Edge servers settings, for example, to update SSL certificates, Edge disk image must be updated as follows:

  1. Disable autoscaling and delete all Edge instances in Edge instance group

  2. Launch source Edge server instance

  3. Update the settings as needed (for example, update SSL certificates)

  4. Stop source Edge instance

  5. Delete Edge disk image

  6. Create new Edge dick image with the same name (for example, test-edge-image-1)

  7. Enable autoscaling in Edge instance group (autoscaling settings will be preserved)