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