【问题标题】:Configuring Azure PostgreSQL in Gitlab EE在 Gitlab EE 中配置 Azure PostgreSQL
【发布时间】:2021-03-25 09:14:51
【问题描述】:

我正在寻找有关如何在基于 Docker Swarm 的 Gitlab 实例中配置 Azure PostgreSQL DB 的帮助。

最初,我遵循https://docs.gitlab.com/13.6/ee/administration/postgresql/external.html 中的文档。然而我发现默认提供的用户是 username 的形式,而 Azure 要求它是 username@hostname 的形式。我尝试在 gitlab.rb 文件 (gitlab_rails['db_username'] = 'username@hostname') 中传递用户名,但它仍然失败,即使将 @ 替换为 %40 作为 URI 编码。

经过一番广泛的搜索,我找到了这个文档 - https://docs.gitlab.com/13.6/ee/administration/environment_variables.html,它建议使用DATABASE_URL 环境变量以postgresql://username:password@hostname:port/dbname 的形式设置完整的连接字符串,我这样做了,它确实解决了 Gitlab 的问题本身与 Azure PostgreSQL 通信(在这种情况下,我根据 Azure 要求将用户名替换为 username%40hostname)。

唉,成功是短暂的,后来我发现Puma和Sidekiq都无法连接到数据库,总是抛出以下错误:

==> /var/log/gitlab/sidekiq/current <==
could not connect to server: No such file or directory
        Is the server running locally and accepting
        connections on Unix domain socket "/var/opt/gitlab/postgresql/.s.PGSQL.5432"?

经过一番搜索,发现gitlab-ctl在启动Gitlab实例时生成如下文件:

# This file is managed by gitlab-ctl. Manual changes will be
# erased! To change the contents below, edit /etc/gitlab/gitlab.rb
# and run `sudo gitlab-ctl reconfigure`.
production:
  adapter: postgresql
  encoding: unicode
  collation:
  database: <database>
  username: "<username>"
  password:
  host: "/var/opt/gitlab/postgresql"
  port: 5432
  socket:
  sslmode:
  sslcompression: 0
  sslrootcert:
  sslca:
  load_balancing: {"hosts":[]}
  prepared_statements: false
  statement_limit: 1000
  connect_timeout:
  variables:
    statement_timeout:

(删除的数据库和用户名)

它几乎忽略了DATABASE_URL env 变量并假定 gitlab.rb 中现在不存在的配置参数。

所以,现在,我有点别无选择,想知道是否有人遇到过类似的问题,如果有,您如何克服这个问题。

感谢任何帮助。

提前致谢。

【问题讨论】:

    标签: gitlab sidekiq gitlab-omnibus azure-postgresql gitlab-ee


    【解决方案1】:

    TL/DR:将username@hostname 字符串直接传递到双引号中的 gitlab_rails['db_username'] 中。官方 Gitlab 页面中连接 Azure PostgreSQL 的文档不正确。

    所以,经过一番搜索和深入Gitlab配置,我发现这个问题非常具体,并且与docker secrets的使用有关。

    在我的 gitlab.rb 配置文件中,在数据库配置部分,我使用以下内容:

    ### GitLab database settings
    ###! Docs: https://docs.gitlab.com/omnibus/settings/database.html
    ###! **Only needed if you use an external database.**
    gitlab_rails['db_adapter'] = "postgresql"
    gitlab_rails['db_encoding'] = "unicode"
    gitlab_rails['db_database'] = File.read('/run/secrets/postgresql_database')
    gitlab_rails['db_username'] = File.read('/run/secrets/postgresql_user')
    gitlab_rails['db_password'] = File.read('/run/secrets/postgresql_password')
    gitlab_rails['db_host'] = File.read('/run/secrets/postgresql_host')
    gitlab_rails['db_port'] = File.read('/run/secrets/postgresql_port')
    gitlab_rails['db_sslmode'] = 'require'
    

    现在,这个确切的配置以前用于测试目的并且可以正常工作(但没有使用 Azure PostgreSQL 数据库)。我正在将正确的秘密传递给 docker,并且我已经确认这些秘密确实存在。

    (旁注:另外,我已经确定 Gitlab 使用 Ruby ActiveRecord::Base 库中的 ActiveRecord::Base.establish_connection 方法来连接数据库)

    然而,当为用户使用 username@hostname 配置并将其传递到 postgresql_user 密码时,ActiveRecord::Base.establish_connection 方法突然假定 @hostname 是我想要连接的实际主机名。而且我已经确认在 docker 容器内正确生成了秘密

    现在,它变得更加奇怪了,因为如果我将 username@hostname 字符串直接传递给 gitlab.rb 文件 - gitlab_rails['db_username'] 参数 - 在双引号中,它会突然开始连接而不会抱怨。

    因此,简而言之,如果将 Azure PostgreSQL 数据库用于 dockerized Gitlab 实例并使用机密将配置传递给 gitlab.rb 文件,不要username@hostame通过secret传递,但直接放在gitlab.rb文件中。

    我不知道这是 Ruby 还是 Gitlab 的特定问题(我不是 Ruby 开发人员),但我确实尝试将 File.read 输出转换为字符串、符号、使用文件.open('filepath', &:readline) 和其他恶作剧,但没有任何效果。因此,如果有人愿意为此添加他们的理由,请随时这样做。

    此外,Azure 提供的教程 - https://docs.microsoft.com/pt-pt/azure/postgresql/connect-ruby - 不适用于 Gitlab,因为它抱怨 %40。

    希望这可以帮助任何人。

    【讨论】:

      猜你喜欢
      • 2020-08-07
      • 2021-06-06
      • 1970-01-01
      • 1970-01-01
      • 2015-11-30
      • 2014-06-04
      • 2013-09-01
      • 2019-06-29
      • 1970-01-01
      相关资源
      最近更新 更多