Splitting an IMS image in three, and the route that would not install
One container held three CSCFs, a resolver and a database; pulling two of those out was the easy half, and a one-line `ip route` was the hard one.
One container held three CSCFs, a resolver and a database; pulling two of those out was the easy half, and a one-line ip route was the hard one.
The IMS deployment started as a single image carrying the three Kamailio call session control functions (P-, I- and S-CSCF), a BIND resolver and MySQL. In April 2025 it became a monorepo producing three images: ims, dns and mysql. Each has its own Dockerfile, pipeline file, Makefile and deployment metadata, over a shared common.mk. The database got persistent storage on the host it runs on, under a fixed path (/data/wireless-mysql-ims), so a container replacement stops being a data event.
The three CSCFs stayed in one image, and that was deliberate. They share a Kamailio configuration tree and a database, so splitting them buys separate scaling and costs a config split with no owner (three kamailio.cfg trees, one subscriber table). What came out were the only two components holding state or holding up startup — the resolver and the database. A colleague later added a WHICH_CSCF variable, so one image can run one function per container. That is the right way round. The split then costs a variable, instead of a rewrite.
The route that would not install
The startup script added routes for the UE subnets through the user-plane gateway (one ip route add per subnet, no checks):
for subnet in $OGSTUN2_SUBNET; do
ip route add $subnet via $UPF_IP
done
The gateway is not on a directly connected subnet, so ip route add <subnet> via <upf> is rejected with an invalid-gateway error (RTNETLINK answers: Network is unreachable). Under set -o errexit the container then dies during startup, which is at least honest. The replacement asks the kernel for the path first, pins a host route to the gateway, and only then adds the subnets:
ROUTE_INFO=$(ip route get "$UPF_IP" | head -1)
UPF_GW=$(echo "$ROUTE_INFO" | awk '{for(i=1;i<=NF;i++) if($i=="via") print $(i+1)}')
IFACE=$(echo "$ROUTE_INFO" | awk '{for(i=1;i<=NF;i++) if($i=="dev") print $(i+1)}')
ip route add ${UPF_IP}/32 via ${UPF_GW} dev ${IFACE}
for subnet in $UE_SUBNETS; do
ip route add "$subnet" via "$UPF_IP" dev "$IFACE" onlink
done
onlink is the whole trick: it tells the kernel to treat the gateway as reachable on that interface without a connected route proving it. Both additions are now guarded against an existing route, so a restart is idempotent. A gateway the kernel cannot resolve is a hard error with a message, instead of a half-configured container. The subnet list also became a variable in the deployment template (UE_SUBNETS, space-separated), so adding a UE range is a variable change and not an image change. Adding routes from inside a container needs the container to be privileged, which the template does explicitly.
BIND to CoreDNS, and what envsubst eats
3GPP service names are keyed by mobile country and network code, so BIND wanted a zone file per combination (two per code pair, one for the EPC domain and one for the IMS domain). CoreDNS's template plugin matches the pattern instead, and one server block answers for any of them:
template IN SRV {
match "^_sip\._(?P<proto>tcp|udp)\.pcscf\.ims\.mnc(?P<mnc>[0-9]{2,3})\.mcc(?P<mcc>[0-9]{3})\.3gppnetwork\.org\.$"
answer "{{ .Name }} 3600 IN SRV 0 0 {$PCSCF_SIP_PORT} pcscf.ims.mnc{{ .Match.mnc }}.mcc{{ .Match.mcc }}.3gppnetwork.org."
additional "pcscf.ims.mnc{{ .Match.mnc }}.mcc{{ .Match.mcc }}.3gppnetwork.org. 3600 IN A {$IMS_IP}"
}
Specific combinations that need different answers keep an override zone file — everything else falls through to the wildcard. Names outside the estate get NXDOMAIN, and malformed 3GPP names get SERVFAIL (two-level blocking). A typo in a core element's configuration is therefore distinguishable from a name that never existed.
The migration's one real trap was in rendering. Converting the zone files to templates meant running them through envsubst. It substitutes $ORIGIN and $TTL as well — both are zone directives, and both vanished. They are written as placeholders and restored afterwards:
envsubst < "$template" | sed 's/__ORIGIN__/\$ORIGIN/g' | sed 's/__TTL__/\$TTL/g' > "$output"
The SOA record also had to be collapsed onto one line, which CoreDNS requires and BIND does not. Measured after the cutover (a verification run, not a benchmark): a 51.8% cache hit ratio and a 0.374 ms average response time.
Observability came in the same ticket. prometheus :9153 is declared in every server block (both wildcards, and each override). So coredns_dns_requests_total, coredns_cache_entries and the request-duration histogram all carry a zone label — which a single global endpoint would have flattened.
Getting metrics out of Kamailio
Kamailio exposes statistics internally (kamcmd stats.get_statistics), and nothing scrapes them. The CSCF configs now load xhttp and xhttp_prom with xhttp_prom_stats=all. An event_route[xhttp:request] serves /metrics and only /metrics, on a dedicated port per function (8081, 8082, 8083).
One line made the difference: tcp_accept_no_cl=yes. Without it Kamailio rejects a request carrying no Content-Length — which is every HTTP GET a scraper will ever send.
Two smaller changes, and what the migration cost
The base image moved off plain Ubuntu onto the FRR image, so the container announces its own service addresses by BGP (one frr.conf template, one start script). The hand-maintained resolv.conf went with it, replaced by forwarding in the DNS service. IPsec was narrowed to integrity protection, with encryption disabled — a deliberate choice about what the access leg needs.
The CI migration in February 2026 took three workflows for three services, plus one correction: image_additional_tags wants a plain string (red) and had been given a JSON array. One commit later it built.