Just enough complexity
This post assumes that you are familiar with Linux, containers and hosting
Recently I started thinking about how I want to deploy my self hosted services including this website. Everything is run as a container nowadays and my host operating system is only there to start the container runtime.
I've deployed a fair number of servers and applications for personal and professional use. I wanted to go through my inventory of skills and tools to find the best compromise between simplicity and maintainability and at the same time look for modern solutions.
What I ended up with may not be the simplest to get started with but the payoff is in the long term usage and maintenance. I found a setup where I can easily make changes, still understand the system and otherwise don't have to worry about it.
Immutable Operating Systems
So I started researching what operating systems are there which are optimized for container usage. I still wanted a normal Linux system which I can administer and know how it works. But it should be a more minimal and reliable system, which once set up, doesn't need much attention and at the same time doesn't introduce me to the complexities of Kubernetes. That's how I found out about immutable operating systems.
They don't have a normal package management. The root filesystem is read-only, only /etc and /var are writable and persisted, so anything you install and configure anywhere else is gone after a reboot or system update. The OS itself is actually shipped as an OCI1 image, the same technology of layered image your containers use, which is a nice symmetry. An update just pulls in a new image next to the current one, and rolling back means switching back to the previous deployment. Packages are installed as an additional layer over the base operating system layer. This gives us:
- reduced attack surface because our host system is lean and everything runs as a container
- system upgrades are low risk because of rollbacks and can therefore be automated
In my case I have chosen Fedora CoreOS (FCOS) which is the continuation of the original CoreOS. It has SELinux enabled by default, which is a nice additional safety layer coming from Debian, where I didn't bother to set it up or look into it properly. The initial provisioning is done via a Butane/Ignition file where you can declaratively describe the initial system state. This generally covers user accounts and SSH keys. It is comparable to cloud-init
variant: fcosversion: 1.6.0passwd: users: # core is the default user in FCOS - name: core ssh_authorized_keys: - ssh-ed25519 <your-ssh-key>Everything else stays out of the box, including automatic updates. Zincati is a service that ships with Fedora CoreOS. It checks for new releases and applies them automatically. This is perfect for my needs because everything I need runs in a container and Podman itself is pre-installed. Ignition just does the initial provisioning and I don't have to touch anything again.
Podman
If you're not familiar with Podman, it's an open-source, daemon-less container manager alternative to Docker. It delegates orchestration to systemd and this is where Podman Quadlets come into play. They are normal systemd unit files with a custom container section. Let's see how this looks like for the reverse proxy Caddy which serves this page.
The following demonstrates a rootful usage of Podman because the containers are started by systemd and therefore as root. You can easily go the rootless route if you need to.
[Unit]Description=Caddy Reverse Proxy# start only after networking is upAfter=network-online.target[Container]Image=docker.io/caddy:2ContainerName=caddyNetwork=proxy.networkPublishPort=80:80PublishPort=443:443# :Z relabels the bind mount for SELinux so only this container can access itVolume=/opt/caddy/Caddyfile:/etc/caddy/Caddyfile:ZVolume=/opt/caddy/data:/data:Z# enables automatic container updates by PodmanAutoUpdate=registry[Install]WantedBy=multi-user.target[Unit]Description=Caddy Proxy Network[Network]Driver=bridgeIf you've ever used a docker-compose.yml it should feel familiar. We just describe the service and all its dependencies like networks and volumes. Only the [Container] and [Network] part is Podman specific, the rest is vanilla systemd.
To start this container we run
sudo systemctl daemon-reloadsudo systemctl start caddy.serviceYou're probably wondering why we start .service but we defined the container itself as .container. The missing piece is, that Podman hooks into systemd and creates the .service directly from the container definitions and converts them to standard unit files with an exec command which invokes the Podman CLI. We can inspect the generated files with
/usr/lib/systemd/system-generators/podman-system-generator --dryrunWhich prints this (and some more which I removed for brevity)
...[Service]Environment=PODMAN_SYSTEMD_UNIT=%nKillMode=mixedExecStop=/usr/bin/podman rm -v -f -i caddyExecStopPost=-/usr/bin/podman rm -v -f -i caddyDelegate=yesType=notifyNotifyAccess=allSyslogIdentifier=%NExecStart=/usr/bin/podman run --name caddy --replace --rm --cgroups=split --network systemd-proxy --sdnotify=conmon -d -v /opt/caddy/Caddyfile:/etc/caddy/Caddyfile:Z -v /opt/caddy/data:/data:Z --label io.containers.autoupdate=registry --publish 80:80 --publish 443:443 docker.io/caddy:2...The [Container] section is gone and replaced by a standard [Service] section with the auto-generated Podman commands to manage this service. So this is extremely convenient. We just copy the quadlets files to our server (/etc/containers/ is persisted) and our services get orchestrated like everything else on this machine. No separate Docker daemon, no YAML config. For me it's just one abstraction layer less which ironically makes it even more powerful! Start your service after your SSH server? No problem, just describe the dependency to systemd.
Logging
Nothing special, no configuration needed. Because it's a systemd service you can look at the logs like everything else.
sudo journalctl -u caddy.serviceAutomatic container updates
As previously mentioned Podman runs without a daemon. You're maybe wondering how it can do automatic updates in the background then. A timer! It's just another basic systemd building block. Think of it like a cronjob if you're not familiar.
sudo systemctl enable --now podman-auto-update.timerPyinfra
The last part I included in my setup is kinda optional but I love declarative deployments. We could've stopped here and just rsync-ed the files and ran the systemd reload/enable commands with a little script. But that's no fun and I have a personal pet-peeve with Bash scripts.
So the common solution is generally Ansible. I've used it in the past and I like how the playbook reads like a deployment manual. The idempotent2 operations I don't want to miss anymore but running Ansible is slooooow. But why is that? For every task in the playbook, Ansible copies a zip archive with the Python code for this operation to the host and executes it with the host supplied Python interpreter. I don't like either of those so I began looking for an alternative solution and found pyinfra.
pyinfra is a python-native, agentless automation tool that runs commands over ssh — concurrently, idempotently, and 6× faster than ansible.3
This is my personal sweet spot: lean deployment definitions without turning into a messy Bash script. It gives enough structure but under the hood it's idempotent SSH commands. You don't need a Python interpreter on your destination machine. It just executes standard CLI commands over SSH.
When you run a deployment there are two phases
- Prepare - Gathers the current system state
- Execute - Executes only the commands to reach the desired state
This simple structure makes it possible to only run the commands which are needed to bring the system into the desired state. In practice it looks like this.
web = [ ("my-host", { "some-key": "some-value"})]This assumes that my-host is already defined in ~/.ssh/config.
from pyinfra.operations import files, systemdfiles.put( src="quadlets/caddy.container", dest="/etc/containers/systemd/caddy.container") systemd.service( "caddy", daemon_reload = True, running = True, restarted = True)We deploy like this (I am using uvx to install pyinfra in this example)
uvx pyinfra inventory.py deploy.pyWhat I really appreciate is that if you edit the deployment in an IDE with a Python language server you get autocompletion and documentation for every function. And because it's all just standard Python we have the full power of a programming language instead of a YAML/Jinja mix.
What I mean by this is that I don't have to look up how to define a loop in Ansible, I can just use Python itself for that.
quadlets = [ "caddy.container", "proxy.network"]for quadlet in quadlets: files.put( src=f"quadlets/{quadlet}", dest=f"/etc/containers/systemd/{quadlet}" )Conclusions
This initially doesn't seem very lean or simple but once you've got it all together there are not many moving parts and I would argue they are the basics of configuring and maintaining a Linux system. The knowledge of systemd unit files is applicable to all other systemd unit files, pyinfra can be used for other things.
The final stack looks like this:
- Fedora CoreOS as the foundation. Read-only filesystem and OS upgrades shipped as OCI images. After that it's just a host system you don't have to pay attention to (doesn't mean you shouldn't audit things but you know what I mean)
- Podman Quadlets for integrating our services into systemd. The integration feels so smooth and there are much more things I could show, like defining a
.targetto treat different services as one unit or how you can manage environment variables. Again, nothing I just said is Podman specific. - Pyinfra to manage our files and start the services. This is a replacement for a custom script to not have to worry about idempotency yourself and as an added benefit you get a much nicer CLI output. I will probably do a writeup on how you can write reusable blocks and your own facts for more modular and demanding deployments.
You can find my deployment setup at https://github.com/kevinschweikert/vps. Funnily enough I still run Rocky Linux, rsync to copy my website files and Ansible for provisioning but I've got everything ready for Fedora CoreOS and Pyinfra. I've deployed with this stack at work and I'm really happy with it. It recently updated a FCOS major version 44 to 45 without anybody noticing.
I know this is not a beginner tutorial but if you have any questions or if you have an idea on how to improve let me know.
Open Container Initiative https://opencontainers.org/ ↩
Means you can run it multiple times with the same outcome ↩
Pyinfra Homepage https://pyinfra.com/ ↩