极狐 GitLab

与捆绑的 PgBouncer 服务一起工作

Tier: 基础版, 专业版, 旗舰版

Offering: 私有化部署

PgBouncer 捆绑在 `gitlab-jh` 软件包中,但是是免费可用的。如果需要技术支持,您需要一个[专业版订阅](https://gitlab.cn/pricing/)。

PgBouncer 用于在故障转移情况下无缝迁移数据库连接到服务器。此外,它可以在非容错设置中用于连接池化,加快响应时间并减少资源使用。

极狐GitLab 专业版包含了一个可通过 /etc/gitlab/gitlab.rb 管理的 PgBouncer 捆绑版本。

PgBouncer 作为容错极狐GitLab 安装的一部分#

此内容已移至新位置

PgBouncer 作为非容错极狐GitLab 安装的一部分#

  1. 使用命令 gitlab-ctl pg-password-md5 pgbouncer 生成 PGBOUNCER_USER_PASSWORD_HASH

  2. 使用命令 gitlab-ctl pg-password-md5 gitlab 生成 SQL_USER_PASSWORD_HASH。稍后输入明文 SQL_USER_PASSWORD。

  3. 在您的数据库节点上,确保在 /etc/gitlab/gitlab.rb 中设置以下内容

    ruby
    postgresql['pgbouncer_user_password'] = 'PGBOUNCER_USER_PASSWORD_HASH' postgresql['sql_user_password'] = 'SQL_USER_PASSWORD_HASH' postgresql['listen_address'] = 'XX.XX.XX.Y' # XX.XX.XX.Y 是 postgresql 应该监听的节点上的 IP 地址 postgresql['md5_auth_cidr_addresses'] = %w(AA.AA.AA.B/32) # AA.AA.AA.B 是 pgbouncer 节点的 IP 地址
  4. 运行 gitlab-ctl reconfigure

    如果数据库已经在运行,则需要在重新配置后运行 `gitlab-ctl restart postgresql` 来重启。
  5. 在运行 PgBouncer 的节点上,确保在 /etc/gitlab/gitlab.rb 中设置以下内容

    ruby
    1pgbouncer['enable'] = true 2pgbouncer['databases'] = { 3 gitlabhq_production: { 4 host: 'DATABASE_HOST', 5 user: 'pgbouncer', 6 password: 'PGBOUNCER_USER_PASSWORD_HASH' 7 } 8}

    您可以为每个数据库传递其他配置参数,例如:

    ruby
    1pgbouncer['databases'] = { 2 gitlabhq_production: { 3 ... 4 pool_mode: 'transaction' 5 } 6}
  6. 运行 gitlab-ctl reconfigure

  7. 在运行 Puma 的节点上,确保在 /etc/gitlab/gitlab.rb 中设置以下内容

    ruby
    gitlab_rails['db_host'] = 'PGBOUNCER_HOST' gitlab_rails['db_port'] = '6432' gitlab_rails['db_password'] = 'SQL_USER_PASSWORD'
  8. 运行 gitlab-ctl reconfigure

  9. 此时,您的实例应该通过 PgBouncer 连接到数据库。如果您遇到问题,请参见故障排除部分

备份#

不要通过 PgBouncer 连接备份或恢复极狐GitLab:这会导致极狐GitLab 中断。

阅读更多关于如何重新配置备份

启用监控#

如果您启用了监控,则必须在 所有 PgBouncer 服务器上启用。

  1. 创建/编辑 /etc/gitlab/gitlab.rb 并添加以下配置:

    ruby
    1# 启用 Prometheus 的服务发现 2consul['enable'] = true 3consul['monitoring_service_discovery'] = true 4 5# 替换占位符 6# Y.Y.Y.Y consul1.gitlab.example.com Z.Z.Z.Z 7# 使用 Consul 服务器节点的地址 8consul['configuration'] = { 9 retry_join: %w(Y.Y.Y.Y consul1.gitlab.example.com Z.Z.Z.Z), 10} 11 12# 设置导出器将监听的网络地址 13node_exporter['listen_address'] = '0.0.0.0:9100' 14pgbouncer_exporter['listen_address'] = '0.0.0.0:9188'
  2. 运行 sudo gitlab-ctl reconfigure 来编译配置。

管理控制台#

在 Linux 软件包安装中,提供了一个命令来自动连接到 PgBouncer 管理控制台。

要启动会话,请运行以下命令并提供 pgbouncer 用户的密码:

shell
sudo gitlab-ctl pgb-console

获取关于实例的一些基本信息:

shell
1pgbouncer=# show databases; show clients; show servers; 2 name | host | port | database | force_user | pool_size | reserve_pool | pool_mode | max_connections | current_connections 3---------------------+-----------+------+---------------------+------------+-----------+--------------+-----------+-----------------+--------------------- 4 gitlabhq_production | 127.0.0.1 | 5432 | gitlabhq_production | | 100 | 5 | | 0 | 1 5 pgbouncer | | 6432 | pgbouncer | pgbouncer | 2 | 0 | statement | 0 | 0 6(2 rows) 7 8 type | user | database | state | addr | port | local_addr | local_port | connect_time | request_time | ptr | link 9| remote_pid | tls 10------+-----------+---------------------+--------+-----------+-------+------------+------------+---------------------+---------------------+-----------+------ 11+------------+----- 12 C | gitlab | gitlabhq_production | active | 127.0.0.1 | 44590 | 127.0.0.1 | 6432 | 2018-04-24 22:13:10 | 2018-04-24 22:17:10 | 0x12444c0 | 13| 0 | 14 C | gitlab | gitlabhq_production | active | 127.0.0.1 | 44592 | 127.0.0.1 | 6432 | 2018-04-24 22:13:10 | 2018-04-24 22:17:10 | 0x12447c0 | 15| 0 | 16 C | gitlab | gitlabhq_production | active | 127.0.0.1 | 44594 | 127.0.0.1 | 6432 | 2018-04-24 22:13:10 | 2018-04-24 22:17:10 | 0x1244940 | 17| 0 | 18 C | gitlab | gitlabhq_production | active | 127.0.0.1 | 44706 | 127.0.0.1 | 6432 | 2018-04-24 22:14:22 | 2018-04-24 22:16:31 | 0x1244ac0 | 19| 0 | 20 C | gitlab | gitlabhq_production | active | 127.0.0.1 | 44708 | 127.0.0.1 | 6432 | 2018-04-24 22:14:22 | 2018-04-24 22:15:15 | 0x1244c40 | 21| 0 | 22 C | gitlab | gitlabhq_production | active | 127.0.0.1 | 44794 | 127.0.0.1 | 6432 | 2018-04-24 22:15:15 | 2018-04-24 22:15:15 | 0x1244dc0 | 23| 0 | 24 C | gitlab | gitlabhq_production | active | 127.0.0.1 | 44798 | 127.0.0.1 | 6432 | 2018-04-24 22:15:15 | 2018-04-24 22:16:31 | 0x1244f40 | 25| 0 | 26 C | pgbouncer | pgbouncer | active | 127.0.0.1 | 44660 | 127.0.0.1 | 6432 | 2018-04-24 22:13:51 | 2018-04-24 22:17:12 | 0x1244640 | 27| 0 | 28(8 rows) 29 30 type | user | database | state | addr | port | local_addr | local_port | connect_time | request_time | ptr | link | rem 31ote_pid | tls 32------+--------+---------------------+-------+-----------+------+------------+------------+---------------------+---------------------+-----------+------+---- 33--------+----- 34 S | gitlab | gitlabhq_production | idle | 127.0.0.1 | 5432 | 127.0.0.1 | 35646 | 2018-04-24 22:15:15 | 2018-04-24 22:17:10 | 0x124dca0 | | 35 19980 | 36(1 row)

绕过 PgBouncer 的步骤#

Linux 软件包安装#

某些数据库更改必须直接完成,而不是通过 PgBouncer 完成。

主要受影响的任务是 数据库恢复极狐GitLab 升级与数据库迁移

  1. 要找到主节点,请在数据库节点上运行以下命令:

    shell
    sudo gitlab-ctl patroni members
  2. 在您执行任务的应用节点上编辑 /etc/gitlab/gitlab.rb,并更新 gitlab_rails['db_host']gitlab_rails['db_port'],以使用数据库主机和端口。

  3. 运行重新配置:

    shell
    sudo gitlab-ctl reconfigure

完成任务或步骤后,切换回使用 PgBouncer:

  1. 更改 /etc/gitlab/gitlab.rb 以指向 PgBouncer。

  2. 运行重新配置:

    shell
    sudo gitlab-ctl reconfigure

Helm chart 安装#

高可用部署也需要绕过 PgBouncer,原因与基于 Linux 软件包的安装相同。 对于 Helm chart 安装:

  • 数据库备份和恢复任务由工具箱容器执行。
  • 迁移任务由迁移容器执行。

您应该覆盖每个子 chart 上的 PostgreSQL 端口,以便这些任务可以直接连接并执行 PostgreSQL:

微调#

PgBouncer 的默认设置适合大多数安装。在特定情况下,您可能希望更改性能相关和资源相关的变量,以增加可能的吞吐量或限制资源使用,从而可能导致数据库的内存耗尽。

您可以在官方 PgBouncer 文档中找到参数及其相应文档。以下列出了最相关的内容及其在 Linux 软件包安装中的默认值:

  • pgbouncer['max_client_conn'] (默认值: 2048, 取决于服务器文件描述符限制) 这是 PgBouncer 中的 "前端" 池:从 Rails 到 PgBouncer 的连接。
  • pgbouncer['default_pool_size'] (默认值: 100) 这是 PgBouncer 中的 "后端" 池:从 PgBouncer 到数据库的连接。

default_pool_size 的理想数量必须足够处理所有需要访问数据库的预置服务。每个列出的服务使用以下公式来定义数据库池大小:

  • puma : max_threads + headroom (默认 14)
    • max_threads 通过以下方式配置: gitlab['puma']['max_threads'] (默认: 4)
    • headroom 可以通过 DB_POOL_HEADROOM 环境变量配置 (默认值为 10)
  • sidekiq : max_concurrency + 1 + headroom (默认 31)
    • max_concurrency 通过以下方式配置: sidekiq['max_concurrency'] (默认: 20)
    • headroom 可以通过 DB_POOL_HEADROOM 环境变量配置 (默认值为 10)
  • geo-logcursor: 1+headroom (默认 11)
    • headroom 可以通过 DB_POOL_HEADROOM 环境变量配置 (默认值为 10)

要计算 default_pool_size,请将 pumasidekiqgeo-logcursor 的实例数量乘以每个实例可以消耗的连接数量,如上所述。总数是建议的 default_pool_size

如果您使用多个 PgBouncer 和一个内部负载均衡器,您可以通过实例数量来除以 default_pool_size,以确保负载在它们之间均匀分布。

pgbouncer['max_client_conn'] 是 PgBouncer 可以接受的连接的硬限制。您不太可能需要更改此设置。如果您达到该限制,您可能需要考虑添加额外的 PgBouncer 和一个内部负载均衡器。

在设置指向 Geo 跟踪数据库的 PgBouncer 限制时,您可以在计算中忽略 puma,因为它仅偶尔访问该数据库。

故障排除#

如果您遇到通过 PgBouncer 连接的问题,首先查看的地方总是日志:

shell
sudo gitlab-ctl tail pgbouncer

此外,您可以查看 管理控制台 中的 show databases 的输出。在输出中,您应该期望看到 gitlabhq_production 数据库的 host 字段中的值。此外,current_connections 应大于 1。

信息: LOG: invalid CIDR mask in address#

请参见 Geo 文档中的建议修复 信息: LOG: invalid CIDR mask in address

信息: LOG: invalid IP mask "md5": Name or service not known#

请参见 Geo 文档中的建议修复 信息: LOG: invalid IP mask "md5": Name or service not known