|
| 1 | +# Architecture |
| 2 | + |
| 3 | +[PostgreSQL](https://www.postgresql.org/) is one of the most popular open source database. The “[Charmed PostgreSQL](https://charmhub.io/postgresql)” is a Juju-based operator to deploy and support PostgreSQL from [day 0 to day 2](https://codilime.com/blog/day-0-day-1-day-2-the-software-lifecycle-in-the-cloud-age/), it is based on the [PostgreSQL Community Edition](https://www.postgresql.org/community/) using the [Patroni](https://github.com/zalando/patroni) to manage PostgreSQL cluster based on [PostgreSQL synchronous replication](https://patroni.readthedocs.io/en/latest/replication_modes.html#postgresql-synchronous-replication). |
| 4 | + |
| 5 | + |
| 6 | + |
| 7 | +<a name="hld"></a> |
| 8 | +## HLD (High Level Design) |
| 9 | + |
| 10 | +The charm design leverages on the SNAP “[charmed-postgresql](https://snapcraft.io/charmed-postgresql)” which is deployed by Juju on the specified VM/MAAS/bare-metal machine based on Ubuntu Jammy/22.04. SNAP allows to run PostgreSQL service(s) in a secure and isolated environment ([strict confinement](https://ubuntu.com/blog/demystifying-snap-confinement)). The installed SNAP: |
| 11 | +``` |
| 12 | +> juju ssh postgresql/0 snap list charmed-postgresql |
| 13 | +Name Version Rev Tracking Publisher Notes |
| 14 | +charmed-postgresql 14.9 70 latest/stable dataplatformbot held |
| 15 | +``` |
| 16 | + |
| 17 | +The SNAP ships the following components: |
| 18 | + |
| 19 | +* PostgreSQL (based on Ubuntu APT package "[postgresql](https://packages.ubuntu.com/jammy/postgresql)") |
| 20 | +* PgBouncer (based on Canonical [backport](https://launchpad.net/~data-platform/+archive/ubuntu/pgbouncer)) |
| 21 | +* Patroni (based on Canonical [backport](https://launchpad.net/~data-platform/+archive/ubuntu/patroni)) |
| 22 | +* pgBackRest (based on Canonical [backport](https://launchpad.net/~data-platform/+archive/ubuntu/pgbackrest)) |
| 23 | +* Prometheus PostgreSQL Exporter (based on Canonical [backport](https://launchpad.net/~data-platform/+archive/ubuntu/postgres-exporter)) |
| 24 | +* Prometheus PgBouncer Exporter (based on Canonical [backport](https://launchpad.net/~data-platform/+archive/ubuntu/pgbouncer-exporter)) |
| 25 | +* Prometheus Grafana dashboards and Loki alert rules are part of the charm revision (and missing in SNAP). |
| 26 | + |
| 27 | +Versions of all the components above are carefully chosen to fit functionality of each other. |
| 28 | + |
| 29 | +The Charmed PostgreSQL unit consisting of a several services which are enabled/activated accordingly to the setup: |
| 30 | + |
| 31 | +``` |
| 32 | +> juju ssh postgresql/0 snap services charmed-postgresql |
| 33 | +Service Startup Current Notes |
| 34 | +charmed-postgresql.patroni enabled active - |
| 35 | +charmed-postgresql.pgbackrest-service enabled active - |
| 36 | +charmed-postgresql.prometheus-postgres-exporter enabled active - |
| 37 | +
|
| 38 | +``` |
| 39 | + |
| 40 | +The `patroni` snap service is a main PostgreSQL instance which is normally up and running right after the charm deployment. |
| 41 | + |
| 42 | +The `pgbackrest` snap service is a backup framework for PostgreSQL. It is disabled if [Backup](/t/9683) is not configured. |
| 43 | + |
| 44 | +The `prometheus-postgres-exporter` service is activated after the relation with [COS Monitoring](/t/10600) only. |
| 45 | + |
| 46 | +> **:information_source: Note:** it is possible to star/stop/restart snap services manually but it is NOT recommended to avoid a split brain with a charm state machine! Do it with a caution!!! |
| 47 | +
|
| 48 | +> **:warning: Important:** all snap resources must be executed under the special user `snapd_daemon` only! |
| 49 | +
|
| 50 | +The snap "charmed-postgresql" also ships list of tools used by charm: |
| 51 | +* `charmed-postgresql.psql` (alias `psq`) - is PostgreSQL interactive terminal. |
| 52 | +* `charmed-postgresql.patronictl` - a tool to monitor and manage Patroni. |
| 53 | +* `charmed-postgresql.pgbackrest` - a tool to backup/restore PostgreSQL DB. |
| 54 | + |
| 55 | +<a name="integrations"></a> |
| 56 | +## Integrations |
| 57 | + |
| 58 | +### PgBouncer |
| 59 | + |
| 60 | +[PgBouncer](http://www.pgbouncer.org/) is a lightweight connection pooler for PostgreSQL that provides transparent routing between your application and back-end PostgreSQL Servers. The "[PgBouncer](https://charmhub.io/pgbouncer)" is an independent charm "Charmed PostgreSQL" can be related with. |
| 61 | + |
| 62 | +### TLS Certificates Operator |
| 63 | + |
| 64 | +[TLS Certificates](https://charmhub.io/tls-certificates-operator) charm responsible for distributing certificates through relationship. Certificates are provided by the operator through Juju configs. For the playground deployments, the [self-signed operator](https://charmhub.io/self-signed-certificates) is available as well. |
| 65 | + |
| 66 | +### S3 Integrator |
| 67 | + |
| 68 | +[S3 Integrator](https://charmhub.io/s3-integrator) is an integrator charm for providing S3 credentials to Charmed PostgreSQL which seek to access shared S3 data. Store the credentials centrally in the integrator charm and relate consumer charms as needed. |
| 69 | + |
| 70 | +### Data Integrator |
| 71 | + |
| 72 | +[Data Integrator](https://charmhub.io/data-integrator) charm is a solution to request DB credentials for non-native Juju applications. Not all applications implement a data_interfaces relation but allow setting credentials via config. Also, some of the applications are run outside of juju. This integrator charm allows receiving credentials which can be passed into application config directly without implementing juju-native relation. |
| 73 | + |
| 74 | +### PostgreSQL Test App |
| 75 | + |
| 76 | +The charm "[PostgreSQL Test App](https://charmhub.io/postgresql-test-app)" is a Canonical test application to validate the charm installation / functionality and perform the basic performance tests. |
| 77 | + |
| 78 | +### Grafana |
| 79 | + |
| 80 | +Grafana is an open-source visualization tools that allows to query, visualize, alert on, and visualize metrics from mixed datasources in configurable dashboards for observability. This charms is shipped with its own Grafana dashboard and supports integration with the [Grafana Operator](https://charmhub.io/grafana-k8s) to simplify observability. Please follow [COS Monitoring](/t/10600) setup. |
| 81 | + |
| 82 | +### Loki |
| 83 | + |
| 84 | +Loki is an open-source fully-featured logging system. This charms is shipped with support for the [Loki Operator](https://charmhub.io/loki-k8s) to collect the generated logs. Please follow [COS Monitoring](/t/10600) setup. |
| 85 | + |
| 86 | +### Prometheus |
| 87 | + |
| 88 | +Prometheus is an open-source systems monitoring and alerting toolkit with a dimensional data model, flexible query language, efficient time series database and modern alerting approach. This charm is shipped with a Prometheus exporters, alerts and support for integrating with the [Prometheus Operator](https://charmhub.io/prometheus-k8s) to automatically scrape the targets. Please follow [COS Monitoring](/t/10600) setup. |
| 89 | + |
| 90 | +<a name="lld"></a> |
| 91 | +## LLD (Low Level Design) |
| 92 | + |
| 93 | +Please check the charm state machines displayed on [workflow diagrams](/t/9305). The low-level logic is mostly common for both VM and K8s charms. |
| 94 | + |
| 95 | +<!--- TODO: Describe all possible installations? Cross-model/controller? ---> |
| 96 | + |
| 97 | +### Juju Events |
| 98 | + |
| 99 | +Accordingly to the [Juju SDK](https://juju.is/docs/sdk/event): “an event is a data structure that encapsulates part of the execution context of a charm”. |
| 100 | + |
| 101 | +For this charm, the following events are observed: |
| 102 | + |
| 103 | +1. [on_install](https://juju.is/docs/sdk/install-event): install the snap "charmed-postgresql" and perform basic preparations to bootstrap the cluster on the first leader (or join the already configured cluster). |
| 104 | +2. [leader-elected](https://juju.is/docs/sdk/leader-elected-event): generate all the secrets to bootstrap the cluster. |
| 105 | +3. [leader-settings-changed](https://juju.is/docs/sdk/leader-settings-changed-event): Handle the leader settings changed event. |
| 106 | +4. [start](https://juju.is/docs/sdk/start-event): Init/setting up the cluster node. |
| 107 | +5. [config_changed](https://juju.is/docs/sdk/config-changed-event): usually fired in response to a configuration change using the GUI or CLI. Create and set default cluster and cluster-set names in the peer relation databag (on the leader only). |
| 108 | +6. [update-status](https://juju.is/docs/sdk/update-status-event): Takes care of workload health checks. |
| 109 | +<!--- 7. database_storage_detaching: TODO: ops? event? |
| 110 | +8. TODO: any other events? |
| 111 | +---> |
| 112 | + |
| 113 | +### Charm Code Overview |
| 114 | + |
| 115 | +The "[src/charm.py](https://github.com/canonical/postgresql-operator/blob/main/src/charm.py)" is the default entry point for a charm and has the `PostgresqlOperatorCharm` Python class which inherits from CharmBase. |
| 116 | + |
| 117 | +CharmBase is the base class from which all Charms are formed, defined by [Ops](https://juju.is/docs/sdk/ops) (Python framework for developing charms). See more information in [Charm](https://juju.is/docs/sdk/constructs#heading--charm). |
| 118 | + |
| 119 | +The `__init__` method guarantees that the charm observes all events relevant to its operation and handles them. |
| 120 | + |
| 121 | +The VM and K8s charm flavors shares the codebase via [charm libraries](https://juju.is/docs/sdk/libraries) in [lib/charms/postgresql_k8s/v0/](https://github.com/canonical/postgresql-k8s-operator/blob/main/lib/charms/postgresql_k8s/v0/postgresql.py) (of K8s flavor of the charm!): |
| 122 | +``` |
| 123 | +> charmcraft list-lib postgresql-k8s |
| 124 | +Library name API Patch |
| 125 | +postgresql 0 12 |
| 126 | +postgresql_tls 0 7 |
| 127 | +``` |
0 commit comments