A small Go program that provides a simple yet effective solution for automatic DNS failover using Cloudflare's API.
The program monitors a list of server IPs in order of priority and updates the A record for a specified domain to the IP address of the first responsive server. This ensures high availability and reliability of services by automatically switching to a backup server in case the primary server goes down.
This DNS failover mechanism is not to be confused with round-robin DNS, where multiple A records are set for the same domain to distribute traffic randomly across different servers. While potentially useful, this cannot be depended upon for site reliability, since if one of the servers goes down, the DNS server will still keep that server’s IP in the round-robin rotation.
Before you begin, ensure you have met the following requirements:
- A Cloudflare account with access to API tokens.
- Clone the repository:
git clone https://github.com/cycneuramus/cloudflare-dns-failover
cd cloudflare-dns-failover
- Configure your records
Open the config.yml.example
with your preferred text editor and edit the placeholder values. Save the file as config.yml
.
- Build the program
go build .
To start the DNS failover mechanism, simply run the program:
./cloudflare-dns-failover
This assumes that config.yml
exists in the working directory. You can, however, pass its path as a flag:
./cloudflare-dns-failover -c /path/to/config.yml
The program executes in an infinite loop, checking the availability of the servers at the specified interval and updating the DNS record as needed.
To maintain continuous operation, ensure that the program is running in a stable environment (e.g. as a systemd
service or perhaps even as an orchestrated exec
job in a Nomad cluster).