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.
Prerequisites
Section titled “Prerequisites”- An installed and authenticated evroc CLI, with the project and region selected
- An existing virtual machine (VM),
myvm, serving HTTP on port8080 - A second VM,
apivm, serving HTTP on port8080, with responses you can distinguish frommyvm - Network access from the load balancer to the VM’s backend port; see Configure security groups
- A PEM certificate chain covering both
web.example.comandapi.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.
Create the backend pool and service
Section titled “Create the backend pool and service”Add each VM to a separate backend pool:
evroc loadbalancer backendpool create web-pool \ --backend-ref=myvmevroc loadbalancer backendpool create api-pool \ --backend-ref=apivmCreate a service for each application’s HTTP port. Backend IP selection defaults to IPv4:
evroc loadbalancer backendservice create web-backend \ --backend-pool-ref=web-pool \ --port=8080evroc loadbalancer backendservice create api-backend \ --backend-pool-ref=api-pool \ --port=8080Configure an HTTP health check using a path your application serves:
evroc loadbalancer backendservice sethealthcheckhttp web-backend \ --path=/readyevroc loadbalancer backendservice sethealthcheckhttp api-backend \ --path=/readyCreate and upload the certificate
Section titled “Create and upload the certificate”Create the empty certificate resource, then upload its content:
evroc loadbalancer certificate create web-certevroc loadbalancer certificate upload web-cert \ --certificate-file=fullchain.pem \ --private-key-file=privkey.pemThe service validates the certificate and key. Check that the certificate’s Ready condition is True:
evroc loadbalancer certificate get web-cert -o yamlCreate an HTTP route per hostname
Section titled “Create an HTTP route per hostname”Create a separate route for each hostname:
evroc loadbalancer httproute create web-route \ --hostname=web.example.com \ --backend-service-ref=web-backendevroc loadbalancer httproute create api-route \ --hostname=api.example.com \ --backend-service-ref=api-backendEach 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:
evroc networking publicip create web-ipevroc loadbalancer loadbalancer create web-lb \ --public-ip-ref=web-ipAttach 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:
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-certFor 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:
evroc loadbalancer certificate get web-cert -o yamlevroc loadbalancer httproute get web-route -o yamlevroc loadbalancer httproute get api-route -o yamlevroc loadbalancer backendservice get web-backend -o yamlevroc loadbalancer backendservice get api-backend -o yamlevroc loadbalancer loadbalancer get web-lb -o yamlWait 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:
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.