Launch your first application
In this walkthrough you deploy a small web app: two nginx containers on two members, with an HAProxy load balancer in front on the third. Then you check it, curl it and restart it. About twenty minutes.
You'll need a cluster called lab with members node-a, node-b and node-c, formed from this directory. If you don't have one yet, start at Launch your first machine and Form a cluster. We'll use these tailnet addresses:
| Member | Tailnet address |
|---|---|
node-a | 100.64.0.11 |
node-b | 100.64.0.12 |
node-c | 100.64.0.13 |
1. Build the image (5 minutes)
sg runs whatever image you name, so first make one with a web server listening on port 8080 and a /healthz endpoint. Do this on any member; the cluster shares images.
ssh [email protected] # node-a's public address
cat > /root/hello.conf <<'EOF'
server {
listen 8080;
default_type text/plain;
location = /healthz { return 200 "ok\n"; }
location / { return 200 "hello from $hostname\n"; }
}
EOF
lxc launch ubuntu:24.04 build-hello
lxc exec build-hello -- cloud-init status --wait # first boot must finish before apt works
lxc exec build-hello -- sh -ec 'apt-get update -q && DEBIAN_FRONTEND=noninteractive apt-get install -y -q nginx'
lxc file push /root/hello.conf build-hello/etc/nginx/conf.d/hello.conf
lxc exec build-hello -- sh -ec 'rm -f /etc/nginx/sites-enabled/default && nginx -t && systemctl enable nginx'
lxc stop build-hello
lxc publish build-hello --alias hello-web-1
lxc delete build-hello
exit
You now have a container image called hello-web-1. It can't boot as a VM; for that, see Launch a VM. Why images?
2. Write application.yaml (3 minutes)
In the same directory as your machine and cluster specs:
api_version: genesis/v1
application:
name: hello
cluster: lab
groups:
- name: web
count: 2
image: hello-web-1
target: {web-0: node-a, web-1: node-b}
size: {cpu: 1, memory: 1GiB}
env: {APP_MODE: demo}
expose:
- port: 8080/tcp
health: [/usr/bin/curl, -fsS, http://127.0.0.1:8080/healthz]
load_balancers:
- name: edge
kind: haproxy
balances: [web]
target: {edge-0: node-c}
listen: 8088/tcp
check: {type: http, path: /healthz, expect_status: 200}
Line by line:
cluster: labmust be a cluster registered in this directory's ledger.count: 2andtargetgo together: exactly two entries, keyedweb-0andweb-1, each naming a member. The instances will be calledhello-web-0andhello-web-1.sizesets LXD's CPU and memory limits.envbecomes environment variables in the instances.exposepublishes port 8080 of each instance on its member's tailnet address.healthis a command run inside each instance. Exit 0 means healthy. Container groups must have one.load_balancersputs one HAProxy instance (hello-edge-0) onnode-c, listening on 8088 and checking/healthzon each backend.
3. Register it (1 minute)
sg application init --config application.yaml
application.yaml: valid (genesis/v1). registered hello on cluster lab.
init contacts nothing. It refuses a malformed spec, an unknown cluster, a target that names a non-member, or a name that's already registered to another file or cluster. Bindings never move silently: to rename, use a new name.
4. Plan (1 minute)
sg application plan --config application.yaml
plan observes every member and prints the ordered changes and a hash. It runs no health checks and changes nothing.
Application hello on cluster lab — plan: 4 change(s).
1 instances.web medium instance hello-web-0 is absent on node-a; instance hello-web-1 is absent on node-b
2 app.health none run every declared instance's exec health argv before load balancer activation
3 lb.edge high create owned load balancer edge on node-c; backends …; listen 100.64.0.13:8088; …
4 app.verify none verify workstation reachability and the load balancer listeners before success
Plan hash: 3f1c…
Plan only: no health command, verification or host change ran.
The two none rows are gates, not changes: health before the load balancer takes traffic, and reachability from your workstation before the deploy counts.
5. Deploy (5 minutes)
sg application deploy --config application.yaml
deploy builds its own fresh plan, shows it and asks:
Deploy application hello: 4 ordered change(s)? [y/N]:
Type y. sg creates the instances on their members, attaches the proxy devices for port 8080, starts them, runs the health commands, configures HAProxy with the healthy backends, and finally checks every exposed port from your machine. If all of that passes:
application hello run <run-id> finished: ok; 4 changes applied and verified; generation 1 recorded.
If anything fails, the run stops and records it, and no generation is recorded. Fix the cause and deploy again.
6. Look at it (2 minutes)
sg application status --config application.yaml
Application hello — 2 instance(s) observed.
INSTANCE STATE HEALTH MEMBER DRIFT
hello-web-0 RUNNING healthy node-a none
hello-web-1 RUNNING healthy node-b none
HEALTH is the result of running your health command just now. A stopped instance shows not-run and drift stopped; a missing one shows absent and missing-instance. It's read-only and never repairs drift. Now try it from your workstation:
curl http://100.64.0.11:8080/ # hello from hello-web-0
curl http://100.64.0.12:8080/ # hello from hello-web-1
curl http://100.64.0.13:8088/ # through HAProxy: either one
7. Restart one instance (1 minute)
sg application restart --config application.yaml web-0
Restart application instances hello-web-0? This interrupts service. [y/N]:
The selector can be a group (web), an instance key (web-0) or a full name (hello-web-0). Leave it out to restart everything. A restart re-checks health but doesn't create a generation.
What next
- Scale, upgrade and roll back this app.
- Launch a VM when a container isn't enough.
- Add a database for it to talk to.
- When you're done, destroy it.