Help us learn about your current experience with the documentation. Take the survey.

使用随附的 PgBouncer 服务

  • Tier: Free, Premium, Ultimate
  • Offering: GitLab Self-Managed

PgBouncer 已包含在 gitlab-ee 软件包中,但可免费使用。 如需技术支持,您需要 Premium 订阅

PgBouncer 用于在故障转移场景中无缝迁移服务器间的数据库连接。此外,它也可用于非容错配置的连接池,在减少资源占用的同时加快响应速度。

GitLab Premium 包含一个随附版本的 PgBouncer,可通过 /etc/gitlab/gitlab.rb 进行管理。

容错 GitLab 安装中的 PgBouncer

此内容已移至新位置

非容错 GitLab 安装中的 PgBouncer

  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 中设置以下内容:

    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 中设置以下内容:

    pgbouncer['enable'] = true
    pgbouncer['databases'] = {
      gitlabhq_production: {
        host: 'DATABASE_HOST',
        user: 'pgbouncer',
        password: 'PGBOUNCER_USER_PASSWORD_HASH'
      }
    }

    您可以为每个数据库传递额外的配置参数,例如:

    pgbouncer['databases'] = {
      gitlabhq_production: {
         ...
         pool_mode: 'transaction'
      }
    }

    请谨慎使用这些参数。完整参数列表请参考 PgBouncer 文档

  6. 运行 gitlab-ctl reconfigure

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

    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 并添加以下配置:

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

管理员控制台

在 Linux 软件包安装中,提供了自动连接 PgBouncer 管理员控制台的命令。关于如何与控制台交互的详细说明,请参阅 PgBouncer 文档

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

sudo gitlab-ctl pgb-console

获取实例基本信息:

pgbouncer=# show databases; show clients; show servers;
        name         |   host    | port |      database       | force_user | pool_size | reserve_pool | pool_mode | max_connections | current_connections
---------------------+-----------+------+---------------------+------------+-----------+--------------+-----------+-----------------+---------------------
 gitlabhq_production | 127.0.0.1 | 5432 | gitlabhq_production |            |       100 |            5 |           |               0 |                   1
 pgbouncer           |           | 6432 | pgbouncer           | pgbouncer  |         2 |            0 | statement |               0 |                   0
(2 rows)

 type |   user    |      database       | state  |   addr    | port  | local_addr | local_port |    connect_time     |    request_time     |    ptr    | link
| remote_pid | tls
------+-----------+---------------------+--------+-----------+-------+------------+------------+---------------------+---------------------+-----------+------
+------------+-----
 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 |
|          0 |
 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 |
|          0 |
 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 |
|          0 |
 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 |
|          0 |
 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 |
|          0 |
 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 |
|          0 |
 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 |
|          0 |
 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 |
|          0 |
(8 rows)

 type |  user  |      database       | state |   addr    | port | local_addr | local_port |    connect_time     |    request_time     |    ptr    | link | rem
ote_pid | tls
------+--------+---------------------+-------+-----------+------+------------+------------+---------------------+---------------------+-----------+------+----
--------+-----
 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 |      |
  19980 |
(1 row)

绕过 PgBouncer 的流程

Linux 软件包安装

某些数据库更改必须直接执行,不能通过 PgBouncer。

主要受影响的任务包括数据库恢复带数据库迁移的 GitLab 升级

  1. 在数据库节点上运行以下命令查找主节点:

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

  3. 运行重新配置:

    sudo gitlab-ctl reconfigure

完成任务或流程后,切换回使用 PgBouncer:

  1. /etc/gitlab/gitlab.rb 改回指向 PgBouncer。

  2. 运行重新配置:

    sudo gitlab-ctl reconfigure

Helm chart 安装

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

  • 数据库备份和恢复任务由 toolbox 容器执行。
  • 迁移任务由 migrations 容器执行。

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

性能调优

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

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

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

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

  • pumamax_threads + headroom(默认 14
    • max_threads 通过 gitlab['puma']['max_threads'] 配置(默认:4
    • headroom 可通过 DB_POOL_HEADROOM 环境变量配置(默认为 10
  • sidekiqmax_concurrency + 1 + headroom(默认:31
    • max_concurrency 通过 sidekiq['max_concurrency'] 配置(默认:20
    • headroom 可通过 DB_POOL_HEADROOM 环境变量配置(默认为 10
  • geo-logcursor1+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 连接时遇到任何问题,首先应检查日志:

sudo gitlab-ctl tail pgbouncer

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

消息:LOG: invalid CIDR mask in address

请参阅 Geo 文档中的建议修复方案

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

请参阅 Geo 文档中的建议修复方案