In this tutorial, you will add a second origin for some basic round robining, and then use the Cloudflare Load Balancing product to fail traffic over as needed. You will also enhance your load balancing configuration through the use of “geo steering” to serve results from an origin server that is geographically closest to your end users.
1. Add another DNS record for www
To get started, add a DNS record for a second web server, located in Asia. The IP address for this server is 198.51.100.15.
Note that while the name of the resource is different because Terraform resources of the same type must be uniquely named, the DNS name, or what your customers will type in their browser, is the same: www.
2. Preview and merge the changes
Check the terraform plan and then merge and apply the changes.
With the second DNS record in place, make some requests to see where the traffic is served from.
As noted above, there is no discernible pattern for which origin receives the request. When Cloudflare connects to an origin with multiple DNS records, one of the IP addresses is selected at random. If both IPs are in the same data center and sessions can be shared (that is, it does not matter if the same user hops between origin servers), this may work fine. However, for anything more complicated, such as origins in different geographies or active health checks, you will want to use Cloudflare’s Load Balancing product.
4. Switch to using Cloudflare’s Load Balancing product
Create a monitor to run health checks against your origin servers.
Create a pool of one or more origin servers that will receive load balanced traffic.
Create a load balancer with an external hostname — for example, www.example.com — and one or more pools.
i. Define and create the health check (“monitor”)
To monitor the origins, create a basic health check that makes a GET request to each origin on the URL https://www.example.com. If the origin returns the 200 status code (OK) within five seconds, it is considered healthy. If it fails to do so three times in a row, it is considered unhealthy. This health check will be run once per minute from several regions and send an email notification to your email address (configured as <YOUR_EMAIL>) if any failures are detected.
ii. Define and create the pool of origins
In this example, the pool will be called www-servers with two origins added to it:
Note the reference to the monitor that you added in the last step. When applying this configuration, Terraform will determine that it first needs to create the monitor before looking up the ID and providing it to the pool you wish to create.
iii. Define and create the load balancer
When you create a load balancer (LB), it will replace any existing DNS records with the same name. For example, if you create the www.example.com load balancer below, it will supersede the two www DNS records that you previously defined. One benefit of leaving the DNS records in place is that if you temporarily disable load balancing, connections to this hostname are still possible as shown in step 2 above.
iv. Preview and merge the changes
As usual, review the proposed plan before applying any changes.
The plan looks good. Merge the plan and apply it.
v. Test the changes
With load balancing in place, run four curl requests again to see where the traffic is served from.
You should now see each request load balanced evenly across the two origins you defined.