聊聊基礎(chǔ)設(shè)施即代碼(IAC),Zalando Postgres Operator
Postgres Operator 在由 Patroni 提供支持的 Kubernetes (K8s) 上提供易于運行的高可用性 PostgreSQL 集群。它僅通過 Postgres 清單 (CRD) 進行配置,以輕松集成到自動化 CI/CD 管道中,而無需直接訪問 Kubernetes API,從而促進基礎(chǔ)設(shè)施即代碼(infrastructure as code)而不是手動操作。
- Patroni: https://github.com/zalando/patroni
- PostgreSQL: https://www.postgresql.org/
Operator 功能
- Postgres 集群變化的滾動更新,包括快速的小版本更新
- 無需重新啟動 pod 即可調(diào)整實時卷大小(AWS EBS、PVC)
- 使用 PGBouncer 進行數(shù)據(jù)庫連接池
- 支持 PG13 的快速升級。支持所有集群全局升級
- 恢復和克隆 Postgres 集群(包括主要版本升級)
- 此外,可以配置到 S3 存儲桶的邏輯備份
- 來自 S3 WAL 存檔的備用集群
- 可針對非云環(huán)境進行配置
- K8s 上的基本憑證和用戶管理,簡化應用程序部署
- 支持自定義 TLS 證書
- 創(chuàng)建和編輯 Postgres 集群清單的 UI
- 在 Amazon AWS、Google Cloud、OpenShift 和 Kind 本地運行良好
- 支持 AWS EBS gp2 到 gp3 遷移,支持 iops 和吞吐量配置
PostgreSQL 功能
- 支持 PostgreSQL 14,從 9.6+ 開始
- 通過 Patroni 流式復制集群
- 通過 Spilo 使用 pg_basebackup / WAL-E 進行時間點恢復(Point-In-Time-Recovery)
Spilo: https://github.com/zalando/spilo
pg_basebackup: https://www.postgresql.org/docs/11/app-pgbasebackup.html
WAL-E: https://github.com/wal-e/wal-e
- 預加載庫:bg_mon, pg_stat_statements, pgextwlist, pg_auth_mon
bg_mon: https://github.com/CyberDem0n/bg_mon
pg_stat_statements: https://www.postgresql.org/docs/14/pgstatstatements.html
pgextwlist: https://github.com/dimitri/pgextwlist
pg_auth_mon: https://github.com/RafiaSabih/pg_auth_mon
- 包括。流行的 Postgres 擴展,例如 decoderbufs, hypopg, pg_cron, pg_partman, pg_stat_kcache, pgq, plpgsql_check, postgis, set_user 和 timescaledb
decoderbufs: https://github.com/debezium/postgres-decoderbufs
hypopg: https://github.com/HypoPG/hypopg
pg_cron: https://github.com/citusdata/pg_cron
pg_partman: https://github.com/pgpartman/pg_partman
pg_stat_kcache: https://github.com/powa-team/pg_stat_kcache
pgq: https://github.com/pgq/pgq
plpgsql_check: https://github.com/okbob/plpgsql_check
postgis: https://postgis.net/
set_user: https://github.com/pgaudit/set_user
timescaledb: https://github.com/timescale/timescaledb
Postgres Operator 在 Zalando 開發(fā)并已在生產(chǎn)中使用了三年多。
使用 Spilo 12 或更舊的鏡像
如果您已經(jīng)在舊版本中使用帶有 Spilo 12 Docker 鏡像的 Postgres operator,則需要注意備份路徑的更改。我們將主版本引入備份路徑,以平滑現(xiàn)在支持的主版本升級。
- 主版本升級: https://github.com/zalando/postgres-operator/blob/master/docs/administrator.md#minor-and-major-version-upgrade
新的 operator 配置可以設(shè)置兼容性標志 enable_spilo_wal_path_compat 以使 Spilo 在當前路徑中查找 wal 段,但也可以在舊格式路徑中查找。這是以潛在的性能成本為代價的,應該在幾天后禁用。
最新的 Spilo 鏡像是:registry.opensource.zalan.do/acid/spilo-14:2.1-p4
最舊的 Spilo 鏡像是:registry.opensource.zalan.do/acid/spilo-12:1.6-p5
入門
為了快速獲得第一印象,請按照入門教程的說明進行操作。
- 入門教程: https://github.com/zalando/postgres-operator/blob/master/docs/quickstart.md
支持的 Postgres 和應用程序設(shè)置
概念
Postgres operator 在 Kubernetes (K8s) 上管理 PostgreSQL 集群:
- operator 監(jiān)視 PostgreSQL 集群清單的添加、更新和刪除,并相應地更改正在運行的集群。例如,當用戶提交新清單時,operator 會獲取該清單并生成一個新的 Postgres 集群以及所有必要的實體,例如 K8s StatefulSets 和 Postgres 角色。有關(guān)清單可能包含的設(shè)置,請參閱此 Postgres 集群清單。
operator: https://coreos.com/blog/introducing-operators.html
Postgres 集群清單: https://github.com/zalando/postgres-operator/blob/master/manifests/complete-postgres-manifest.yaml
- operator 還監(jiān)視其自身配置的更新,并在必要時更改正在運行的 Postgres 集群。例如,如果 pod 中的 Docker 鏡像發(fā)生變化,則 operator 執(zhí)行滾動更新,這意味著它會與新的 Docker 鏡像一一重新生成每個托管 StatefulSet 的 pod。
自身配置: https://github.com/zalando/postgres-operator/blob/master/manifests/configmap.yaml
- 最后,operator 定期將每個 Postgres 集群的實際狀態(tài)與集群清單中定義的所需狀態(tài)同步。
- operator 的目標是解放雙手,因為配置只能通過清單進行。這可以輕松集成到自動化部署管道中,而無需直接訪問 K8s。
作用域
Postgres Operator 的 scope 是提供、修改配置和清理使用 Patroni 的 Postgres 集群,基本上是為了在 K8s 上輕松方便地運行基于 Patroni 的集群。配置和修改包括一側(cè)的 K8s 資源,但也包括集群啟動并運行后的數(shù)據(jù)庫和角色配置。我們盡量把工作留給 K8s 和 Patroni,尤其是集群引導和高可用性。不過,operator 需要參與一些總體編排,比如滾動更新以改善用戶體驗。
在當前狀態(tài)下,監(jiān)控或調(diào)整 Postgres 不在 operator 的作用域內(nèi)。但是,通過全局可配置的 sidecar,我們提供了足夠的靈活性來補充其他工具,如 ZMON、Prometheus 或更多 Postgres 特定選項。
- ZMON: https://opensource.zalando.com/zmon/
- Prometheus: https://prometheus.io/
所涉實體概述
這是一張圖,它總結(jié)了 operator 在提交新的 Postgres cluster CRD 時將創(chuàng)建的內(nèi)容:
如果沒有對單個集群 pod 內(nèi)部的概述,這張圖是不完整的,所以讓我們放大一下:
這兩張圖應該可以幫助您了解 operator 提供的功能類型的基礎(chǔ)知識。
狀態(tài)
該項目目前正在積極開發(fā)中。然而,Zalando 已經(jīng)在內(nèi)部使用它,以便在 K8s 上運行大量 Postgres 集群,用于 staging 環(huán)境和越來越多的 production 集群。在這種環(huán)境中,operator 被部署到多個 K8s 集群中,用戶通過我們的 CI/CD 基礎(chǔ)設(shè)施部署清單或依靠精簡的用戶界面來創(chuàng)建清單。