Routing with Caddy
Caddy can route each of your sites to the instances whose labels match a selector, using the caddy-dicer plugin. Caddy spreads requests across the matching instances. It also follows the daemon’s events, so an instance starts getting requests as soon as it is running and healthy, and stops getting them when it stops. You never need to reload Caddy for this. Instances can also carry their routing in labels, so a new site needs no change to Caddy’s configuration.
In this example, three instances of a shop serve shop.example.com, and a
single blog instance serves blog.example.com. Caddy runs on the Dicer
host and gets certificates for both sites from Let’s Encrypt.
Build Caddy with the plugin
Caddy plugins are compiled into the Caddy binary. Build Caddy with this plugin using xcaddy, which needs Go. The plugin and Dicer’s module are private, so Go has to download them from GitHub with your git credentials, rather than from the public proxy:
$ go install github.com/caddyserver/xcaddy/cmd/xcaddy@latest
$ export GOPRIVATE='github.com/hostinger/*'
$ xcaddy build --with github.com/hostinger/caddy-dicer
$ ./caddy list-modules | grep dicer
http.reverse_proxy.upstreams.dicer
If you build on a different machine, set the target system to match the
host, for example GOOS=linux GOARCH=amd64 xcaddy build ... or
GOARCH=arm64.
On Debian and Ubuntu, install Caddy’s package first, which sets up the
service and the caddy user. Then replace the package’s binary with your
build, as shown in
Caddy’s documentation:
$ sudo dpkg-divert --divert /usr/bin/caddy.default --rename /usr/bin/caddy
$ sudo mv ./caddy /usr/bin/caddy.custom
$ sudo update-alternatives --install /usr/bin/caddy caddy /usr/bin/caddy.default 10
$ sudo update-alternatives --install /usr/bin/caddy caddy /usr/bin/caddy.custom 50
Your build stays in place when the package is upgraded.
Give Caddy a token
Caddy only needs to list instances and networks, and to follow events. Turn on the daemon’s TCP listener on the loopback address only, and give Caddy a token with just those permissions:
server:
listen: 127.0.0.1:7443$ sudo systemctl restart dicerd
$ sudo dicer token create caddy --scopes instances:read,events:read,networks:read |
sudo install -m 600 -o caddy -g caddy /dev/stdin /etc/caddy/dicer-token
The events:read scope lets Caddy see events for every resource on the
host, not only instances, but it cannot change anything. Caddy lists
networks only to learn their gateway addresses. It connects to the ports
that instances publish at those addresses.
Caddy could use the daemon’s socket instead, without a token, but only if
the caddy user is in the dicer group. That gives Caddy as much power as
root, so the token is the safer choice.
Run the instances
Label each instance with the app it runs, and give it a health check. Run the first shop instance, then fork it for the other two:
$ dicer run -d --name shop-1 --label app=shop --health-http 3000/healthz ghcr.io/acme/shop:3
$ dicer fork shop-1 shop-2
$ dicer fork shop-1 shop-3
$ dicer run -d --name blog --label app=blog --health-http 8080 ghcr.io/acme/blog:1
A fork has the same definition as the instance it was forked from, including its labels and health check, but its own name and address. It also starts with a copy of that instance’s memory, so the shop is already running in it, with nothing to boot or warm up.
The instances don’t publish any ports. Caddy connects to each one directly, at its address on the Dicer network.
Route to them
For each site, give the label selector that picks its instances, and the port they listen on:
(dicer) {
address 127.0.0.1:7443
token {file./etc/caddy/dicer-token}
}
shop.example.com {
reverse_proxy {
dynamic dicer app=shop 3000 {
import dicer
}
lb_policy least_conn
lb_try_duration 5s
}
}
blog.example.com {
reverse_proxy {
dynamic dicer app=blog 8080 {
import dicer
}
}
}$ sudo systemctl reload caddy
$ curl https://shop.example.com
A selector can match on more than one label, for example
app=shop,tier in (frontend,edge),!canary. The plugin’s
README describes
the full syntax. With lb_try_duration, a request that reaches an instance
just as it stops is retried on another instance instead of failing.
Route by instance labels
Instead of one site per host, each instance can declare the host it serves
with a caddy.host label, and its port with caddy.port. A single site
with no selector then routes each host to the instances that serve it:
$ dicer run -d --name shop-1 --label caddy.host=shop.example.com --label caddy.port=3000 \
--health-http 3000/healthz ghcr.io/acme/shop:3
$ dicer run -d --name blog --label caddy.host=blog.example.com --label caddy.port=8080 \
--health-http 8080 ghcr.io/acme/blog:1
{
on_demand_tls {
permission dicer {
address 127.0.0.1:7443
token {file./etc/caddy/dicer-token}
}
}
}
https:// {
tls {
on_demand
}
reverse_proxy {
dynamic dicer {
address 127.0.0.1:7443
token {file./etc/caddy/dicer-token}
}
lb_try_duration 5s
}
}A label can list several hosts, separated by commas, such as
caddy.host=shop.example.com,www.shop.example.com. A fork keeps its
source’s labels, so it serves the same hosts. It publishes no ports, though,
so it cannot scale to zero until you give it one (see
Good to know).
With no hosts in the Caddyfile, Caddy cannot get their certificates in
advance. It gets each certificate the first time its host is requested.
The plugin’s permission dicer allows this only for hosts listed in an
instance’s caddy.host label. A request for any other host gets no
certificate. The plugin’s
README describes the
labels in full.
Add and remove instances
Fork another shop instance, and Caddy starts sending it requests once its health check passes:
$ dicer fork shop-1 shop-4
The instance you fork from is paused for about a second while its memory is
copied. Caddy retries requests that arrive meanwhile on the other instances,
because of lb_try_duration. To avoid pausing a serving instance at all,
take a snapshot of it once, and fork that
whenever you need another instance:
$ dicer snapshot create shop-1 shop-warm
$ dicer snapshot fork shop-warm shop-5
Stop or delete an instance, and Caddy stops sending it requests:
$ dicer stop shop-2
The same happens when an instance is paused, or when its health check finds
it unhealthy. If a site has no instances left to route to, Caddy answers
503 Service Unavailable.
Good to know
- Caddy must be able to reach the instances’ network, and by default only
the Dicer host can. Run Caddy on the host, or on a machine with a route to
that network. If an instance publishes the port Caddy routes to with
-p, Caddy reaches it through the published port on the host instead. - Instances can scale to zero. Give an instance
--standby-afterand a published port, such as-p 8001:3000. When all of a site’s instances are on standby, Caddy sends requests to the published port of one of them, which wakes it. The first request waits about a second while the instance wakes. Each instance needs its own host port. A fork publishes none unless you give it one with-p. An instance on standby that publishes no port gets no requests, because nothing would wake it. - While the daemon restarts, Caddy keeps routing to the instances it already knows about, because they keep running. Once the daemon is back, Caddy picks up any changes.
- One Caddy can route to instances on several hosts. For each site, point the plugin at the listener of the host its instances run on, with a token from that host. Caddy also needs a network route to each host’s instances.