【问题标题】:Postgres DB URL string from a K8s deployment yaml file来自 K8s 部署 yaml 文件的 Postgres DB URL 字符串
【发布时间】:2021-11-01 22:33:27
【问题描述】:

我正在尝试从自定义 helm chart 联系 azure 上完全托管的 Postgres 服务,然后我必须根据我要部署的应用程序放置 url 连接字符串。

我想问在 helm chart 部署中,DATABASE_URL 应该是哪个值? 我的情况如下:

  • 我想使用外部 Azure 托管的 PostgreSQL,而不是 helm chart 附带的 PostgreSQL 容器。 因此,我修改了DATABASE_URL的值,given here连接到K8s内部的容器,我是这样修改的:
 name: DATABASE_URL
 # value: "postgres://{{ .Values.postgresql.postgresqlUsername }}:$(POSTGRESQL_PASSWORD)@{{ .Release.Name }}-postgresql"
 value: "postgres://nmbrs@postgresql-nmb-psfc-stag:$(POSTGRESQL_PASSWORD)@postgresql-nmb-psfc-stag.postgres.database.azure.com/postfacto-staging-db"

但我收到此错误

/usr/local/lib/ruby/2.7.0/uri/generic.rb:208:in `initialize': the scheme postgres does not accept registry part: nmbrs@postgresql-nmb-psfc-stag:mypassword*@postgresql-nmb-psfc-stag.postgres.database.azure.com (or bad hostname?) (URI::InvalidURIError)

如果我想联系完全 Postgres 托管的服务,那么真正的 DATABASE_URL 值应该是什么?

与此等价的值是多少?:

value: "postgres://{{ .Values.postgresql.postgresqlUsername }}:$(POSTGRESQL_PASSWORD)@{{ .Release.Name }}-postgresql"

我的意思是

postgres//<username>:<my-pg-password>@<WHICH VALUE SHOULD BE HERE?>

{{ .Release.Name }}-postgresql" 的值是多少?

为了记录,我自定义的postfacto/deployment/helm/templates/deployment.yamlthis

更新

我改变了这个值

- name: DATABASE_URL
            # value: "postgres://{{ .Values.postgresql.postgresqlUsername }}:$(POSTGRESQL_PASSWORD)@{{ .Release.Name }}-postgresql"
            # value: "postgres://nmbrs@postgresql-nmb-psfc-stag:$(POSTGRESQL_PASSWORD)@postgresql-nmb-psfc-stag.postgres.database.azure.com:5432/postfacto-staging-db"
            value: "postgres://postgresql-nmb-psfc-stag.postgres.database.azure.com/postfacto-staging-db"
          

我得到了一个不同的错误:

Caused by:
PG::ConnectionBad: FATAL:  Invalid Username specified. Please check the Username and retry connection. The Username should be in <username@hostname> format.
FATAL:  Invalid Username specified. Please check the Username and retry connection. The Username should be in <username@hostname> format.

但不清楚语法格式应该如何,因为this article 说:

接下来,对数据库凭据进行编码。使用格式 DB_ADAPTER://USER:PASSWORD@HOSTNAME/DB_NAME。如果您在 127.0.0.1 上使用用户“deploy”和密码“secret”的 mysql,并且有一个数据库 railsapp,请运行

DB_ADAPTER://USER:PASSWORD@HOSTNAME/DB_NAME的格式,和我一开始用的一样

【问题讨论】:

  • value: "postgres://nmbrs:$(POSTGRESQL_PASSWORD)@postgresql-nmb-psfc-stag.postgres.database.azure.com/postfacto-staging-db" - 为什么将数据库名称添加到用户名/密码段
  • 是的,数据库名称不应该在那里,if we see the url defined in the helm chart 我只是在尝试不同的格式。无论如何,如果我最后删除数据库名称,错误是一样的。
  • @ArunPJohny 我认为 Rails 作为一种框架有一种特定的方式来定义数据库 url 连接,我正在检查这个 guides.rubyonrails.org/configuring.html#configuring-a-database 但我不清楚
  • 改后报错是什么?另外,test2021* 是什么?是密码吗
  • @ArunPJohny 是的,这是一个测试密码,对不起,你能在问题中看到我上面的更新吗?

标签: postgresql kubernetes


【解决方案1】:

我认为您的连接字符串的问题是,它的用户名有一个特殊字符@,这可能会破坏连接字符串格式并导致验证错误。

你的价值

- name: DATABASE_URL
  value: "postgres://nmbrs@postgresql-nmb-psfc-stag:$(POSTGRESQL_PASSWORD)@postgresql-nmb-psfc-stag.postgres.database.azure.com/postfacto-staging-db"

您可以尝试对 username 部分进行 URL 编码,例如

- name: DATABASE_URL
  value: "postgres://nmbrs%40postgresql-nmb-psfc-stag:$(POSTGRESQL_PASSWORD)@postgresql-nmb-psfc-stag.postgres.database.azure.com/postfacto-staging-db"

【讨论】:

    【解决方案2】:

    据我所见,这个 helm 图表写得不好,并且错误地假设了一些事情,即 DATABASE_URL 已构建为仅正确格式化 kubernetes posgress helm 版本而不是其他内容。

    我的建议:

    1. 不要通过 helm 在 k8s 上安装此图表,而是使用 helm template 功能在本地渲染模板。
    2. 编辑导出的普通清单,以满足您的需求
    3. 转到您的 Azure DB,并获取它ConnectionString,这取决于您如何在 k8s 中管理您的秘密,将其传递给 DATABASE_URL 环境
    4. 手动应用清单

    【讨论】:

    • 我知道我会考虑这个。谢谢
    猜你喜欢
    • 1970-01-01
    • 2021-08-28
    • 1970-01-01
    • 2022-10-13
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    • 2022-01-17
    • 1970-01-01
    相关资源
    最近更新 更多