Geo with external PostgreSQL instances

原文:https://docs.gitlab.com/ee/administration/geo/replication/external_database.html

Geo with external PostgreSQL instances

如果您使用的不是由 Omnibus 管理的 PostgreSQL 实例,则此文档很重要. 这包括 AWS RDS 之类的云托管实例,或者手动安装和配置的 PostgreSQL 实例.

注意:我们强烈建议运行 Omnibus 管理的实例,因为它们是积极开发和测试的. 我们的目标是与大多数外部数据库(不受 Omnibus 管理)兼容,但我们不保证兼容性.

Primary node

  1. SSH 到 GitLab 应用程序服务器并以 root 用户身份登录:

    sudo -i 
    1. 编辑/etc/gitlab/gitlab.rb并为您的节点添加一个唯一的 ID(任意值):
    # The unique identifier for the Geo node.
    gitlab_rails['geo_node_name'] = '<node_name_here>' 
    1. 重新配置节点以使更改生效:
    gitlab-ctl reconfigure 
    1. 执行以下命令以将节点定义为主节点:
    gitlab-ctl set-geo-primary-node 

    该命令将使用您在/etc/gitlab/gitlab.rb定义的external_url .

Configure the external database to be replicated

要设置外部数据库,您可以:

  • 自己设置流复制(例如,在 AWS RDS 中).
  • 手动执行 Omnibus 配置,如下所示.

Leverage your cloud provider’s tools to replicate the primary database

假设您在使用 RDS 的 AWS EC2 上设置了一个主节点. 现在,您仅可以在其他区域中创建只读副本,复制过程将由 AWS 管理. 确保已根据需要设置了网络 ACL,子网和安全组,以便辅助应用程序节点可以访问数据库.

以下说明详细说明了如何为常见的云提供程序创建只读副本:

设置只读副本后,您可以跳过以配置辅助应用程序节点 .

Manually configure the primary database for replication

geo_primary_role通过更改pg_hba.confpostgresql.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

Manually configure the replica database

手动对外部副本数据库的pg_hba.confpostgresql.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

对于 Omnibus, geo_secondary_role具有三个主要功能:

  1. 配置副本数据库.
  2. 配置跟踪数据库.
  3. 启用地理日志光标 (本节未介绍).

要配置与外部只读副本数据库的连接并启用 Log Cursor,请执行以下操作:

  1. SSH 到 GitLab 辅助应用程序服务器并以 root 用户身份登录:

    sudo -i 
    1. 编辑/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 
    1. 保存文件并重新配置 GitLab

Configure the tracking database

辅助节点使用单独的 PostgreSQL 安装作为跟踪数据库,以跟踪复制状态并自动从潜在的复制问题中恢复. 设置了roles ['geo_secondary_role'] Omnibus 会自动配置跟踪数据库. 如果要在 Omnibus 外部运行此数据库,请按照以下说明进行操作.

如果您将云托管服务用于跟踪数据库,则可能需要向跟踪数据库用户授予其他角色(默认情况下,这是gitlab_geo ):

跟踪数据库需要与辅助副本数据库建立FDW连接以提高性能.

如果您准备好将外部数据库用作跟踪数据库,请按照以下说明使用它:

注意:如果您要将 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 
    1. SSH 到 GitLab 辅助服务器并以 root 用户身份登录:
    sudo -i 
    1. 使用 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 
    1. 保存文件并重新配置 GitLab
  2. 运行跟踪数据库迁移:

    gitlab-rake geo:db:create
    gitlab-rake geo:db:migrate 
    1. 配置PostgreSQL FDW连接和凭据:

    将下面的脚本保存在一个文件中,例如. /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.

  3. 保存文件并重新启动 GitLab

  4. Populate the FDW tables:

    gitlab-rake geo:db:refresh_foreign_tables 
Copyright © 温玉 2021 | 浙ICP备2020032454号 all right reserved,powered by Gitbook该文件修订时间: 2021-03-27 13:48:25

results matching ""

    No results matching ""