Skip to content

Create an HTTPS load balancer

Use an HTTPS listener to terminate TLS at the load balancer and route HTTP requests to your application’s backend service. This example attaches two HTTP routes to one listener: web.example.com sends requests to web-backend, and api.example.com sends requests to api-backend. Both hostnames share one public IP and port 443.

  • An installed and authenticated evroc CLI, with the project and region selected
  • An existing virtual machine (VM), myvm, serving HTTP on port 8080
  • A second VM, apivm, serving HTTP on port 8080, with responses you can distinguish from myvm
  • Network access from the load balancer to the VM’s backend port; see Configure security groups
  • A PEM certificate chain covering both web.example.com and api.example.com, and its matching private key

Replace web.example.com and api.example.com with your applications’ hostnames, and fullchain.pem and privkey.pem with the paths to your certificate chain and private key. The chain must contain the leaf certificate followed by the required intermediates, without the root CA certificate. See Manage load balancer certificates for supported keys and renewal instructions.

For reference flags such as --backend-service-ref, short names resolve in the selected project and region. These flags also accept full resource references.

Add each VM to a separate backend pool:

Terminal window
evroc loadbalancer backendpool create web-pool \
--backend-ref=myvm
evroc loadbalancer backendpool create api-pool \
--backend-ref=apivm

Create a service for each application’s HTTP port. Backend IP selection defaults to IPv4:

Terminal window
evroc loadbalancer backendservice create web-backend \
--backend-pool-ref=web-pool \
--port=8080
evroc loadbalancer backendservice create api-backend \
--backend-pool-ref=api-pool \
--port=8080

Configure an HTTP health check using a path your application serves:

Terminal window
evroc loadbalancer backendservice sethealthcheckhttp web-backend \
--path=/ready
evroc loadbalancer backendservice sethealthcheckhttp api-backend \
--path=/ready

Create the empty certificate resource, then upload its content:

Terminal window
evroc loadbalancer certificate create web-cert
evroc loadbalancer certificate upload web-cert \
--certificate-file=fullchain.pem \
--private-key-file=privkey.pem

The service validates the certificate and key. Check that the certificate’s Ready condition is True:

Terminal window
evroc loadbalancer certificate get web-cert -o yaml

Create a separate route for each hostname:

Terminal window
evroc loadbalancer httproute create web-route \
--hostname=web.example.com \
--backend-service-ref=web-backend
evroc loadbalancer httproute create api-route \
--hostname=api.example.com \
--backend-service-ref=api-backend

Each route contains one catch-all rule named default, restricted to its hostname. You can manage each route’s rules independently; see Route HTTP requests.

Create the load balancer and HTTPS listener

Section titled “Create the load balancer and HTTPS listener”

This example uses the default VPC and subnets. To use a different VPC, select it and its subnets when you create the load balancer; you can’t change its network configuration afterward. See Attach the load balancer to a specific VPC before running the creation command.

Allocate a public IP and create the load balancer:

Terminal window
evroc networking publicip create web-ip
evroc loadbalancer loadbalancer create web-lb \
--public-ip-ref=web-ip

Attach both HTTP routes and the certificate to an HTTPS listener on port 443. Repeat --listener-route-ref for each route, or supply comma-separated references. HTTPS requires a certificate reference:

Terminal window
evroc loadbalancer loadbalancer addlistener web-lb \
--listener-name=https \
--listener-port=443 \
--listener-protocol=HTTPS \
--listener-route-ref=web-route \
--listener-route-ref=api-route \
--listener-certificate-ref=web-cert

For plaintext HTTP routing, use --listener-protocol=HTTP with an HTTP route and omit the certificate reference. HTTP and HTTPS listeners accept up to 32 HTTP route references. TCP listeners accept at most one L4 route reference. The HTTPS listener uses one certificate, which must cover all hostnames you serve through it. Routing uses each HTTP route’s hostname restrictions after TLS termination.

Check readiness and verify an HTTPS request

Section titled “Check readiness and verify an HTTPS request”

Inspect the certificate, route, backend service, and load balancer:

Terminal window
evroc loadbalancer certificate get web-cert -o yaml
evroc loadbalancer httproute get web-route -o yaml
evroc loadbalancer httproute get api-route -o yaml
evroc loadbalancer backendservice get web-backend -o yaml
evroc loadbalancer backendservice get api-backend -o yaml
evroc loadbalancer loadbalancer get web-lb -o yaml

Wait for the resources’ Ready conditions to be True. Read the load balancer’s public IPv4 address from status.networking.publicIPv4Address. Replace <public-ipv4> below with that address:

Terminal window
curl --resolve web.example.com:443:<public-ipv4> \
https://web.example.com/
curl --resolve api.example.com:443:<public-ipv4> \
https://api.example.com/

Both requests connect to the same load balancer IP, using their respective hostnames for TLS and HTTP routing. Verify that web.example.com returns the response from myvm and api.example.com returns the response from apivm. For a certificate issued by a private CA, add --cacert with that CA’s PEM file to each request. For normal access, configure both hostnames’ DNS records to point to the public IP.