Two apps on one port, each by its name
You have a server and two small web apps. You want hello.example.com and shop.example.com to reach them, both on port 80 of that one server, each its own app, deployed without touching the other.
In this tutorial you make both apps from a template, a form each. For each app plinto makes its code, its version group, its environment and its pipeline, deploys it, and puts a load balancer in front that answers by the app's name. The first app also makes the proxy both of them share.
Before you start
- A host group with a ready host. Here it's
web, with one host,web-1, atweb-1.example.net. Add a host says how. - System services on that group. Port 80 is below 1024, so the shared proxy runs as a system service, and a group takes those only with System services on, on its page in the Hosts app.
- Port 80 open on those hosts, to anyone: the run's last step asks the app through it, as anyone on the internet would.
- Nothing to declare.
GoWebServiceTemplate, the app, andCaddyEdgeProxyTemplate, the proxy the apps share, are the catalog's: every organization's Templates app lists them, marked catalog. One of your own of either name would be listed beside it, marked yours.
No domain yet? You don't need one to start. Each app can be tried by its name with curl before your DNS knows it, as below.
Make the first app
New app
In the Templates app, New app. Pick GoWebServiceTemplate, the one marked catalog, and name the app
Hello. The template names everything after it: the packageHello, the version grouphello, and the environmentsHelloandHello/Prod.The app is a small Go service:
/hellosays which host answered, which shows the load balancer's turns once the group has more hosts, and/healthanswersokwhile it serves.Where it runs
Runs on:
web. Leave Port empty: the app takes the first port free on the group's hosts from 8080, here 8080 itself.Its load balancer
Under Parts, tick + Load balancer and pick By name: on the group's shared proxy, with other apps. Its own port would give Hello a proxy of its own instead, on a port nobody else uses.
New app · Parts+ Load balancer ✓ caddy, asking each server /healthHow it answers ● By name · on the group's shared proxy, with other apps Name hello.example.com On web Port 80, the shared proxy's, left empty The name is what the app answers to, plain HTTP. On is the group whose shared proxy answers it: the app's own will do.
Read It will make
As you type, plinto checks the app and lists what it would make, each name checked free. With a load balancer by name, the last part reads:
New app · It will make · 5 Load balancer- First web's shared proxy, WebSharedProxy, made by the catalog's CaddyEdgeProxyTemplate: caddy on web at port 80, for every app there with a name
- Then hello, answering hello.example.com, sending to web at 8080 and asking each /health
- Answers at
http://hello.example.com, plain HTTP, once hello.example.com points at web-1.example.net in your DNS
First: Hello is the first app on
webby name, so it makes the group's shared proxy,WebSharedProxy. Every app after it uses that one.Make it
Make Hello starts the run, and its page follows each step. The first ones make the app as any template does, up to its first deployment. The three after are the load balancer's:
Run · HelloStep What it says once done Names checked … First deployment The app, its group, environments and pipeline, built and deployed to Hello/ProdProxy Run 2 made WebSharedProxy and deployed it to web, at port 80 Load balancer Made hello, of kind caddy, on web through WebSharedProxy/Prod, sending to web at 8080 Answers http://web-1.example.net:80/health answers HTTP 200 for hello.example.com, through hello: Hello is at http://hello.example.com once hello.example.com points at web-1.example.net The proxy is an app too, with a run of its own: Hello's run starts it and waits, and
WebSharedProxygets its own version group, environment and pipeline. A step that fails stops the run and says why; what it made stays, and Retry carries on from there.
Try it before DNS
Caddy picks the site by the name the request asks for, so curl can ask for hello.example.com at the host itself:
$ curl -H 'Host: hello.example.com' http://web-1.example.net/hello
Hello, world, from web-1
A name the proxy has no site for, here the host's own, gets a 404:
$ curl http://web-1.example.net/hello
No app answers to web-1.example.net here
A second app on the same port
Again New app, with GoWebServiceTemplate: name it Shop, Runs on web, + Load balancer, By name, and Name shop.example.com. Port 8080 is Hello's now, so Shop takes 8081.
This time It will make finds the proxy there:
- On web's shared proxy, WebSharedProxy, there already at port 80
- Then shop, answering shop.example.com, sending to web at 8081 and asking each /health
and the run's Proxy step says Uses WebSharedProxy, web's shared proxy, at port 80. Once it's made, the same port answers both names:
$ curl -H 'Host: shop.example.com' http://web-1.example.net/hello
Hello, world, from web-1
Make the shop say something else
Both apps say the same, as the template wrote them. Shop's code is yours now, a package like any other, so a change goes as any change does. Get it into a workspace on Shop's version group:
$ plinto workspace create shop --version-group shop
$ cd shop
$ plinto workspace add Shop
Change what /hello says:
mux.HandleFunc("GET /hello", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Welcome to the shop, from %s\n", host)
})
and what its test wants of it, in src/Shop/src/main_test.go, so the build passes:
"/hello": "Welcome to the shop, from web-1\n",
Commit it in src/Shop, and send it as a review:
$ plinto review -m "Welcome to the shop"
Once someone approves it and it's merged, Shop's pipeline builds it and deploys it to Shop/Prod. The deployment takes each host out of shop before it restarts the app there, and puts it back once it answers. With two hosts or more, nobody asking shop.example.com meanwhile gets an error; with web-1 alone, Shop is away for the seconds its restart takes. Hello isn't touched either way:
$ curl -H 'Host: shop.example.com' http://web-1.example.net/hello
Welcome to the shop, from web-1
$ curl -H 'Host: hello.example.com' http://web-1.example.net/hello
Hello, world, from web-1
Point the names
In your DNS, point hello.example.com and shop.example.com at the group's hosts: a record for each host's address, since each runs the shared proxy. Then anyone can ask by name:
$ curl http://shop.example.com/hello
Welcome to the shop, from web-1
How it works
- WebSharedProxy is Caddy, deployed to every host of
webby its own pipeline, with plinto's agent for it, CaddyAgent, beside it. - Each load balancer with a name is a site in that Caddy,
http://hello.example.com, sending to the app's hosts at its port and asking each its health path. The agent writes the site when the load balancer is made or a host goes in or out, and Caddy reads it without a restart. - A deployment of Hello takes each host out of
hellobefore it restarts there, and back in once it answers. With two hosts or more, one always serves; a group's only host goes out anyway. - Any other name reaches the catch-all site, which answers 404. The proxy's own health is
/.plinto/health. - The next app made by name on
webjoinsWebSharedProxy, and nothing else changes. The first one by name on another group makes that group's shared proxy.
Move a load balancer you already have
A load balancer of kind caddy you made before, behind a proxy of its own, moves onto the shared proxy in the Load balancers app: open it, Edit…, and set
| Setting | To |
|---|---|
environment | WebSharedProxy/Prod |
on | web, the shared proxy's group |
host | its name, such as orders.example.com |
Its old proxy keeps running, answering 503, since nothing sends to it any more.
What it doesn't do yet
- HTTPS. The shared proxy answers plain HTTP.
- Your DNS. You point the names by hand.
- A name twice. Each name is one load balancer's on a shared proxy: a second one asking for it is refused.
- Taking an unused proxy off. A proxy no load balancer uses keeps running until you delete its environment, which takes it off its hosts.