You have a recursive DNS resolver, an authoritative DNS, an NTP service, or a public health-check endpoint, and you want clients in three cities to reach “the same IP” but actually land on the geographically closest data centre. The classic answer — GeoDNS, with one A record per region — works for most cases and falls apart in the moments that matter: a DC outage, where stale resolver caches keep sending people to the dead region for hours. Anycast solves both problems by moving the routing decision off DNS and onto BGP. Every DC announces the same /32 (or /24) prefix from its border router, and every Tier-1 backbone makes the AS-path choice; if the closest DC drops, the route is withdrawn and traffic shifts to the next-closest one in seconds. The catch is that anycast requires real BGP peering with an upstream you can pay (or argue) for, a clean /24 or /32 you can advertise, and a daemon on your edge servers willing to talk BGP. bird2 is that daemon — small, well-understood, and the basis of most academic and ISP anycast deployments.
What’s happening
In a unicast world, an IP belongs to one host on one network. With anycast, the same IP is advertised from multiple network locations simultaneously, and the global routing table picks “the best path” per source — typically by AS-path length and IGP metric. Two pieces make it work. First, every edge server in every DC runs a small BGP speaker (bird2 in our case) that establishes an eBGP session with the DC’s border router. Second, the BGP speaker only advertises the anycast prefix when the local service is healthy — and withdraws it when the service goes down. The withdrawal triggers an immediate BGP update across the upstream’s network, the route is reconverged, and clients re-resolve to the next-closest DC within the BGP propagation window (usually 1-30 seconds). The configuration is small but high-stakes. If your route-map accidentally advertises more than the anycast prefix, you can leak internal addresses to your upstream and get a polite filtering email. If your healthcheck only verifies the local daemon’s PID rather than its actual responsiveness, you keep announcing the prefix while the service is silently broken — exactly the failure mode anycast is supposed to prevent. And if you don’t agree the prefix and ASN with your upstream in advance, your sessions never come up: BGP is paperwork before it is configuration.
How to verify
The eBGP session should be Established on every edge server:
birdc show protocols
# Name Proto Table State Since Info
# device1 Device --- up 2026-05-14
# kernel1 Kernel master4 up 2026-05-14
# upstream_v4 BGP --- up 2026-05-14 Established
The anycast prefix should be exported to the upstream when the service is healthy:
birdc show route export upstream_v4
# 198.51.100.10/32 unicast [direct1 ...]
# dev lo0
# Type: device univ
When you stop the local service, bird should withdraw the route within a few seconds:
sudo systemctl stop my-anycast-service
sleep 2
birdc show route export upstream_v4
# (empty — route withdrawn)
# And on the upstream's side, the route disappears from the routing table within
# one BGP keepalive interval.
From a third-party looking glass (e.g. RIPE RIS, your transit provider’s portal), the prefix should be visible with multiple AS-paths — one per DC announcing it.
The fix
The installer below configures bird2 v2 on a single edge server, peering eBGP with one upstream router. Run the same script on every edge server in every DC (with different LOCAL_AS, LOCAL_ROUTER_ID, UPSTREAM_IP, and UPSTREAM_AS per location). The anycast prefix and ASN must match across all servers.
#!/usr/bin/env bash
set -euo pipefail
### bird2 BGP anycast installer for Ubuntu 22.04
### Run on every edge server in every DC.
### Service-conditional advertisement: bird only exports the anycast prefix
### while the local service is healthy.
if [[ "$(id -u)" -ne 0 ]]; then
echo "Run as root."
exit 1
fi
# === EDIT PER NODE / PER DC ===
LOCAL_AS="65020" # your AS (private or public)
LOCAL_ROUTER_ID="10.20.1.11" # this node's unique router-id (use its mgmt IP)
LOCAL_NODE_IP="10.20.1.11" # this node's IP on the peering subnet
UPSTREAM_IP="10.20.1.1" # the DC border router's IP
UPSTREAM_AS="65010" # the upstream's AS
BGP_PASSWORD="CHANGE_ME_BGPPASS" # TCP-MD5 (RFC 2385) shared with upstream
ANYCAST_PREFIX="198.51.100.10/32" # the IP everyone announces
ANYCAST_LOOPBACK_IF="lo" # interface holding the anycast IP
HOLD_TIME="30" # seconds
KEEPALIVE="10" # seconds
SERVICE_NAME="my-anycast-service.service" # systemd unit to gate advertisement on
SERVICE_PORT="53" # local TCP/UDP port to probe
SERVICE_PROTO="udp" # tcp or udp
# ==============================
echo
echo "========================================"
echo "bird2 anycast on ${LOCAL_ROUTER_ID}"
echo "Local AS ${LOCAL_AS} -> Upstream AS ${UPSTREAM_AS} (${UPSTREAM_IP})"
echo "Anycast prefix: ${ANYCAST_PREFIX}"
echo "========================================"
echo
echo "==> apt install"
export DEBIAN_FRONTEND=noninteractive
apt-get update
apt-get -y install bird2 ipcalc-ng iproute2 net-tools curl jq
echo "==> Binding the anycast IP on loopback"
ANYCAST_IP=$(echo "${ANYCAST_PREFIX}" | cut -d/ -f1)
if ! ip -4 addr show "${ANYCAST_LOOPBACK_IF}" | grep -qF "${ANYCAST_IP}/32"; then
ip addr add "${ANYCAST_IP}/32" dev "${ANYCAST_LOOPBACK_IF}"
fi
# Persist across reboots via systemd-networkd drop-in
mkdir -p /etc/systemd/network
cat > /etc/systemd/network/10-anycast-lo.network <<EOF
[Match]
Name=${ANYCAST_LOOPBACK_IF}
[Address]
Address=${ANYCAST_IP}/32
Scope=global
EOF
systemctl restart systemd-networkd || true
echo "==> Sysctl: allow non-local bind, ARP filter sanity"
cat > /etc/sysctl.d/99-anycast.conf <<'SYSCTL'
net.ipv4.ip_nonlocal_bind = 1
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
SYSCTL
sysctl --system
echo "==> Writing healthcheck script"
mkdir -p /etc/bird/scripts
cat > /etc/bird/scripts/check_anycast_service.sh <<EOF
#!/bin/bash
# Returns 0 (healthy) if the local service responds, 1 (unhealthy) otherwise.
# We probe the actual port to catch daemons stuck in degraded states.
set -e
# 1. Systemd unit is active
systemctl is-active --quiet "${SERVICE_NAME}" || exit 1
# 2. Port is accepting traffic
if [[ "${SERVICE_PROTO}" == "tcp" ]]; then
timeout 2 bash -c '</dev/tcp/127.0.0.1/${SERVICE_PORT}' >/dev/null 2>&1 || exit 1
else
# UDP: send a probe and rely on the service's response.
# For DNS, use dig as a richer probe:
# dig +short +time=1 +tries=1 @127.0.0.1 health.example.com >/dev/null 2>&1 || exit 1
timeout 2 ncat -u -z 127.0.0.1 "${SERVICE_PORT}" >/dev/null 2>&1 || exit 1
fi
exit 0
EOF
chmod 755 /etc/bird/scripts/check_anycast_service.sh
echo "==> Writing bird2 config"
cat > /etc/bird/bird.conf <<EOF
# bird2 anycast config for ${LOCAL_ROUTER_ID}
# Generated $(date -Is)
router id ${LOCAL_ROUTER_ID};
log "/var/log/bird/bird.log" all;
log syslog all;
debug protocols { events, states };
# Bring kernel routes into bird (read-only here; we only advertise the anycast prefix)
protocol device {
scan time 10;
}
protocol direct {
ipv4;
interface "lo";
}
protocol kernel {
ipv4 {
import none;
export none;
};
scan time 20;
}
# A simple static route for the anycast prefix, used only for sourcing the export.
protocol static anycast_routes {
ipv4;
route ${ANYCAST_PREFIX} via "${ANYCAST_LOOPBACK_IF}";
}
# Health-gated filter: only export the anycast prefix when the service is alive.
# bird2 cannot exec scripts at filter-time, so we toggle a file flag from a
# systemd timer (see /etc/systemd/system/bird-health.* below) and read it here.
function service_is_healthy()
{
return defined(BGP_HEALTHY) && BGP_HEALTHY = 1;
}
filter export_to_upstream {
if net = ${ANYCAST_PREFIX} then {
bgp_path.prepend(${LOCAL_AS}); # one extra prepend lengthens AS-path for tie-breaks
# Remove if you want this DC equally preferred everywhere.
accept;
}
reject;
}
filter import_from_upstream {
# Reject everything from upstream — we are a stub announcer, we don't take
# routes back. If you actually need a default route via the upstream, build
# a small permit clause here, but be careful: importing 0/0 from a transit
# provider can mask routing errors.
reject;
}
protocol bgp upstream_v4 {
description "eBGP to upstream ${UPSTREAM_AS} via ${UPSTREAM_IP}";
local ${LOCAL_NODE_IP} as ${LOCAL_AS};
neighbor ${UPSTREAM_IP} as ${UPSTREAM_AS};
password "${BGP_PASSWORD}";
hold time ${HOLD_TIME};
keepalive time ${KEEPALIVE};
graceful restart on;
ipv4 {
import filter import_from_upstream;
export filter export_to_upstream;
next hop self;
};
}
EOF
mkdir -p /var/log/bird
chown bird:bird /var/log/bird
# bird2 doesn't natively run an external healthcheck per filter.
# We tie the advertisement to a /run flag file using systemd: a timer
# runs check_anycast_service.sh every 5s; if it fails, we disable the BGP
# protocol; if it succeeds, we enable it. That gives the same effect as
# health-gated advertisement, but with proper structural separation.
cat > /usr/local/sbin/bird-health.sh <<'BIRDHC'
#!/bin/bash
set -e
if /etc/bird/scripts/check_anycast_service.sh; then
birdc 'enable upstream_v4' >/dev/null 2>&1 || true
else
birdc 'disable upstream_v4' >/dev/null 2>&1 || true
fi
BIRDHC
chmod 755 /usr/local/sbin/bird-health.sh
cat > /etc/systemd/system/bird-health.service <<'BIRDHCSVC'
[Unit]
Description=Toggle bird BGP session based on service health
After=bird.service
[Service]
Type=oneshot
ExecStart=/usr/local/sbin/bird-health.sh
BIRDHCSVC
cat > /etc/systemd/system/bird-health.timer <<'BIRDHCTMR'
[Unit]
Description=Run bird-health every 5 seconds
[Timer]
OnBootSec=20s
OnUnitActiveSec=5s
AccuracySec=1s
Unit=bird-health.service
[Install]
WantedBy=timers.target
BIRDHCTMR
echo "==> Reload + enable services"
systemctl daemon-reload
systemctl enable --now bird
systemctl enable --now bird-health.timer
sleep 5
echo
echo "========================================"
echo "DONE on ${LOCAL_ROUTER_ID}"
echo "========================================"
echo
echo "Loopback anycast IP:"
ip -4 addr show "${ANYCAST_LOOPBACK_IF}" | grep "${ANYCAST_IP}" || true
echo
echo "bird protocols:"
birdc show protocols
echo
echo "Exports to upstream (should show ${ANYCAST_PREFIX} when service is healthy):"
birdc show route export upstream_v4 || true
echo
echo "To see live withdrawal/announcement behaviour:"
echo " systemctl stop ${SERVICE_NAME}"
echo " sleep 6 && birdc show route export upstream_v4 # empty"
echo " systemctl start ${SERVICE_NAME}"
echo " sleep 6 && birdc show route export upstream_v4 # ${ANYCAST_PREFIX} re-appears"
The upstream side needs a matching neighbor block. On a Cisco IOS XE:
router bgp 65010
bgp log-neighbor-changes
neighbor 10.20.1.11 remote-as 65020
neighbor 10.20.1.11 password 0 CHANGE_ME_BGPPASS
neighbor 10.20.1.11 timers 10 30
neighbor 10.20.1.11 prefix-list ANYCAST-IN in
neighbor 10.20.1.11 prefix-list NONE-OUT out
ip prefix-list ANYCAST-IN seq 10 permit 198.51.100.10/32
ip prefix-list ANYCAST-IN seq 20 deny 0.0.0.0/0 le 32
ip prefix-list NONE-OUT seq 10 deny 0.0.0.0/0 le 32
A few important operational pieces. Always negotiate the prefix and ASN with your upstream before you bring up sessions; a serious transit provider will require an IRR-registered route object (route: in RADb, RIPE, ARIN) and may filter you on first session if your prefix isn’t documented. The bgp_path.prepend in the export filter is a deliberate handle on which DC clients prefer: lengthen the AS-path on weaker DCs, leave the canonical one un-prepended, and you’ll see traffic distribute approximately by capacity rather than randomly by AS-topology. Never advertise a /24 you don’t own; the global BGP table is policed and a misadvertisement gets noticed within minutes. The bird-health.timer pattern above is a clean alternative to bird’s lack of exec-time filters; the 5-second granularity is good for most services — drop it to 2 seconds for latency-critical ones, but watch CPU on bird. Finally, monitor your upstream’s view of your prefix via a looking glass once a quarter — a silent re-origination by a misconfigured peer is the kind of thing that costs you traffic without ever triggering your own alerts.
Anycast adds a routing dimension to your operations, and getting it wrong is visible to the entire internet. Our Cloud Compute team operates anycast DNS/NTP/health endpoints under Managed Cloud Ops with peering, IRR objects, and looking-glass monitoring in place — open a routing-design conversation if you want a second pair of eyes on the BGP plan before you bring sessions up.