Launch a VM

Every application group runs as containers unless you say otherwise. Add type: vm and the same group runs as full virtual machines on the same cluster. This walkthrough builds a VM image, deploys two VMs with a bigger root disk and a data volume, and gets a shell in one. About thirty minutes, most of it image building.

There is no separate "launch a VM" command. A VM is always an instance in an application group, so it gets the same plan, confirmation, health check, generations and destroy as everything else.

You'll need the lab cluster from Form a cluster. If you haven't deployed a container application yet, do Launch your first application first; this page assumes it.

Container or VM?

Choose a container whenChoose a VM when
It's an ordinary service: a web app, a worker, a cacheIt needs its own kernel: Docker or Kubernetes inside, kernel modules, a different kernel version
You want fast starts (a second or two) and low overheadYou want a hardware boundary between it and the host, not just namespaces
Many small instances per memberIt needs a raw block device (a database, a filesystem you format yourself)
It runs PostgreSQL or ClickHouse through sg: services always run in VMs

VMs cost more. Each one reserves its own memory, boots its own kernel and takes ten to twenty seconds to be ready.

1. Build a VM image (10 minutes)

The steps are the same as for a container, with three differences: --vm on launch, a wait for the VM's agent, and cloud-init clean before publishing so every VM made from the image runs first-boot setup fresh.

ssh [email protected]                 # any cluster member
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-vm --vm -c limits.cpu=2 -c limits.memory=2GiB

# lxc exec into a VM needs the LXD agent, which starts a few seconds after boot.
until lxc exec build-hello-vm -- true 2>/dev/null; do sleep 3; done
lxc exec build-hello-vm -- cloud-init status --wait

lxc exec build-hello-vm -- sh -ec 'apt-get update -q && DEBIAN_FRONTEND=noninteractive apt-get install -y -q nginx'
lxc file push /root/hello.conf build-hello-vm/etc/nginx/conf.d/hello.conf
lxc exec build-hello-vm -- sh -ec 'rm -f /etc/nginx/sites-enabled/default && nginx -t && systemctl enable nginx && cloud-init clean --logs'
lxc stop build-hello-vm
lxc publish build-hello-vm --alias hello-vm-1
lxc delete build-hello-vm
exit

Check the image type:

lxc image list hello-vm-1 -c lt
# hello-vm-1 | VIRTUAL-MACHINE

2. Write application.yaml (3 minutes)

api_version: genesis/v1
application:
  name: vmdemo
  cluster: lab
  groups:
    - name: app
      count: 2
      type: vm
      image: hello-vm-1
      target: {app-0: node-a, app-1: node-b}
      size: {cpu: 2, memory: 2GiB, root_disk: 20GiB}
      volumes:
        - name: data0
          pool: default
          size: 10GiB
          content_type: block
          attach: [app-0]
          mount: /srv/data
        - name: data1
          pool: default
          size: 10GiB
          content_type: block
          attach: [app-1]
          mount: /srv/data
      expose:
        - port: 8080/tcp
      health: [/usr/bin/curl, -fsS, http://127.0.0.1:8080/healthz]

What's different from a container group:

  • type: vm makes sg create each instance with lxc init … --vm.
  • size.memory is the VM's RAM. Unlike a container limit, a VM's kernel uses what it's given and rarely hands it back, so plan for the full amount: two of these take 4GiB from the members.
  • size.root_disk sizes the VM's own disk. Leave it out and you get the pool's default (10GiB on a stock LXD). It can grow later; it never shrinks.
  • content_type: block volumes attach only to VMs. Each instance gets its own here, since a volume used by two instances must stay on one member.

3. Deploy (5 minutes)

sg application init --config application.yaml
sg application plan --config application.yaml
sg application deploy --config application.yaml

The plan shows volumes.data0 and volumes.data1 (medium, the volumes don't exist yet) and instances.app (medium, both instances absent), the same shape as a container deploy.

4. Check it (2 minutes)

sg application status --config application.yaml
curl http://100.64.0.11:8080/          # hello from vmdemo-app-0
curl http://100.64.0.12:8080/          # hello from vmdemo-app-1

status runs your health command in each VM, exactly as for containers.

5. Get a shell (2 minutes)

sg doesn't set up SSH into instances, VM or container. Use lxc on the member that holds the instance:

ssh [email protected]
lxc exec vmdemo-app-0 -- bash

Inside, look at the disks:

lsblk -o NAME,SIZE,TYPE,MOUNTPOINT
ls -l /dev/disk/by-id/ | grep sg--volume
# scsi-0QEMU_QEMU_HARDDISK_lxd_sg--volume--data0 -> ../../sdb

The root disk is 20G, and the block volume is a second, raw disk. The mount field doesn't mount a block volume. Format and mount it yourself the first time, using the stable by-id name (the dashes in sg-volume- are doubled):

dev=/dev/disk/by-id/scsi-0QEMU_QEMU_HARDDISK_lxd_sg--volume--data0
mkfs.ext4 -L data "$dev"
mkdir -p /srv/data
echo "$dev /srv/data ext4 defaults,nofail 0 2" >> /etc/fstab
mount /srv/data

The data lives on the volume and survives restarts, image upgrades and rollbacks. The fstab line lives on the root disk, so an image upgrade (which recreates the VM) loses it. Bake the fstab line and the mount point into your image, or mount from your app's start-up script, rather than editing each VM by hand.

6. Health checks for VMs

health is required for container groups and optional for VM groups. Leave it out and:

  • deploy can't tell whether your software started, only that the VM is running;
  • status shows not-configured instead of healthy;
  • only the exposed-port check stands between a broken image and a new generation, and a later rollback can land on it.

Keep it unless the VM has nothing to check. When it does nothing more than run its init system, this waits for boot to finish and fails if any unit failed:

      health: [/usr/bin/systemctl, is-system-running, --wait]

7. Change it and clean up

Everything on Scale, upgrade and roll back works for VMs. Two things to keep in mind:

  • An image upgrade recreates each VM. Anything not on a volume or in the image is gone, including that fstab edit.
  • scale changes only count and placement. It doesn't add volumes, so new instances in a group like this one start without a data disk until you add a volume entry for them and deploy.

When you're done:

sg application destroy --config application.yaml

This deletes the VMs and the block volumes with their data. See Destroy and data safety.