多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
# Geo with external PostgreSQL instances > 原文:[https://docs.gitlab.com/ee/administration/geo/replication/external_database.html](https://docs.gitlab.com/ee/administration/geo/replication/external_database.html) * [**Primary** node](#primary-node) * [Configure the external database to be replicated](#configure-the-external-database-to-be-replicated) * [Leverage your cloud provider’s tools to replicate the primary database](#leverage-your-cloud-providers-tools-to-replicate-the-primary-database) * [Manually configure the primary database for replication](#manually-configure-the-primary-database-for-replication) * [**Secondary** nodes](#secondary-nodes) * [Manually configure the replica database](#manually-configure-the-replica-database) * [Configure **secondary** application nodes to use the external read-replica](#configure-secondary-application-nodes-to-use-the-external-read-replica) * [Configure the tracking database](#configure-the-tracking-database) # Geo with external PostgreSQL instances[](#geo-with-external-postgresql-instances-premium-only "Permalink") 如果您使用的*不是由 Omnibus 管理*的 PostgreSQL 实例,则此文档很重要. 这包括 AWS RDS 之类的云托管实例,或者手动安装和配置的 PostgreSQL 实例. **注意:**我们强烈建议运行 Omnibus 管理的实例,因为它们是积极开发和测试的. 我们的目标是与大多数外部数据库(不受 Omnibus 管理)兼容,但我们不保证兼容性. ## **Primary** node[](#primary-node "Permalink") 1. SSH 到 GitLab **主**应用程序服务器并以 root 用户身份登录: ``` sudo -i ``` 2. 编辑`/etc/gitlab/gitlab.rb`并为您的节点添加一个**唯一的** ID(任意值): ``` # The unique identifier for the Geo node. gitlab_rails['geo_node_name'] = '<node_name_here>' ``` 3. 重新配置**主**节点以使更改生效: ``` gitlab-ctl reconfigure ``` 4. 执行以下命令以将节点定义**为主**节点: ``` gitlab-ctl set-geo-primary-node ``` 该命令将使用您在`/etc/gitlab/gitlab.rb`定义的`external_url` . ### Configure the external database to be replicated[](#configure-the-external-database-to-be-replicated "Permalink") 要设置外部数据库,您可以: * 自己设置流复制(例如,在 AWS RDS 中). * 手动执行 Omnibus 配置,如下所示. #### Leverage your cloud provider’s tools to replicate the primary database[](#leverage-your-cloud-providers-tools-to-replicate-the-primary-database "Permalink") 假设您在使用 RDS 的 AWS EC2 上设置了一个主节点. 现在,您仅可以在其他区域中创建只读副本,复制过程将由 AWS 管理. 确保已根据需要设置了网络 ACL,子网和安全组,以便辅助应用程序节点可以访问数据库. 以下说明详细说明了如何为常见的云提供程序创建只读副本: * Amazon RDS- [创建只读副本](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/USER_ReadRepl.html#USER_ReadRepl.Create) * PostgreSQL 的 Azure 数据库-在 PostgreSQL 的 Azure 数据库中[创建和管理只读副本](https://docs.microsoft.com/en-us/azure/postgresql/howto-read-replicas-portal) 设置只读副本后,您可以跳过以[配置辅助应用程序节点](#configure-secondary-application-nodes-to-use-the-external-read-replica) . #### Manually configure the primary database for replication[](#manually-configure-the-primary-database-for-replication "Permalink") [`geo_primary_role`](https://docs.gitlab.com/omnibus/roles/)通过更改`pg_hba.conf`和`postgresql.conf`来配置要复制的**主**节点数据库. 手动对外部数据库配置进行以下配置更改,并确保稍后重新启动 PostgreSQL 才能使更改生效: ``` ## ## Geo Primary Role ## - pg_hba.conf ## host all all <trusted primary IP>/32 md5 host replication gitlab_replicator <trusted primary IP>/32 md5 host all all <trusted secondary IP>/32 md5 host replication gitlab_replicator <trusted secondary IP>/32 md5 ``` ``` ## ## Geo Primary Role ## - postgresql.conf ## wal_level = hot_standby max_wal_senders = 10 wal_keep_segments = 50 max_replication_slots = 1 # number of secondary instances hot_standby = on ``` ## **Secondary** nodes[](#secondary-nodes "Permalink") ### Manually configure the replica database[](#manually-configure-the-replica-database "Permalink") 手动对外部副本数据库的`pg_hba.conf`和`postgresql.conf`进行以下配置更改,并确保之后重新启动 PostgreSQL 才能使更改生效: ``` ## ## Geo Secondary Role ## - pg_hba.conf ## host all all <trusted secondary IP>/32 md5 host replication gitlab_replicator <trusted secondary IP>/32 md5 host all all <trusted primary IP>/24 md5 ``` ``` ## ## Geo Secondary Role ## - postgresql.conf ## wal_level = hot_standby max_wal_senders = 10 wal_keep_segments = 10 hot_standby = on ``` ### Configure **secondary** application nodes to use the external read-replica[](#configure-secondary-application-nodes-to-use-the-external-read-replica "Permalink") 对于 Omnibus, [`geo_secondary_role`](https://docs.gitlab.com/omnibus/roles/)具有三个主要功能: 1. 配置副本数据库. 2. 配置跟踪数据库. 3. 启用[地理日志光标](index.html#geo-log-cursor) (本节未介绍). 要配置与外部只读副本数据库的连接并启用 Log Cursor,请执行以下操作: 1. SSH 到 GitLab **辅助**应用程序服务器并以 root 用户身份登录: ``` sudo -i ``` 2. 编辑`/etc/gitlab/gitlab.rb`并添加以下内容 ``` ## ## Geo Secondary role ## - configure dependent flags automatically to enable Geo ## roles ['geo_secondary_role'] # note this is shared between both databases, # make sure you define the same password in both gitlab_rails['db_password'] = '<your_password_here>' gitlab_rails['db_username'] = 'gitlab' gitlab_rails['db_host'] = '<database_read_replica_host>' # Disable the bundled Omnibus PostgreSQL, since we are # using an external PostgreSQL postgresql['enable'] = false ``` 3. 保存文件并[重新配置 GitLab](../../restart_gitlab.html#omnibus-gitlab-reconfigure) ### Configure the tracking database[](#configure-the-tracking-database "Permalink") **辅助**节点使用单独的 PostgreSQL 安装作为跟踪数据库,以跟踪复制状态并自动从潜在的复制问题中恢复. 设置了`roles ['geo_secondary_role']` Omnibus 会自动配置跟踪数据库. 如果要在 Omnibus 外部运行此数据库,请按照以下说明进行操作. 如果您将云托管服务用于跟踪数据库,则可能需要向跟踪数据库用户授予其他角色(默认情况下,这是`gitlab_geo` ): * Amazon RDS 需要[`rds_superuser`](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/Appendix.PostgreSQL.CommonDBATasks.html#Appendix.PostgreSQL.CommonDBATasks.Roles)角色. * PostgreSQL 的 Azure 数据库需要[`azure_pg_admin`](https://docs.microsoft.com/en-us/azure/postgresql/howto-create-users#how-to-create-additional-admin-users-in-azure-database-for-postgresql)角色. 跟踪数据库需要与**辅助**副本数据库建立[FDW](https://s0www0postgresql0org.icopy.site/docs/11/postgres-fdw.html)连接以提高性能. 如果您准备好将外部数据库用作跟踪数据库,请按照以下说明使用它: **注意:**如果您要将 AWS RDS 用作跟踪数据库,请确保其有权访问辅助数据库. 不幸的是,仅分配相同的安全组是不够的,因为出站规则不适用于 RDS PostgreSQL 数据库. 因此,您需要将入站规则显式添加到只读副本的安全组,以允许来自跟踪数据库的端口 5432 上的所有 TCP 通信. 1. 通过手动更改与跟踪数据库关联的`pg_hba.conf` ,确保辅助节点可以与跟踪数据库通信. 请记住,之后要重新启动 PostgreSQL 才能使更改生效: ``` ## ## Geo Tracking Database Role ## - pg_hba.conf ## host all all <trusted tracking IP>/32 md5 host all all <trusted secondary IP>/32 md5 ``` 2. SSH 到 GitLab **辅助**服务器并以 root 用户身份登录: ``` sudo -i ``` 3. 使用 PostgreSQL 实例的机器的连接参数和凭据编辑`/etc/gitlab/gitlab.rb` : ``` geo_secondary['db_username'] = 'gitlab_geo' geo_secondary['db_password'] = '<your_password_here>' geo_secondary['db_host'] = '<tracking_database_host>' geo_secondary['db_port'] = <tracking_database_port> # change to the correct port geo_secondary['db_fdw'] = true # enable FDW geo_postgresql['enable'] = false # don't use internal managed instance ``` 4. 保存文件并[重新配置 GitLab](../../restart_gitlab.html#omnibus-gitlab-reconfigure) 5. 运行跟踪数据库迁移: ``` gitlab-rake geo:db:create gitlab-rake geo:db:migrate ``` 6. 配置[PostgreSQL FDW](https://s0www0postgresql0org.icopy.site/docs/11/postgres-fdw.html)连接和凭据: 将下面的脚本保存在一个文件中,例如. `/tmp/geo_fdw.sh`并修改连接参数以匹配您的环境. 执行它以建立 FDW 连接. ``` #!/bin/bash # Secondary Database connection params: DB_HOST="<public_ip_or_vpc_private_ip>" DB_NAME="gitlabhq_production" DB_USER="gitlab" DB_PASS="<your_password_here>" DB_PORT="5432" # Tracking Database connection params: GEO_DB_HOST="<public_ip_or_vpc_private_ip>" GEO_DB_NAME="gitlabhq_geo_production" GEO_DB_USER="gitlab_geo" GEO_DB_PORT="5432" query_exec () { gitlab-psql -h $GEO_DB_HOST -U $GEO_DB_USER -d $GEO_DB_NAME -p $GEO_DB_PORT -c "${1}" } query_exec "CREATE EXTENSION postgres_fdw;" query_exec "CREATE SERVER gitlab_secondary FOREIGN DATA WRAPPER postgres_fdw OPTIONS (host '${DB_HOST}', dbname '${DB_NAME}', port '${DB_PORT}');" query_exec "CREATE USER MAPPING FOR ${GEO_DB_USER} SERVER gitlab_secondary OPTIONS (user '${DB_USER}', password '${DB_PASS}');" query_exec "CREATE SCHEMA gitlab_secondary;" query_exec "GRANT USAGE ON FOREIGN SERVER gitlab_secondary TO ${GEO_DB_USER};" ``` **注意:**上面的脚本模板使用`gitlab-psql`因为它打算从 Geo 机器上执行,但是您可以将其更改为`psql`并从任何有权访问数据库的机器上运行. 我们还建议将`psql`用于 AWS RDS. 7. 保存文件并[重新启动 GitLab](../../restart_gitlab.html#omnibus-gitlab-restart) 8. Populate the FDW tables: ``` gitlab-rake geo:db:refresh_foreign_tables ```