490.md 10.7 KB
Newer Older
Lab机器人's avatar
readme  
Lab机器人 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
# 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 
    ```