Balancing traffic with round-robin DNS using CNAME records.
TypeScriptRound-robin DNS is a technique that enables the distribution of network traffic across multiple servers using DNS records. It is one of the simplest methods of load balancing and can be achieved by creating multiple DNS records with the same name but different values that point to various servers.
Below is a Pulumi program written in TypeScript that demonstrates how to set up round-robin DNS using AWS Route 53. Route 53 is Amazon's DNS service that allows you to manage your DNS records. In this example, we will create a hosted zone for a domain and then add multiple CNAME records pointing to different server addresses. Each CNAME record will share the same name (e.g.,
www
) but will resolve to different IP addresses.Let's walk through the code:
-
We import the necessary Pulumi and AWS packages.
-
We create a new hosted zone for our domain.
-
We add multiple CNAME records within the hosted zone. These records will have a
TTL
(time to live) value that indicates how long DNS resolvers should cache the response. ATTL
of 300 seconds (5 minutes) is common for dynamic DNS configurations like round-robin. -
We set the
roundRobinRecords
variable, which is an array of different IP addresses (in this case, subdomains) where our traffic will be distributed. -
We loop through the
roundRobinRecords
and create a CNAME record for each IP address, making sure they all have the same record name but different values.
Here's the complete program:
import * as pulumi from "@pulumi/pulumi"; import * as aws from "@pulumi/aws"; // Replace with your domain name const domainName = "example.com"; // Create a new Route 53 zone for the domain const zone = new aws.route53.Zone("my-zone", { name: domainName, }); // Define round-robin CNAME records const roundRobinRecords = [ "server1.example.com", "server2.example.com", "server3.example.com", ]; // Create CNAME records to balance traffic across servers roundRobinRecords.forEach((serverAddress, index) => { new aws.route53.Record(`www-record-${index}`, { zoneId: zone.id, name: "www", // Subdomain part of the URL (e.g., www.example.com) type: "CNAME", ttl: 300, records: [serverAddress], }); }); // Export the domain name and name servers export const domainNameOutput = domainName; export const nameServers = zone.nameServers;
By running this Pulumi program and applying it, you will setup round-robin DNS load balancing for
www.example.com
which will equally distribute traffic betweenserver1.example.com
,server2.example.com
, andserver3.example.com
.Remember to replace
example.com
and server addresses with your actual domain and server addresses.After deploying this, DNS queries for
www.example.com
will be resolved toserver1.example.com
,server2.example.com
, andserver3.example.com
in a round-robin fashion. The TTL value defines how long each DNS resolution result should be cached, and a lower value can be used for more responsive load balancing adjustments.Please note that for this code to run, you should have the AWS credentials configured in your Pulumi environment as it requires access to AWS Route 53 services to create and manage DNS records.
-
Rewrite this program in Python
PythonBelow is the equivalent Pulumi program written in Python, which sets up a Route 53 zone for a specified domain name and creates round-robin CNAME records.
import pulumi import pulumi_aws as aws # Replace with your domain name domain_name = "example.com" # Create a new Route 53 zone for the domain zone = aws.route53.Zone("my-zone", name=domain_name) # Define round-robin CNAME records round_robin_records = [ "server1.example.com", "server2.example.com", "server3.example.com", ] # Create CNAME records to balance traffic across servers for index, server_address in enumerate(round_robin_records): aws.route53.Record(f"www-record-{index}", zone_id=zone.id, name="www", # Subdomain part of the URL (e.g., www.example.com) type="CNAME", ttl=300, records=[server_address]) # Export the domain name and name servers pulumi.export('domain_name_output', domain_name) pulumi.export('name_servers', zone.name_servers)
This program will create a Route 53 hosted zone and CNAME records using round-robin DNS to distribute traffic among the specified servers.