第 3 章 从嵌入式 PostgreSQL 10 数据库迁移到外部 PostgreSQL 10 数据库
-
在将
system.appSpec.replicas
扩展到1
之前,数据库应升级到受支持的版本,当前为 PostgreSQL 13。请参阅 Red Hat 3scale API Management 支持的配置 - 本文档关于从嵌入式 PostgreSQL 10 数据库迁移到外部 PostgreSQL 10 数据库。要从外部 PostgreSQL 10 数据库升级到外部 PostgreSQL 13 数据库,您必须遵循官方 PostgreSQL 文档。
- 免责声明:此处包含的外部网络链接仅为方便用户而提供。红帽没有审阅链接的内容,并不对其内容负责。包含任何指向外部网站的链接并不表示红帽认可该网站或其实体、产品或服务。您同意红帽对因您使用(或依赖)外部网站或内容而导致的任何损失或费用不承担任何责任。
在同一 DB 版本中应该发生从嵌入式 PostgreSQL 数据库迁移到和外部 PostgreSQL 数据库的过程。在此迁移指南中,它应该是 PostgreSQL 10。您应该在生产环境中使用外部数据库。
如果您使用 PostgreSQL 作为 system-database
,请使用 3scale 安装的外部数据库 支持版本。
- 这些步骤是常规准则。具体步骤可能会因您的操作系统、PostgreSQL 版本和数据库的特定要求而异。
- 在升级前,仔细阅读 PostgreSQL 文档 和发行注记。
- 在将此步骤应用到生产部署之前,在非生产环境中测试此步骤。
- 此过程会破坏服务的调配,直到过程完成为止。因为这个过程需要涉及到系统中断,请确保计划有一个维护窗口进行。
流程
使用 APIManager 客户资源(CR)缩减
system-app
DeploymentConfig (DC):apiVersion: apps.3scale.net/v1alpha1 kind: APIManager metadata: name: <apimanager_sample> spec: system: appSpec: replicas: 0 wildcardDomain: <example.com>
验证 pod 是否已缩减:
$ oc get deploymentconfig system-app -o jsonpath='{.status.availableReplicas}{"\n"}' 0
-
等待所有 3scale pod 的状态为
Terminated
,然后继续 PostgreSQL 迁移。
-
等待所有 3scale pod 的状态为
备份现有的 PostgreSQL 数据库,包括所有数据、配置和用户帐户:
$ DB_USER=$(oc get secret system-database -o jsonpath="{.data.DB_USER}" | base64 --decode) $ DATABASE_NAME=$(oc get secret system-database -o jsonpath="{.data.URL}" | base64 --decode | cut -d '/' -f4)
重要不要管道到
stdout
。二进制文件已损坏。带有自定义格式的转储:
$ oc rsh $(oc get pods -l 'deploymentConfig=system-postgresql' -o json | jq -r '.items[0].metadata.name') bash -c "pg_dump -U $DB_USER -F c $DATABASE_NAME -f /tmp/<backupfilename>.backup"
下载备份:
$ oc cp $(oc get pods -l 'deploymentConfig=system-postgresql' -o json | jq -r '.items[0].metadata.name'):/tmp/<backupfilename>.backup <backupfilename>.backup
- 在目标外部系统中安装部署在 3scale 上的同一 PostgreSQL 10 版本。按照安装说明,从 PostgreSQL 网站下载安装软件包。
- 复制并恢复您现有的 PostgreSQL 数据库的备份,包括所有数据、配置和用户帐户到目标外部系统。
在 PostgreSQL 中创建新数据库:
$ createdb -U <username> <databasename>
将备份文件中的数据导入到新的 PostgreSQL 数据库。
使用自定义格式恢复:
$ pg_restore [--host <databasehostname>] -U <username> -d <databasename> --verbose -F c <backupfilename>.backup
通过连接到数据库并运行查询来验证数据是否已成功导入到新的 PostgreSQL 数据库:
postgresql://<username>:<password>@<databasehostname>/<databasename>
更新
system-database
secret:$ oc apply -f - <<EOF --- apiVersion: v1 kind: Secret metadata: name: system-database stringData: DB_PASSWORD: <password> DB_USER: <username> URL: "postgresql://<username>:<password>@<databasehostname>:<databaseport>/<databasename>" type: Opaque EOF
更新 APImanager CR,以启用外部数据库并扩展系统:
$ oc patch apimanager <apimanager_sample> --type=merge --patch '{"spec": {"system": {"database": null, "appSpec": {"replicas": 1}}, "externalComponents": {"system": {"database": true}}}}'
删除本地 postgresql 部署:
$ oc delete service system-postgresql $ oc delete deploymentconfig system-postgresql $ oc delete pvc postgresql-data
验证 pod 是否已扩展:
$ oc wait --for=condition=available apimanager/<apimanager_sample> --timeout=-1s