← Recipes

阅读中文版本

Automate Blue-Green DNS Cutover

Switch traffic between blue and green stacks by updating DNS with a low TTL and verifying health before and after the cutover.

~35 mindnsopsdeployment

Task

Script a DNS-based blue-green cutover that flips traffic only after the new stack passes health checks.

When to use this

Manual traffic switching is slow and error-prone, and reverting depends on remembering the previous DNS record.

Prerequisites

  • DNS provider API access
  • Two identical stacks (blue and green)
  • Health endpoints on each stack

Steps

1. Lower the TTL ahead of the cutover.

# Set record TTL to 60s at least one TTL window before switching
example-dns update --name app.example.com --ttl 60

Expected output: The short TTL ensures the switch propagates within a minute.

2. Verify the green stack then flip the record.

curl -fsS https://green.example.com/health || exit 1
example-dns update --name app.example.com --value green.example.com --ttl 60
for i in $(seq 1 10); do curl -fsS https://app.example.com/health && break; sleep 3; done

Expected output: Traffic reaches green and the health check passes end to end.

Troubleshooting

Problem

Old IPs still receive traffic

Solution

Wait for the TTL to drain and confirm existing connections complete before destroying blue.

Problem

Green healthy on its own but fails via DNS

Solution

Check TLS certificates and host headers for the public name.

Limitations

  • DNS propagation is not instantaneous.
  • Requires a rollback record prepared in advance.
  • Long-lived connections may not drain quickly.

What this recipe does

Script a DNS-based blue-green cutover that flips traffic only after the new stack passes health checks.

Manual traffic switching is slow and error-prone, and reverting depends on remembering the previous DNS record.

Prerequisites

  • DNS provider API access
  • Two identical stacks (blue and green)
  • Health endpoints on each stack

Procedure

  1. Lower the TTL ahead of the cutover.
# Set record TTL to 60s at least one TTL window before switching
example-dns update --name app.example.com --ttl 60

Expected result: The short TTL ensures the switch propagates within a minute.

  1. Verify the green stack then flip the record.
curl -fsS https://green.example.com/health || exit 1
example-dns update --name app.example.com --value green.example.com --ttl 60
for i in $(seq 1 10); do curl -fsS https://app.example.com/health && break; sleep 3; done

Expected result: Traffic reaches green and the health check passes end to end.

How to confirm it worked

Run the commands above and confirm every expected result in order. If a step does not produce the stated output, use the troubleshooting notes before continuing. A passing run shows the public hostname resolves to green, health checks pass, and rolling back restores blue within the TTL.

Troubleshooting

Old IPs still receive traffic — Wait for the TTL to drain and confirm existing connections complete before destroying blue.

Green healthy on its own but fails via DNS — Check TLS certificates and host headers for the public name.

Limitations

  • DNS propagation is not instantaneous.
  • Requires a rollback record prepared in advance.
  • Long-lived connections may not drain quickly.