My Nextcloud died after a reboot (and the database was 'Ready' the whole time)
Homelab diary, entry #1. I just break my own stuff and write about it. Take everything with a grain of salt.
So this Sunday I did what every responsible homelab person do: apt update, apt upgrade, reboot. New kernel, new systemd, new everything. What can go wrong, right?
Well. Nextcloud can go wrong.
The setup
Single node k3s on Ubuntu, running Nextcloud, Postgres, Collabora, behind Traefik with cert-manager. Nothing fancy. A little bit messy yaml files in a folder, as it should be.
The symptoms
After the reboot the desktop sync client was complaining, and /status.php was returning 500. I checked the pods and everything was green, Running, 1/1. Very suspicious, because the site was clearly not working.
Then I ran occ inside the Nextcloud pod and got this:
Failed to connect to the database: connection to server at postgres (10.43.x.x), port 5432 failed: Connection refused
Ok. So the database is the problem.
Digging
First thing I tried is pg_isready inside the postgres pod itself. Also 'no response'. So it is not DNS, not the Service, not the network. Postgres just doesn't listen on the port. The pod is Ready only because I had no readiness probe, so Kubernetes basically just saw a running container and was happy (and it made me happy for a while, too. However this happiness was false, even if it took long).
The logs were completely empty, which is weird, because postgres always says something when it starts. Then I looked at the processes in the pod:
PID STAT TIME COMMAND
1 R 6:32 gosu postgres /usr/local/bin/docker-entrypoint.sh postgres
6 minutes of CPU time in a pod that is 6 minutes old. So one core is at 100% and it is doing nothing useful. The gosu (the little tool that switches from root to the postgres user) never gets to start postgres.
Wrong theories (there were a few)
- Broken disk or filesystem? Nope.
dmesgwas clean and the reboot was a proper one. - Crazy high open files limit? This is a known thing that makes some programs spin on startup. I checked,
ulimit -nwas 1048576. Normal. Theory dead.
The fix that worked (but I don't fully understand)
The image is postgres:16-alpine and the postgres user there is uid 70. If the container already starts as that user, the entrypoint skips gosu completely. So I added this to the pod spec:
spec:
securityContext:
runAsUser: 70
runAsGroup: 70
Postgres started right away. Why was gosu spinning after the reboot? Honestly no idea. I just walked around the problem.
Making the fix permanent (and the yaml fights)
I applied the change with kubectl patch, which is fine until you forget about it and the next apply or replace quietly removes it. So the change must live in the yaml file. While I was there I fixed another technical debt: I had a patch-ingress.yaml next to my main yaml, and both of them defined the same Ingress with different middlewares. Whoever was applied last won. Of course that was making problems too. I merged them into one file. Never again.
The second round of pain
After the apply the new postgres pod was stuck at 0/1. The log said it is doing crash recovery, and then:
FATAL: could not signal for checkpoint: Permission denied
I was already writing theories about the kernel . Then I did a full rollout restart on both Nextcloud and the database, and everything came back. Fine. Sometimes the answer is just 'turn it off and on again'.
My best guess is that the old postgres had too little time to shut down (default grace period is 30 seconds and my checkpoints were slow), got killed, and the new pod walked into a messy state. But this is a guess, not a fact. Nevertheless, I added strategy: Recreate into its yaml.
What I learned
- Add readiness probes. I mean, I knew that, but who has time for everything in a homelab?
RunningandReadymeans nothing if there is no probe. Apg_isreadycheck would show the problem in the first minute. - Use
strategy: Recreatefor single-replica databases with a ReadWriteOnce volume, so two pods never touch the same data directory. - Give postgres time to stop. I will set
terminationGracePeriodSeconds: 120. - Put every patch back into the yaml. The yaml is the truth, not your shell history.
- Do not keep two files for the same object.
- Backups. I had zero database backups. Now there is a
pg_dumpand I will put it in cron.
Oh, and I had two rubber ducks helping me debugging, Gemini and Claude. They were wrong two or three times, but it was good at asking for the right command outputs. If they get to have answers like these, my original rubber duck is going to need a retire plan soon.
I really want to find out why gosu was spinning. If you have idea, don't hesitate and tell me!