当前位置: 首页 > 工具软件 > Patroni > 使用案例 >

PostgreSQL数据库高可用——patroni配置

齐意致
2023-12-01

Patroni 配置存储在 DCS(分布式配置存储)中。有 3 种类型的配置:(Patroni configuration is stored in the DCS (Distributed Configuration Store). There are 3 types of configuration:)

  • 动态配置。这些选项可以随时在 DCS 中设置。如果更改的选项不是启动配置的一部分,它们将异步应用(在下一个唤醒周期)到每个节点,随后会重新加载。如果节点需要重新启动以应用配置(对于具有上下文 postmaster 的选项,如果它们的值已更改),pending_restart则在 members.data JSON 中设置一个特殊标志来指示这一点。此外,节点状态也通过显示"restart_pending": true来指示这一点。(These options can be set in DCS at any time. If the options changed are not part of the startup configuration, they are applied asynchronously (upon the next wake up cycle) to every node, which gets subsequently reloaded. If the node requires a restart to apply the configuration (for options with context postmaster, if their values have changed), a special flag, pending_restart indicating this, is set in the members.data JSON. Additionally, the node status also indicates this, by showing “restart_pending”: true.)
  • 本地配置(patroni.yml)。这些选项在配置文件中定义,优先于动态配置。通过向 Patroni 进程发送 SIGHUP、执行POST /reload REST-API 请求或执行patronictl reload可以在运行时更改和重新加载patroni.yml(无需重新启动 Patroni)。(These options are defined in the configuration file and take precedence over dynamic configuration. patroni.yml could be changed and reloaded in runtime (without restart of Patroni) by sending SIGHUP to the Patroni process, performing POST /reload REST-API request or executing patronictl reload.)
  • 环境配置。可以使用环境变量设置/覆盖一些“本地”配置参数。当您在动态环境中运行并且您事先不知道某些参数(例如在内部运行时无法知道您的外部IP地址docker)时,环境配置非常有用。(It is possible to set/override some of the “Local” configuration parameters with environment variables. Environment configuration is very useful when you are running in a dynamic environment and you don’t know some of the parameters in advance (for example it’s not possible to know your external IP address when you are running inside docker).)

本地配置可以是单个 YAML 文件或目录。当它是一个目录时,该目录中的所有 YAML 文件按排序顺序一个接一个地加载。如果在多个文件中定义了一个键,则最后一个文件中出现的参数配置优先。(The local configuration can be either a single YAML file or a directory. When it is a directory, all YAML files in that directory are loaded one by one in sorted order. In case a key is defined in multiple files, the occurrence in the last file takes precedence.)

某些 PostgreSQL 参数必须在主服务器和副本上保持相同的值。对于那些参数,在本地patroni配置文件或通过环境变量设置的值无效。要更改或设置它们的值,必须更改 DCS 中的共享配置。以下是此类参数的实际列表以及默认值:(Some of the PostgreSQL parameters must hold the same values on the master and the replicas. For those, values set either in the local patroni configuration files or via the environment variables take no effect. To alter or set their values one must change the shared configuration in the DCS. Below is the actual list of such parameters together with the default values:)

  • max_connections:100
  • max_locks_per_transaction:64
  • max_worker_processes:8
  • max_prepared_transactions:0
  • wal_level: hot_standby
  • wal_log_hints:on
  • track_commit_timestamp:off

对于以下参数,PostgreSQL 不要求 master 和所有副本之间的值相等。但是,考虑到副本随时可能成为主控,因此将它们设置不同并没有任何意义;因此,Patroni 将它们的值设置为动态配置(For the parameters below, PostgreSQL does not require equal values among the master and all the replicas. However, considering the possibility of a replica to become the master at any time, it doesn’t really make sense to set them differently; therefore, Patroni restricts setting their values to the Dynamic configuration)

  • max_wal_senders: 5
  • max_replication_slots: 5
  • wal_keep_segments: 8
  • wal_keep_size: 128MB
    这些参数经过验证,以确保它们是健全的,或满足最小值(These parameters are validated to ensure they are sane, or meet a minimum value.)。

Patroni 还控制了其他一些 Postgres 参数:

  • listen_addresses -从环境变量postgresql.listen或从PATRONI_POSTGRESQL_LISTEN环境变量设置
  • port -从环境变量postgresql.listen或从PATRONI_POSTGRESQL_LISTEN环境变量设置
  • cluster_name -从环境变量scope或从PATRONI_SCOPE环境变量设置
  • hot_standby: on
    为了安全起见,上述列表中的参数不会写入postgresql.conf,而是作为参数列表传递给 pg_ctl start,这使它们具有最高优先级,甚至高于ALTER SYSTEM。

应用本地或动态配置选项时,将执行以下操作:

  • 节点首先检查是否有 postgresql.base.conf 或者是否custom_conf设置了参数。
  • 如果设置了custom_conf参数,它将把上面指定的文件作为基本配置,忽略postgresql.base.conf和postgresql.conf。
  • 如果custom_conf参数未设置且postgresql.base.conf存在,则它包含重命名的“原始”配置,并将用作基本配置。
  • 如果没有custom_conf和postgresql.base.conf,则使用原始的 postgresql.conf 并将其重命名为 postgresql.base.conf。
  • 动态选项(除了上面的例外)被转储到 postgresql.conf 中,并且在 postgresql.conf 中将包含设置为使用的基本配置(postgresql.base.conf 或custom_conf中的配置)。因此,我们可以应用新选项而无需重新读取配置文件来检查包含是否不存在。(The dynamic options (with the exceptions above) are dumped into the postgresql.conf and an include is set in postgresql.conf to the used base configuration (either postgresql.base.conf or what is on custom_conf). Therefore, we would be able to apply new options without re-reading the configuration file to check if the include is present not.)
  • Patroni 管理集群所必需的一些参数被使用命令行覆盖。
  • 如果某些需要重新启动的选项被更改(我们应该查看 pg_settings 中的上下文和这些选项的实际值),则设置给定节点的 pending_restart 标志。此标志在任何重新启动时都会重置。

参数将按以下顺序应用(运行时具有最高优先级):

  1. 从文件postgresql.base.conf(或从custom_conf文件,如果已设置)加载参数
  2. 从文件postgresql.conf加载参数
  3. 从文件postgresql.auto.conf加载参数
  4. 使用-o –name=value的运行时参数

这允许配置所有节点 (2),使用ALTER SYSTEM配置特定节点(3) 并确保执行 Patroni 运行所必需的参数 (4),并为管理postgresql 的配置工具留出空间,conf直接而不涉及 Patroni (1)(This allows configuration for all the nodes (2), configuration for a specific node using ALTER SYSTEM (3) and ensures that parameters essential to the running of Patroni are enforced (4), as well as leaves room for configuration tools that manage postgresql.conf directly without involving Patroni (1).)。

此外,以下 Patroni 配置选项只能动态更改:

  • ttl: 30
  • loop_wait: 10
  • retry_timeouts: 10
  • maximum_lag_on_failover: 1048576
  • max_timelines_history: 0
  • check_timeline: false
  • postgresql.use_slots: true
    更改这些选项后,Patroni 将读取存储在 DCS 中的配置的相关部分并更改其运行时值。

每次配置更改时,Patroni 节点都会将 DCS 选项的状态转储到磁盘,该文件patroni.dynamic.json位于 Postgres 数据目录中的文件中。如果这些选项在 DCS 中完全不存在或它们无效,则仅允许主服务器从磁盘转储中恢复这些选项。(Patroni nodes are dumping the state of the DCS options to disk upon for every change of the configuration into the file patroni.dynamic.json located in the Postgres data directory. Only the master is allowed to restore these options from the on-disk dump if these are completely absent from the DCS or if they are invalid.)

 类似资料: