kivikakk.ee

La herramienta del día es nftrace. I couldn’t work out why some pods weren’t able to communicate with each other across the Tailscale mesh. Suspected ACLs, suspected routes weren’t getting installed correctly (p.s. ip route show table 52 (!?)), suspected local firewalls, suspected so much. tcpdump only gets you so far.

Finally, on the target node:

$ doas -s
# nix shell nixpkgs#nftrace nixpkgs#nftables
# nftrace add ip daddr 10.59.1.213
# nftrace monitor

Try the request that isn’t making it through a bunch of times until you can isolate the exact sequence. ^C, nftrace remove, and read carefully:

trace id daac839a inet nftrace-table nftrace-chain packet: iif "tailscale0" ip saddr
100.67.157.26 ip daddr 10.59.1.213 ip dscp cs0 ip ecn not-ect ip ttl 64 ip id 32261
ip protocol tcp ip length 60 tcp sport 33233 tcp dport 9090 tcp flags == syn tcp
window 64480
trace id daac839a inet nftrace-table nftrace-chain rule ip daddr 10.59.1.213 meta nftrace
set 1 (verdict continue)
trace id daac839a inet nftrace-table nftrace-chain policy accept
trace id daac839a ip filter FORWARD packet: iif "tailscale0" oif "cni0" ip saddr 100.67.157.26
ip daddr 10.59.1.213 ip dscp cs0 ip ecn not-ect ip ttl 63 ip id 32261 ip length 60 tcp
sport 33233 tcp dport 9090 tcp flags == syn tcp window 64480
trace id daac839a ip filter FORWARD rule counter packets 44827 bytes 28768164 jump
KUBE-ROUTER-FORWARD (verdict jump KUBE-ROUTER-FORWARD)
trace id daac839a ip filter KUBE-ROUTER-FORWARD rule ip daddr 10.59.1.213 counter packets
5001 bytes 6279235 jump KUBE-POD-FW-FIAOHC4WHRKERAQ6 (verdict jump
KUBE-POD-FW-FIAOHC4WHRKERAQ6)
trace id daac839a ip filter KUBE-POD-FW-FIAOHC4WHRKERAQ6 rule counter packets 5 bytes 300
jump KUBE-NWPLCY-ZYSQVVSY5LQY7Q46 (verdict jump KUBE-NWPLCY-ZYSQVVSY5LQY7Q46)
trace id daac839a ip filter KUBE-NWPLCY-ZYSQVVSY5LQY7Q46 rule limit rate 10/minute burst 10
packets meta mark & 0x00010000 != 0x00010000 counter packets 5 bytes 300 log prefix
"DROP by policy monitoring/prometheus-k8s" group 100 (verdict continue)
trace id daac839a ip filter KUBE-POD-FW-FIAOHC4WHRKERAQ6 rule meta mark & 0x00010000 !=
0x00010000 limit rate 10/minute burst 10 packets counter packets 5 bytes 300 log group
100 (verdict continue)
trace id daac839a ip filter KUBE-POD-FW-FIAOHC4WHRKERAQ6 rule meta mark & 0x00010000 !=
0x00010000 counter packets 5 bytes 300 reject (verdict drop)

What’s that? log prefix "DROP by policy monitoring/prometheus-k8s"?? Guuaaaaauuuuu.

This is part 1 of x in a series.

I have spent most of my life avoiding DevOps-y type things. At GitHub I got familiar enough with kubectl to help debug the applications I had deployed on it, but that was almost a decade ago and I don’t remember a single bit of it.

Most of the things I run I deploy with a really simple systemd unit definition in the Nix module. Here’s an excerpt from the one for the Elixir app this blog ran on:

{
systemd.services.kv = {
description = "kv";
enableStrictShellChecks = true;
wantedBy = [ "multi-user.target" ];
after = [ "kv-migrations.service" ];
requires = [
"postgresql.service"
"kv-migrations.service"
];
script = ''
export KV_STORAGE_ROOT="$STATE_DIRECTORY"
${envVarScript}
${cfg.package}/bin/kv-server
'';
serviceConfig = {
User = cfg.user;
ProtectSystem = "strict";
PrivateTmp = true;
UMask = "0007";
Restart = "on-failure";
RestartSec = "10s";
StateDirectory = "kv";
StateDirectoryMode = "0750";
};
inherit environment;
};
}

It’s very basic, and it worked beautifully! I love that, with NixOS, you can package a reproducible build (with all its dependencies), deployment strategy, and configuration schema all in one place. It’s so damn clean, and it works wonderfully for homelab- or personal services-scale systems. (For more, try Xe’s All Systems Go! talk, Writing your own NixOS modules for fun and (hopefully) profit.)

The downside is that this is not exactly a high-availability setup. When any of the dependencies of a service like this change — such as a new cfg.package, or change in environment — the result is that the existing service is stopped, the service is swapped out, and then the new one is started.

There can often be 10–30 seconds between the stop and start, depending on how much else the nixos-rebuild has to do. And while a failing build won’t leave you with a stopped service — you won’t even get that far — if the build succeeds, but the new service fails to come up for some reason, then you’ll be scrambling fast.

This being NixOS, getting your service back up is as easy as switching to the previous generation, and can be done very fast, but still, it’s not great. Realising this, and still very much wanting to use Nix as a build orchestrator in places where this isn’t an acceptable trade-off, it was time to learn a devops.


Structurally, Kubernetes seems relatively sound, giving us language for defining the shape of a deployed system upon many different axes. It is very YAML and it is very containers, neither of which I am the hugest fan of, but I felt pretty sure there would be tools to help with the former, and Nix my beloved has beautiful solutions for the latter.

If, like me before the start of this exercise, you don’t really know about the model Kubernetes gives you to work with, you might find useful David Ventura’s blog post, A skeptic’s first contact with Kubernetes. If I had found it before and not immediately after coming this far it would’ve been super helpful -_-

One thing worth mentioning is that, as a Very Nix Person (and Very Dissociated Person), I really need my infrastructure to be described in a version-controlled way. Ideally, I would be able to tie all of my infra back into the same place (which is vyx, a Nix flake).

So I decide to start up a cluster and begin experimenting. I hate Docker, Inc. with a passion — I will never forgive them for getting rid of Docker for Mac’s cute whale — plus I want to learn somewhere where I can actually deploy things, so I decide to start with k3s on my VPS. How I chose k3s to begin with, I’m not so sure — maybe because it has relatively few options exposed in its Nix module. Lightweight sounds good, and it’s a “certified Kubernetes distribution”. Whatever that means, it must be good!

NixOS has the option services.k3s.manifests, which is described as “auto-deploying manifests”. Perhaps this is the magic sauce I need to get my infrastructure as code!?

(The answer is, no, it isn’t — the entire cluster is restarted when you change its values, because NixOS. Teehee.)

Nonetheless, I struggled through writing some early manifests this way. Writing YAML in Nix is way better than writing YAML, and very easy to parameterise, extract functions, and so on. I had seen mention of Helm charts here and there, and while I felt like one day I would need to come to terms with them, I preferred to leave that until as late an opportunity as possible. As a bonus, using k3s auto-deploying manifests in this way meant I could write a NixOS module to deploy an application in Kubernetes, without a single line of raw YAML.

So, terrible in many respects — now bringing down an entire cluster on each change instead of just the relevant services (!!!) — but an introduction nonetheless. We are now at the point of siguiente:

  • Decided to turn that homelab server into a gaming PC instead, haha psyche! Instead decided to learn better how to cross-build things and operate k3s without trying to shove everything through a NixOS module.

Part 2 will cover building our own software ready for orchestration (using Nix — we won’t write a single Dockerfile, promise, and as a little bit of a spoiler, we won’t write a single Go template either), and the unique fun presented by developing on aarch64-darwin while largely deploying to x86_64-linux. :)

Just a quick field report. I wanted to try ExternalDNS; I use bunny.net (obviously), and found external-dns-bunny-webhook! Perfect!

Only, it crash looped on startup. After asking myself whether I was really bothered enough to try getting a development environment setup for this (and then asking Annie, who helped resolve it to a yes), I rewrote the build process and development environment in Nix and got a fork with a fix going.

Given the sorry state of GitHub, I figured it was time to start opening “pull requests” there in the form of issues that contain instructions for fetching from off-site. Included here for posterity.

I really wanted to get this working for me, so I ended up forking the project with my fix and build environment. The webhook currently doesn’t handle root records being handed to it by ExternalDNS correctly, and will panic if that happens.

You can find the source here: https://nossa.ee/~talya/external-dns-bunny-webhook.

If you’d like to pull the relevant commit, this should do the trick:

$ git fetch https://nossa.ee/~talya/external-dns-bunny-webhook acb21849b8292222d7e0c85ac8d3dea913147bad
$ git cherry-pick FETCH_HEAD

The patch is inline here if you’d rather apply it directly with git am:

(elided)

This probably doesn’t apply to nearly everyone in the target demographic, but the connection to me seems pretty clear.

I like computers. Static site generators are neat; conceptually they are clean, they ask for almost no infrastructure for your deployed site, are performant as heck, all of that.

I also have ADHD. One extra step can be the difference between a task ever getting done or not.

I also enjoy writing, and would like to do it as often as inspiration strikes.

If I look at my posting history, there are some clear periods where I happily post a lot, and then there are the other times.

  • The former are periods where I am using something where I can write and publish a piece of work entirely from my phone, or a browser tab lazily opened and then closed.
  • The latter are those periods where I am relying solely on an SSG, where publishing usually minimally involves creating a file, writing in it, running a server process to preview the result as I write, calling it done at some point, then running some kind of generate and upload process, as well as committing to version control.

I’ll note too that “well you can always write a draft in a note and then do the publishing work later” doesn’t help, at all — it hasn’t made anything simpler, that’s actually adding a step.

We want to reduce the cycle time as much as possible. Giving yourself aliases and shortcuts to make the necessary steps involved in publishing with SSGs closer to hand is great, but for me at least, the results speak for themselves :)

This keeps happening:

  1. I read a blog post I really like; maybe I found it on Lobsters, maybe it was a link via another blog, who knows. I liked the post.
  2. I read other posts on their blog. I read the about page, or look at some projects! This is a cool person!
  3. I decide I’d love to keep reading their blog. I look for an RSS/Atom feed, so I can know the next time they post, and the next, and the next!
  4. There is no RSS or Atom feed. There is no way to find out the next time they post, short of manually checking every so often, and that is not practicable.
  5. Sad.

This happens surprisingly often — “surprisingly” because, if you don’t give your readers some way to actually be “your readers”, you kind of guarantee you will not have any readers! In which case, I mean, it’s cool that you write and all, but see point number 5 above: sad!! I want to read your posts! Please let me!

(I am writing this post partly so that I can include it in the emails I send to folks when I find out they don’t have a feed I can find. If I have sent this to you, I mean no offence, nor do I wish to put work onto you! I’d just like to share that I think your blog is neat, enough so that I felt moved to reach out to you.)