News for hackers. Stuff that matters.
Re:PostgreSQL 14 on Kubernetes by manigandham on Wednesday October 06, 2021 @01:06AM (#28769410)
Because kubernetes is a fantastic platform that provides a lot of node and app management functionality out of the box. Stateful services like databases are not really a challenge anymore given recent developments in both K8S and database containerization.
Re:PostgreSQL 14 on Kubernetes by tiew9Vii on Wednesday October 06, 2021 @01:41AM (#28769589)
With a database I generally want to have it on its own dedicated host with fast NVME drives, memory, cpu etc, maybe some OS specific system level configuration options for performance etc. I don't want to binpack it on a cluster of nodes each contending for system resources. Also RDBMS databases are not really designed to be ephemeral/elastic, they are stateful/persistent by nature.

What does Kubernetes offer in this scenario?

I'm still hesitant to run things like DB's on K8's but have absolutely no problems running cloud native apps on K8's which can run ephemerally, be bin packed and only needing compute as all state is external to where the app is being executed.

Re:PostgreSQL 14 on Kubernetes by paulfurtado on Wednesday October 06, 2021 @03:19AM (#28770155)
Say you have 100 database clusters and you want to schedule them on dedicated servers.

A kubernetes node can advertise those local block devices as persistent volumes. Your StatefulSet can request that class of local storage and the kubernetes scheduler will then find a volume for the pods on a node that the pod can fit on.

You then get all the features of kubernetes operators plus the performance of dedicated nodes and local storage. When a local storage node disappears, your pod gets a new volume on a new node and can replicate from its peers, or start from a backup.

Kubernetes then offers metrics, certificate management, secret storage and management, load balancing, healthchecks, debugging tools, rich firewalling, RBAC, and a huge ecosystem of plugins. It gives you a unified API for all your workloads. Requesting a database then is as simple as a single API call to kubernetes with a spec for your database (ex: creating a MysqlCluster object).

The company I work for runs MySQL, ZooKeeper, memcached, hbase, hadoop, kafka, elasticsearch, and more in kubernetes with great success. Scale here in one prod region is ~10,000 pods on a clusters of ~600 nodes. There are about 900 separate MySQL clusters consisting of 3 replicas each, 150 separate zk clusters of 5 replicas each, etc.

Re:PostgreSQL 14 on Kubernetes by arianvanp on Wednesday October 06, 2021 @05:50AM (#28771070)
> when a node disappears you get a new volume

Actually no. Currently you need to manually delete the persistent volume claim. Followed by deleting the pod. For the statefulsets ti to create a new PVC.

There even was a race condition until like 1.21 that made this operation fail 20% of the time so you'd have to delete the pod twice or the system would deadlock.

It'd be nice if the PVC would claim a new PVC if the orchestration layer knows 100% the PV is gone but it currently doesn't do this yet.

It's not an enormous problem once you're aware of it. But it does mean things don't auto heal at all. A node going down with local storage on it needs manual intervention to get the system running and healthy again.

There might be some operators that automate this away but i haven't seen it yet in the wild.

Re:PostgreSQL 14 on Kubernetes by paulfurtado on Wednesday October 06, 2021 @11:45AM (#28774531)
We do actually automate this away by deleting PVCs when they are tied to PVs on nodes with our drain taint on them and their pod is in the pending state. We also delete pods owned my StatefulSets that have missing PVCs. These two control loops really grease the wheels. It's definitely frustrating that statefulset pvc management isn't fully declarative, but hopefully this improves.
Re:PostgreSQL 14 on Kubernetes by chousuke on Wednesday October 06, 2021 @12:54PM (#28775356)
How do you prevent someone (or something) from accidentally draining a node hosting a database that does not have a synchronised replica?

Kubernetes is neat, but destroying stuff is so easy (and the context mechanism is just begging for human error) that I am a bit leery about hosting large amounts of stateful stuff in it.

Re:PostgreSQL 14 on Kubernetes by paulfurtado on Wednesday October 06, 2021 @01:43PM (#28775954)
No database should lack an unsynchronized replica in general. A kubernetes admin could accidentally drain the host, but we run in AWS and AWS can kill nodes a lot faster than we can haha. We have enough servers in AWS that at least 5 fail per day. If some database is not replicated, someone is getting paged for it. We run MySQL with semi-sync replication for example: if a primary has two replicas, one replica must ack a write before the primary commits it, so you can lose the primary at any time and not lose data. That said, kubernetes has prevention mechanisms for this.

PodDisruptionBudgets allow you to specify the minimum number of replicas. The drain command calls evict instead of delete and evict honors these restrictions. This is helpful for node draining, but not necessarily helpful if someone accidentally deletes a StatefulSet. However, nothing in kubernetes deletes PVC objects automatically, so if you delete the pod, the volume remains bound to the PVC until you delete it.

preStop hooks are used in all the database pods to ensure that if the current pod is the primary, leadership is correctly changed to a replica before proceeding with the delete. We combine this with extremely long termination grace periods and alerting if termination is taking too long so that an ops team can look into the issue.

preStop hooks are great, but they also are somewhat of a time bomb if not careful, so some of our database operators make use of finalizers instead. With finalizers, nothing happens when a pod enters the terminating state until an operator marks the pod's finalizer as completed. This flow is really nice because you end up with behavior where the kubernetes cluster admins or built in controllers effectively are really gently requesting that a pod terminate, and it's up to the operator of that pod to decide exactly how and when that occurs.

For non-local volumes, like EBS PVCs, once the PVC is deleted an operator will snapshot the volume during a finalizer before allowing the volume to be fully deleted so that we can recover data if this ever accidentally occurs. This was the first protection we ever implemented when initially adopting kubernetes back when we had no idea what we were doing so that we could always recover data in the event of admin mistakes. We can't do this for local volumes, however, any team making use of local volumes truly needs to stay on top of replication when running in the cloud, whether on kubernetes or not or else you absolutely will lose data. AWS local nvme volumes have limited write cycles just like the ones in on-prem servers. So ideally all databses are configured to not accept writes that cannot be replicated. And even with EBS, at scale we end up with something like 20 unrecoverable volume failures per year

Re:PostgreSQL 14 on Kubernetes by chousuke on Wednesday October 06, 2021 @02:35PM (#28776575)
Thanks for the detailed response. I was aware Kubernetes has hooks for pretty much everything, so getting details on how they are used in practice is interesting.

Mostly I'm concerned about preventing administrator mistakes; if you have enough privileges, it's distressingly easy to just accidentally delete all kinds of resources in the default configuration, especially since kubectl is context-sensitive. I like my automation software to have checks in it to prevent me from doing stupid things without explicitly disabling a number of safeties first.

I feel like Kubernetes is a powertool that requires a competent and trained admin team or managed access so that you simply can't make awful mistakes as a "regular user", but a lot of the hype around it seems to be focused on how "easy" it is.

To my eyes, it's indeed easy to just throw manifests from the internet at Kubernetes, but that is often akin to disabling SELinux because some blog says so; it may get things working, but it's not the competent choice most of the time.

I'm also a big proponent of storing everything in git repositories, so I do like how Kubernetes enables declarative configuration, though it seems much of the tooling in the ecosystem works against that by just creating resources in a cluster with no version control...

Re:PostgreSQL 14 on Kubernetes by paulfurtado on Wednesday October 06, 2021 @03:40PM (#28777399)
Cool thing about finalizers is that there's no straightforward way to delete them from the CLI, you need to explicitly patch the object to delete the finalizer so that goes a long way towards preventing instant mass deletion, they really do help a lot here.

The additional protection we have is via admission controllers which hook all API calls to prevent mistakes, of course, you have to foresee those mistakes and come up with a validation that prevents it. But even something as simple as "only 5 pods are allowed to be terminating at once" can help.

I absolutely agree about people claiming kubernetes is easy. It is easy to throw some helm chart at the kube API and suddenly have an app running with all it's databses configured. It is hard to keep them all running.

The pattern of operators seeks to solve this. Instead of a helm chart creating a MySQL statefulset, it creates a MySQLCluster that gets managed by a well-written operator following the best practices and correctly implementing all the hooks. The ecosystem is starting to converge on this finally and the beauty is that the sum of the industry's operational knowledge can be coded into these operators and we'll finally arrive at nearly fully self managing databases with backups and all.

The industry isn't there quite yet though, and so at least for stateful workloads you need significant operational experience. For background: I am the Tech Lead of our team that runs the kubernetes clusters and we provide all of the primitives our users need and control access. Each database type then has dedicated teams operating them and codifying operations into operators. There is a lot of work being done. But prior to kubernetes, all of these teams separately were coding against the EC2 API with no standardization, no unified view, ad-hoc failure detection, ad-hoc auditing, etc. Kubernetes is a very substantial improvement over that, but 90% of companies never reach a scale where this is necessary. But once extremely solid open source operators exist we may truly hit the dream of just applying some self managing operator manifest to an EKS or GKE cluster and getting an actually production grade database setup.

Version control is an interesting topic because it's hard to express transitive dependencies in that code. For example, the MySQL operator creating a StatefulSet and the Statefulset creating pods. It doesn't make sense to commit those lower level resources to git, they're not fully independent. However, for top level things, our build system produces artifacts from git and the deploy system creates them in the cluster. With this setup, only admins could directly apply them, which really helps with keeping things in sync with git.

Re:PostgreSQL 14 on Kubernetes by chousuke on Friday October 08, 2021 @05:22PM (#28804365)
I'm a bit late replying, but I just want to clarify my stance on the last paragraph; I don't think you need to store everything in the cluster in version control if they're created automatically as a result of applying the top-level configuration, If there's something important that's too dynamic to be in source control, I treat it as data to be backed up.

My ideal is that what is in the repository allows me to rebuild the system from scratch (assuming suitable hardware exists) to the point where it can be successfully used as a restore target for your backups following documented restore procedures.

Re:PostgreSQL 14 on Kubernetes by arianvanp on Wednesday October 06, 2021 @02:23PM (#28776424)
Is this part of the postgres controller or is this an own controller that you wrote?
Re:PostgreSQL 14 on Kubernetes by paulfurtado on Thursday October 07, 2021 @03:11AM (#28783169)
This is a controller we wrote in order to make local storage actually usable. Internally we have a "kube cluster operator" that handles the cluster-level ops like this, gracefully draining nodes, etc.
Re:PostgreSQL 14 on Kubernetes by FridgeSeal on Wednesday October 06, 2021 @02:20AM (#28769822)
As the other comment or pointed out, there’s a number of features now that tick these boxes:

- StatefulSets mean that K8s scheduling is aware that the workload is stateful and the auto-attach of drives works fantastically in my experience.

- NodeTaints/Tolerances and workload anti-affinities between workloads mean that you can very easily say “this workload runs on nodes with these features/labels” and “don’t run workload replicas on the same node” and “don’t run other workloads on the same node as this workload. Node groups on things like AWS EKS also make it really easy to get autoscaling groups of nodes with specific hardware.

Re:PostgreSQL 14 on Kubernetes by manigandham on Wednesday October 06, 2021 @05:13AM (#28770844)
Kubernetes is an orchestrator: You have servers. One of them is the master, the rest are workers. Master talks to the workers via ssh. You talk to the master via API. You tell the master node what you want running and it runs it for you. Then it continuously checks to make sure that what's actually running is what you wanted, and makes fixes if it isn't.

What you want to run, and how, where, under what conditions, etc. is all specified in declarative files. Like I said, stateful services are not a problem anymore. K8S can deploy a database service as instances exclusive to specific nodes with the appropriate storage and networking config.

It's like an invisible assistant to automate what you would be doing yourself, or using several other tools to do anyway. A well-tested way to handle most of the common infrastructure issues for any deployed software in a single platform.

EDIT: I'll add that running K8S itself can be challenging, but that's because complexity can't be erased but only abstracted. I recommend using managed K8S to offload this.

Re:PostgreSQL 14 on Kubernetes by arianvanp on Wednesday October 06, 2021 @04:23AM (#28770535)
Have a look at local storage provisioners. There are plenty of them

They give you raw access to storage without redundancy. Which is great if your workload on top (like https://k8ssandra.io) already is redundant. Whilst making sure workloads stick to where their data is stored

E.g. https://github.com/openebs/lvm-localpv

Re:PostgreSQL 14 on Kubernetes by alphabettsy on Wednesday October 06, 2021 @01:52AM (#28769658)
StatefulSets, Node Groups, and Node Affinity/Taints solve some of those issues.
Re:PostgreSQL 14 on Kubernetes by karolist on Wednesday October 06, 2021 @04:01AM (#28770401)
A database is not a service well suited for ephemeral, quick scale down and scale up container workloads that K8s really excells at, and if you have nodes specifically configured to run databases and nothing else then you should simply run them there directly and not mangle K8s to replicate what will essentially be equivalent to running them there directly.
Re:PostgreSQL 14 on Kubernetes by dividedbyzero on Wednesday October 06, 2021 @05:53AM (#28771095)
I've ran several small postgres databases and a kafka cluster on Kubernetes; in our case, the VM route would have added lots and lots of overhead with very questionable benefits, given that we didn't run a single prod VM ourselves (all managed by GKE) and hence we had good tooling, observability, monitoring for k8s workloads in place, but VMs were essentially treated as more like a set of disposable blackboxes (not entirely, but we'd only monitor certain aspects and leave all else to GKE)

Being able to get essentially the same result, but with our familiar processes, tooling, infra, that was really helpful getting things off the ground quickly and keeping them stable. We picked k8s/GKE way before that as a platform where a small set of abstractions allow us to do a lot of different things, and that makes it easy to debug unfamiliar workloads, keeping operations overhead low, and it worked out really well, and much easier for people with little ops experience to learn and get to a point where they could take care of their workloads themselves.

And it never felt we had to mangle k8s for our stateful workloads at all. Statefulsets, storage, node affinity etc. feel very much like organic building blocks and work really really well, with very few surprises. I guess k8s has come a long way as far as stateful workloads are concerned.

Re:PostgreSQL 14 on Kubernetes by manigandham on Wednesday October 06, 2021 @06:01AM (#28771144)
K8S runs workloads across servers in a declarative fashion. Ephemeral or stateful doesn't make a difference anymore.

Whatever you use to manage the nodes directly is basically what K8S provides (and more) already, so you're really just replicating effort and complexity instead.

Re:PostgreSQL 14 on Kubernetes by Arnavion on Wednesday October 06, 2021 @06:24AM (#28771312)
Indeed.

k8s orchestration is also useful for running multiple replicas of a database and using leader election to promote a secondary to a primary if the primary goes down. Doing this without k8s would require some kind of clustering anyway, to coordinate the leader election. So if you're already using k8s for everything else, you might as well also use it for this instead of introducing something else like pacemaker.

The one disadvantage is that leader election under k8s does need to be implemented from scratch, because it isn't a first-party concept in k8s. All it gives you is the first-party implementation of compare-and-swap for resources. So you have to make a StatefulSet for your replicas, make a ConfigMap to store the leader election data (which is where the CAS ensures atomicity), and write a sidecar container to handle the healthchecks and leader election and dynamic scale-up/down of replicas. You'll probably end up making an operator to do all that for you so that you can deploy a custom resource for your replicas and they're converted to StatefulSets with Pods containing the sidecars, etc automatically.

Of course, for the big popular DBs, someone has probably already written such operators for you.

Re:PostgreSQL 14 on Kubernetes by briffle on Wednesday October 06, 2021 @09:33AM (#28772950)
what happens when you declare that your PG 13 cluster should now be PG 14?

I haven't yet seen good documentation on how these K8s databases handle upgrades. Additionally, they all seem very quick to stream from master to create a new node. That is great, for very, very small db's. But a 1TB db would be royal pain to do that with.

Re:PostgreSQL 14 on Kubernetes by bitwalker on Wednesday October 06, 2021 @01:02PM (#28775470)
I think this is where the operator pattern really shines. By defining a custom resource that contains the cluster configuration, the operator can detect certain types of changes, such as upgrading Postgres to a new major version, and automate that change the same way that you'd do it manually. Of course, if such an operator doesn't already exist, its on you to build it, but with popular databases like Postgres, they are generally already out there in some form. That said, I'm not sure if existing Postgres operators handle your specific example.
Re:PostgreSQL 14 on Kubernetes by funcDropShadow on Wednesday October 06, 2021 @04:11AM (#28770459)
So, the argument to deploy a database (e.g. Postgres) via Kubernetes is, that that some of the problems doing so are now solved?
Re:PostgreSQL 14 on Kubernetes by prpl on Wednesday October 06, 2021 @10:51AM (#28773856)
minio’s DirectCSI might be a solution you are interested in
Re:PostgreSQL 14 on Kubernetes by mr-karan on Wednesday October 06, 2021 @02:11AM (#28769768)
> Stateful services like databases are not really a challenge anymore

I've faced so many issues with the CSI driver which doesn't attach/deattach properly when you modify anything on the Statetfulset.

Additionally, you need to pin your DBs ("statefulset workloads") to certain Nodes based on the AZs since a block storage like an EBS cannot really be attached from Zone 1 to Zone 2 automatically.

Based on these limitations, I dont really see any utility/usefulness in running a DB on K8s. I get all of the other niceties that K8s offers, but here it makes the Ops work more complex than desired.

Re:PostgreSQL 14 on Kubernetes by manigandham on Wednesday October 06, 2021 @06:03AM (#28771160)
This is where complexity comes in. Let Kubernetes abstract the resources for your workloads.

For example, use a storage service (like Rook/openEBS/portworx/etc) that uses node-attached disks to create a data layer. Then you can assign volumes to your individual workloads created and managed by the storage service.

This provides reliable and portable storage for your other services without having to tying them to lower-level details.

Re:PostgreSQL 14 on Kubernetes by nullify88 on Wednesday October 06, 2021 @02:43AM (#28769952)
> Additionally, you need to pin your DBs ("statefulset workloads") to certain Nodes based on the AZs since a block storage like an EBS cannot really be attached from Zone 1 to Zone 2 automatically.

Having Postgres replicate the data across AZs or run Ceph on top of EBS to do it at the storage level are options. The complexity of setup is made simpler by running their respective operators.

Re:PostgreSQL 14 on Kubernetes by arianvanp on Wednesday October 06, 2021 @05:53AM (#28771088)
This is not true. Once your PVC is assigned to a pod the scheduler takes into account the zone information stored in the PVC/PV to make subsequent scheduling decisions if the pod gets restarted. The scheduler will make sure to only start a pod in a place where the PV can get reattached.

There used to be bugs with this years ago but all storage providers now get this right.

Re:PostgreSQL 14 on Kubernetes by FridgeSeal on Wednesday October 06, 2021 @02:27AM (#28769856)
Are you on a cloud provider?

I’ve not had issues with EBS/AWS, and affinity configs meant it was really easy to spread nodes throughout AZ’s.

On Azure though, volume mounts/unmounts were an absolute dumpster fire.

Re:PostgreSQL 14 on Kubernetes by Hamuko on Wednesday October 06, 2021 @03:40AM (#28770272)
>Stateful services like databases are not really a challenge anymore given recent developments in both K8S and database containerization.

People say this but my first reaction to "let's put our DB in the Kubernetes cluster" is still to take two steps backwards and prepare to run in the other direction. It really goes against all of my better instincts (and unfortunately hands-on experiences).