Database setup for k8s server reddit / selfhosted ---------------------------------------------------------------------- p-friedrich · 2023-12-26 16:15 Hey everyone, I'm looking for some advice regarding a database setup. I'm currently moving all my self hosted services (vaultwarden, nextcloud, tandoor, gitlab,...) to a new server. Also, I'm switching from docker compose to a microk8s setup with one node. So all in all a couple of bigger changes and an opportunity to rethink previous decisions. With regards to databases, currently every service had it's own database container as part of the docker compose. Now I'm unsure if that is still a good approach or if I should rather maintain one database instance. AFAIK all the services I use can connect to an external Postgres db. Are there any advantages to one way or the other? Also, would you host the database inside k8s? ---------------------------------------------------------------------- adamshand · 2023-12-26 19:30 There's no right answer, it's just trade-offs. Using a single database for multiple services is slightly less resource hungry and means fewer containers to manage. Having a database per service/stack is easier to manage. Each service has its own isolated database. You can stop, upgrade, delete, or back it up without worrying how it affects any other service. It bugged me when I first started using Docker, but I now much prefer having having a database per service/stack. ---------------------------------------------------------------------- p-friedrich · 2023-12-26 20:14 Thanks for the perspective. Resources should not be an issue, the machine I got is way too powerful as it is. I do like the greater isolation when going one db per service. Also I probably cannot guarantee that every service I want to use will support Postgres. So I might have to spin up a dedicated MariaDB or similar in the future. That would defeat the point of a single maintenance obligation. ---------------------------------------------------------------------- adamshand · 2023-12-26 20:54 That's one of the things I like, no longer trying to get everything to work with a single database. Just do whatever is recommended for each app and get on with it. :-) ---------------------------------------------------------------------- PunyDev · 2023-12-27 11:56 I used to have 1 monolith postgres instance with bitnami's postgres stack for all my services. Switched to [CNPG](https://cloudnative-pg.io/documentation/1.21/) for each service per database and I am loving it so far. ---------------------------------------------------------------------- ragibkl · 2023-12-27 13:55 >Also, would you host the database inside k8s? I am doing this now. It's convenient to manage the db containers the same way I manage the apps that use them. You might not face any hurdles just yet with just a single k8s node, but if you plan to have more nodes, do look into how you'll work with persistent volumes. I believe with docker-compose, you simply define the volumes and mount them onto a directory inside the container. The volumes are actually directories that live on the same host, and persist with the host. I think you can still use a similar hostPath volume with kubernetes: [ https://kubernetes.io/docs/concepts/storage/volumes/#hostpath](https:/ /kubernetes.io/docs/concepts/storage/volumes/#hostpath), if you're only on a single k8s node. But you might want to look into alternatives when you play with multi node cluster. I currently use Longhorn storage. I'm using Rancher k8s as well, so YMMV using this on microk8s. Also, I don't have any heavy production workload, just a homelab setup to tinker with stuffs. Take my advice with a grain of salt. ---------------------------------------------------------------------- p-friedrich · 2023-12-28 16:47 What is the advantage of using CNPG instead of just spinning up a database pod in each service deployment? ---------------------------------------------------------------------- p-friedrich · 2023-12-28 16:50 I went with openebs with a ZFS backend for persistent storage. Not sure if that will provide any advantages for my use, but setting it all up is half the fun :-) ​ >Also, I don't have any heavy production workload, just a homelab setup to tinker with stuffs. Take my advice with a grain of salt. most of my self hosted services have a user count of 1 XD