Frequently Asked Questions (FAQ)

Running PostgreSQL in Kubernetes

EveryoneknowsthatstatefulworkloadslikePostgreSQLcannotruninKubernetes.Whydoyousaythecontrary?

An independent research survey commissioned by the Data on KubernetesCommunity in September 2021revealed that half of the respondents run most of their productionworkloads on Kubernetes. 90% of them believe that Kubernetes is readyfor stateful workloads, and 70% of them run databases in production.Databases like Postgres. However, according to them, significantchallenges remain, such as the knowledge gap (Kubernetes and CloudNative, in general, have a steep learning curve) and the quality ofKubernetes operators. The latter is the reason why we believe that anoperator like CloudNativePG highly contributes to the successof your project.

For database fanatics like us, a real game-changer has been theintroduction of the support for local persistent volumes in

CloudNativePGisbuiltonimmutableapplicationcontainers.Whatdoesitmean?

According to the microservice architectural pattern, a container isdesigned to run a single application or process. As a result, suchcontainer images are built to run the main application as thesingle entry point (the so-called PID 1 process).

In Kubernetes terms, the application is referred to as workload.Workloads can be stateless like a web application server or stateful like adatabase. Mapping this concept to PostgreSQL, an immutable applicationcontainer is a single “postgres” process that is running andtied to a single and specific version - the one in the immutablecontainer image.

No other processes such as SSH or systemd, or syslog are allowed.

Immutable Application Containers are in contrast with Mutable SystemContainers, which are still a very common way to interpret and usecontainers.

Immutable means that a container won’t be modified during its life: noupdates, no patches, no configuration changes. If you must update theapplication code or apply a patch, you build a new image and redeployit. Immutability makes deployments safer and more repeatable.

For more information, please refer to *”Why EDB chose immutable application containers”* .

WhatdoesCloudNativemean?

The Cloud Native Computing Foundation defines the term *Cloud Native* “.However, since the start of the Cloud Native PostgreSQL/CloudNativePG operatorat 2ndQuadrant, the development team has been interpreting Cloud Nativeas three main concepts:

  1. An existing, healthy, genuine, and prosperous DevOps culture, founded on people, as well as principles and processes, which enables teams and organizations (as teams of teams) to continuously change so to innovate and accelerate the delivery of outcomes and produce value for the business in safer, more efficient, and more engaging ways2. A microservice architecture that is based on Immutable Application Containers3. A way to manage and orchestrate these containers, such as Kubernetes

Currently, the standard de facto for container orchestration isKubernetes, which automates the deployment, administration andscalability of Cloud Native Applications.

Another definition of Cloud Native that resonates with us is the onedefined by Ibryam and Huß in *”Kubernetes Patterns”, published by O’Reilly* :

Principles, Patterns, Tools to automate containerized microservices at scale

WhyshouldIuseanoperatorinsteadofrunningPostgreSQLasacontainer?

The most basic approach to running PostgreSQL in Kubernetes is to have apod, which is the smallest unit of deployment in Kubernetes, running aPostgres container with no replica. The volume hosting the Postgres datadirectory is mounted on the pod, and it usually resides on networkstorage. In this case, Kubernetes restarts the pod in case of aproblem or moves it to another Kubernetes node.

The most sophisticated approach is to run PostgreSQL using an operator.An operator is an extension of the Kubernetes controller and defines howa complex application works in business continuity contexts. Theoperator pattern is currently state of the art in Kubernetes forthis purpose. An operator simulates the work of a human operator in anautomated and programmatic way.

Postgres is a complex application, and an operator not only needs todeploy a cluster (the first step), but also properly react afterunexpected events. The typical example is that of a failover.

An operator relies on Kubernetes for capabilities like self-healing,scalability, replication, high availability, backup, recovery, updates,access, resource control, storage management, and so on. It alsofacilitates the integration of a PostgreSQL cluster in the logmanagement and monitoring infrastructure.

CloudNativePG enables the definition of the desired state of aPostgreSQL cluster via declarative configuration. Kubernetescontinuously makes sure that the current state of the infrastructurematches the desired one through reconciliation loops initiated by theKubernetes controller. If the desired state and the actual state don’tmatch, reconciliation loops trigger self-healing procedures. That’swhere an operator like CloudNativePG comes into play.

YousaythatCloudNativePGisafullydeclarativeoperator.Whatdoyoumeanbythat?

The easiest way is to explain declarative configuration through anexample that highlights the differences with imperative configuration.In an imperative context, the state is defined as a series of tasks tobe executed in sequence. So, we can get a three-node PostgreSQL clusterby creating the first instance, configuring the replication, cloning asecond instance, and the third one.

In a declarative approach, the state of a system is defined usingconfiguration, namely: there’s a PostgreSQL 13 cluster with two replicas.This approach highly simplifies change management operations, and whenthese are stored in source control systems like Git, it enables theInfrastructure as Code capability. And Kubernetes takes it farther thandeployment, as it makes sure that our request is fulfilled at any time.

WhataretherequiredskillstorunPostgreSQLonKubernetes?

Running PostgreSQL on Kubernetes requires both PostgreSQL and Kubernetesskills in your DevOps team. The best experience is when databaseadministrators familiarize themselves with Kubernetes core conceptsand are able to interact with Kubernetes administrators.

Our advice is for everyone that wants to fully exploit Cloud NativePostgreSQL to acquire the “Certified Kubernetes Administrator (CKA)”status from the CNCF certification program.