My Incus OS infra explained in a blog post

Hi,

I don’t know if this is considered as spam? I wrote a long blog post, including a diagram, explaining how I use Incus OS, I thought this could be interesting to some people :slight_smile:

Thanks for writing and posting this. It is very useful review/testimonial on how you use Incus OS for self-hosting, your design decisions, the challenges, what worked and issues to address in the future. You write that it took you seven months to reach this state. This makes it a mature document that will help anyone planning on investing in using Incus OS.

Definitely not a spam :slight_smile: and well appreciated. Hopefully you will have more things to share in the future.

Thanks.

Definitely taking a look at, got new things from the document. However, it could have been a slightly more technical document, which would have allowed us to experience some new things. Thanks for sharing.

My blog audience is usually tinkerer, I didn’t want to hammer readers with technical details, this could be a separate blog post that focus on some components.

Which area would you like to see explained with more details?

I’m very interesting in CrowdSec with nftables in haproxy vm part. Can you show more details, please.

Im very interested in the opentofu patterns, and how you use cloud init. I have a very similar setup but struggle with some translations from podman compose world on oci containers.

Workstation OpenTofu talks to the Incus API over Tailscale - Tailscale (built into Incus OS), may be mentioned but i really interested the logic of that system.

I didn’t want to expose the API over the Internet, but Incus OS API is only reachable over my LAN although it is connected over tailscale for reaching VPN only services.

OpenTofu side there is one generic module and one folder per service

Every instance is a module "container" block with just a name, a type, an IP and maybe a memory limit.

I have a main.tf with lots of modules declared as below, one module = one incus container = 1 piece of service I run.

module "immich" {
  source        = "./modules/container"
  image         = incus_image.ubuntu_container.fingerprint
  instance_name = "immich"
  type          = "immich"
  ip            = "10.10.100.52"
  memory        = "12GiB"
  network       = incus_network.containers_net
  depends_on    = [module.proxy]
}

The module reads every file in files/<type>/ and turns them into cloud-init write_files entries that land in /etc/containers/systemd/. On top of that it adds some common files: an internal CA, registries.conf pointing to pull-through registry mirrors, the apt proxy config. Then it builds the user-data and puts it in an incus_profile as cloud-init.user-data:

quadlet_files = fileset(local.quadlet_source_dir, "*")

write_files = [
  for f in local.quadlet_files : {
    path        = "/etc/containers/systemd/${f}"
    content     = file("${local.quadlet_source_dir}/${f}")
    permissions = "0600"
  }
]

runcmd then does update-ca-certificates, decrypts secrets, systemctl daemon-reload, enables podman-auto-update.timer and starts <type>-pod.

I extended this a bit later to declare arbitrary files or even in-line files so I don’t have to put everything in /etc/containers/systemd/ :sweat_smile:

Instances have replace_triggered_by = [incus_profile.profile.config]. Any change to a quadlet file changes the user-data, so the container gets destroyed and rebuilt from scratch. The root disk is disposable so it is not an issue, only three Incus volumes survive, all with prevent_destroy:

  • /var/lib/containers: podman images and named volumes
  • /secrets: the instance’s age key
  • /var/cache/apt

Secrets

Files ending in .sops are encrypted with sops + age. At boot, a small script runs the ghcr.io/getsops/sops image with the key from /secrets/sops.txt and writes the plaintext next to the encrypted file, e.g. env.sops becomes env, used by EnvironmentFile=. If the key is missing it just skips decryption, so the box still comes up.

Quadlet

Here’s an exemple with a piece of immich :

# immich.container
[Unit]
Requires=redis.service postgres.service
After=redis.service postgres.service
BindsTo=redis.service postgres.service

[Container]
Pod=immich.pod
ContainerName=immich
EnvironmentFile=/etc/containers/systemd/env
Image=ghcr.io/immich-app/immich-server:release
AutoUpdate=registry
Volume=immich_library:/data

[Service]
Restart=always

[Install]
WantedBy=default.target
# immich.pod
[Pod]
PodName=immich
Network=immich.network
PublishPort=2283:2283

Cool! I saw a pointer to your blog post (lobste.rs post on the Fediverse?), read it, and immediately thought I wanted to look at your config to learn from it. And now you’re here!

Is it (or a sanitized version of it) available anywhere? I’ve looked but haven’t found anything.

I’m thinking about this, it’s highly custom so I don’t think it would make sense to share it, but why not put a sanitised version somewhere just as learning/support materiel indeed.

fantastic overview, thanks for sharing how the container module works in concert with cloud-init, podman quadlets. Would be neat to publish the container module if its generic enough to share. I have a similar but less advanced one, that forces files onto incus volumes that don’t get updated if the file contents change, so maybe embracing the complexity of cloud-init is worth experimenting for my community lab.

I just didn’t understand why you’re running quadlets inside system containers instead of running oci containers.

This is a really good question. So far, I needed to have multiple services working in a pod so I have a logical service running in each incus container (like a nextcloud with its postgresql, redis and vector), I just didn’t figured out how to achieve this easily in Incus, I also have to manage secrets and I don’t know how to deal with this in incus.

I should check again now I’m more familiar with incus :+1:

@victoitor one of the difficulties I ran into with oci containers on incus is providing files before command is run. Have to make a volume, push those files to a volume, and then attach. If doing this through terraform, I am not sure how to tell terraform to update when a file changes, because its tracking incus_volume, not the contents of said volume. This is one of the tricky papercuts that are solved differently (and different mental model) than system containers.

(I was able to make a snippet that reduced the boilerplate for have pre-command files, but it doesn’t update resources correctly)

Kubernetes?

Kubernetes is really good at declarative (GitOps) where everything is in Git, ephemeral (oci) containers, and secrets managements, which I saw was one of your frustrations.

It can also do declarative network policy configuration as well.

I use Kubernetes on a single node despite it usually being clustered because of the above features. And one of the things I really like that makes it easy is the way that it’s easy to set up a MicroVM runtime. MicroVM’s are these lightweight VM’s that run a stripped down linux kernel, just what is needed to run a container. While heavier than a true container, they are lighter than a full VM, especially since they are just OCI containers it’s not virtualizing a whole Linux distro. By isolating the applications from the kernel, it prevents the various Linux local privilege escalations, that would break out of containers, including Incus system containers, and get root on the host.

That’s my next step, I needed a good reason and real world usage for learning kubernetes.

Thanks a lot for this blog post, it inspired me to spin up a IncuOS VM on my proxmox host for testing and now I am in the process of migrating everything from proxmox to IncuOS and OpenTofu :smiley:

I am still in the very beginning of my journey with OpenTofu and IncusOS, but wanted to ask a few questions.

  1. How do you backup the data volumes via the restic instance? Are you mounting them as read-only volumes and then just upload the data? How do you ensure application-consistency?
  2. How does your DR plan look like? If the laptop dies, how do you plan to get the data back into the data volumes?

The backups are done using restic in a container which have all volumes mounted in /mnt/ and it backups /mnt/*, this is done declaratively. There are no consistency check, this is acceptable to me, otherwise I would schedule a plain dump on each postgresql in each container and backup this. From experience, except if the database is really busy, there are little to no issues doing a hot backup with postgresql.

Recovery plan is to get the backups, the required credentials are printed as QR code on a paper and also in a keepassxc database. Then apply the terraform code on a new instance, stop all containers, restore the backups.