当前位置: 代码网 > it编程>数据库>MsSqlserver > 【Docker】PostgreSQL 容器化部署

【Docker】PostgreSQL 容器化部署

2024年08月02日 MsSqlserver 我要评论
PostgreSQL标准软件基于Bitnami PostgreSQL 构建。当前版本为16.1.0。

postgresql标准软件基于bitnami postgresql 构建。当前版本为16.1.0

你可以通过轻云uc部署工具直接安装部署,也可以手动按如下文档操作,该项目已经全面开源,可以从如下环境获取
配置文件地址: https://gitee.com/qingplus/qingcloud-platform

qinghub studio 在线体验

连接到其他容器

使用docker 容器网络,应用程序容器可以轻松访问容器内运行的 postgresql 服务器。

连接到同一网络的容器可以使用容器名称作为主机名来相互通信。

使用命令行

在此示例中,我们将创建一个 postgresql 客户端实例,该实例将连接到与客户端在同一 docker 网络上运行的服务器实例。

第 1 步:创建网络
docker network create app-tier --driver bridge
步骤 2:启动 postgresql 服务器实例

使用命令–network app-tier的参数docker run将 postgresql 容器连接到网络app-tier。

docker run -d --name postgresql-server \
    --network app-tier \
    registry.cn-hangzhou.aliyuncs.com/qingcloudtech/postgresql:latest
第 3 步:启动 postgresql 客户端实例

最后,我们创建一个新的容器实例来启动 postgresql 客户端并连接到上一步中创建的服务器:

docker run -it --rm \
    --network app-tier \
    registry.cn-hangzhou.aliyuncs.com/qingcloudtech/postgresql:latest psql -h postgresql-server -u postgres

使用 docker compose 文件

如果未指定,docker compose 会自动设置一个新网络并将所有已部署的服务附加到该网络。但是,我们将显式定义一个bridge名为 的新网络app-tier。在此示例中,我们假设您希望从您自己的自定义应用程序映像连接到 postgresql 服务器,该映像在以下代码片段中通过服务名称进行标识myapp。

version: '2'

networks:
  app-tier:
    driver: bridge

services:
  postgresql:
    image: 'registry.cn-hangzhou.aliyuncs.com/qingcloudtech/postgresql:latest'
    networks:
      - app-tier
  myapp:
    image: 'your_application_image'
    networks:
      - app-tier
docker-compose up -d

配置

容器启动时

当容器执行时,初始化或启动postgresql之前,它会执行位于/docker-entrypoint-preinitdb.d的扩展名为.sh的文件。

为了将自定义文件放入 docker 映像中,您可以将它们安装为卷。

将额外的命令行标志传递给 postgresql

可以通过以下环境变量将额外的命令行标志传递给 postgresql 服务命令:

  • postgresql_extra_flags:要附加到postgres启动命令的标志。无默认值

初始化一个新实例

当容器第一次执行时,它将执行扩展名为.sh, .sql.sql.gz的文件,位于/docker-entrypoint-initdb.d目录下.

为了将自定义文件放入 docker 映像中,您可以将它们安装为卷。

首次运行时设置 root 密码

在上面的命令中您可能已经注意到环境变量的使用postgresql_password。首次运行映像时传递环境变量postgresql_password会将postgres用户的密码设置为postgresql_password的值(或postgresql_password_file变量中指定的文件的内容)。

docker run --name postgresql -e postgresql_password=password123 registry.cn-hangzhou.aliyuncs.com/qingcloudtech/postgresql:latest

或者通过修改docker-compose.yml文件:

services:
  postgresql:
  ...
    environment:
      - postgresql_password=password123
  ...

注意! 该postgres用户是超级用户,拥有 postgresql 数据库的完全管理访问权限。

首次运行时创建数据库

postgresql_database通过在第一次运行镜像时传递环境变量,将创建一个数据库。如果您的应用程序要求数据库已存在,这非常有用,使您不必使用 postgresql 客户端手动创建数据库。

docker run --name postgresql -e postgresql_database=my_database registry.cn-hangzhou.aliyuncs.com/qingcloudtech/postgresql:latest

或者通过修改docker-compose.yml文件:

services:
  postgresql:
  ...
    environment:
      - postgresql_database=my_database
  ...

首次运行时创建数据库用户

您还可以创建一个受限数据库用户,该用户仅具有使用环境变量创建的数据库的权限postgresql_database。为此,请提供postgresql_username环境变量。

docker run --name postgresql -e postgresql_username=my_user -e postgresql_password=password123 -e postgresql_database=my_database registry.cn-hangzhou.aliyuncs.com/qingcloudtech/postgresql:latest

或者通过修改docker-compose.yml文件:

services:
  postgresql:
  ...
    environment:
      - postgresql_username=my_user
      - postgresql_password=password123
      - postgresql_database=my_database
  ...

注意!postgresql_username指定后,postgres user不会分配密码,因此您无法以该postgres用户身份远程登录 postgresql 服务器。如果您仍想使用用户postgres进行访问,请设置postgresql_postgres_password环境变量(或postgresql_postgres_password_file指定文件中内容)。

审计

postgresql image默认启用 pgaudit 模块。因此,可以使用以下环境变量在容器中启用审核信息:

  • postgresql_pgaudit_log:以逗号分隔的列表,其中包含要审核的不同操作。没有默认值。
  • postgresql_pgaudit_log_catalog:在语句中的所有关系都在 pg_catalog 中的情况下启用会话日志记录。没有默认值。
  • postgresql_log_connections:添加登录日志条目。没有默认值。
  • postgresql_log_disconnections:添加注销日志条目。没有默认值。
  • postgresql_log_hostname:记录客户端主机名。没有默认值。
  • postgresql_log_line_prefix:定义日志条目行的格式。在postgresql 官方文档中查找字符串参数。没有默认值。
  • postgresql_log_timezone:设置日志条目时间戳的时区。没有默认值。

会话设置

postgresql 映像允许配置多个连接和会话管理参数:

  • postgresql_username_connection_limit:如果创建了不同的用户postgres,请设置连接限制。没有默认值。
  • postgresql_postgres_connection_limit:设置用户的连接限制postgres。没有默认值。
  • postgresql_statement_timeout:设置语句超时时间。没有默认值。
  • postgresql_tcp_keepalives_interval:tcp 保活间隔。没有默认值。
  • postgresql_tcp_keepalives_idle:tcp keepalive 空闲时间。没有默认值。
  • postgresql_tcp_keepalives_count:tcp 保活计数。没有默认值。

配置时区

postgresql 映像允许使用以下环境变量配置 postgresql 的时区:

  • postgresql_timezone:设置显示和解释时间戳的时区。
  • postgresql_log_timezone:设置写入服务器日志的时间戳所使用的时区。

修改pg_hba.conf

默认情况下,postgresql image会生成local、md5 在pg_hba.conf文件。为了适应任何其他要求或标准,可以通过以下方式更改 pg_hba.conf 文件:

  • 挂载你自己的pg_hba.conf 到文件/bitnami/postgresql/conf
  • postgresql_pghba_remove_filters将与以逗号分隔的模式列表一起使用。所有与任何模式匹配的行都将被删除。例如,如果我们想要删除所有local和md5验证(例如,仅支持hostssl连接),请设置postgresql_pghba_remove_filters=local, md5.

预加载共享库

可以通过设置 .postgresql 文件来修改 postgresql 在启动时预加载的库列表postgresql_shared_preload_libraries。默认值为postgresql_shared_preload_libraries=pgaudit。例如,如果您想将pg_stat_statements库添加到预加载中,请设置postgresql_shared_preload_libraries=pgaudit, pg_stat_statements。

设置流式复制

可以使用以下环境变量设置流复制集群:

  • postgresql_replication_mode:复制模式。可能的值master/ slave。没有默认值。
  • postgresql_replication_user:首次运行时在主服务器上创建的复制用户。没有默认值。
  • postgresql_replication_password:复制用户密码。没有默认值。
  • postgresql_replication_password_file:包含复制用户密码的文件的路径。这将覆盖 中指定的值postgresql_replication_password。没有默认值。
  • postgresql_master_host:复制主机的主机名/ip(从机参数)。没有默认值。
  • postgresql_master_port_number:复制主机的服务器端口(从机参数)。默认为5432.
    在复制集群中,您可以拥有一个主服务器和零个或多个从服务器。启用复制后,主节点处于读写模式,而从节点处于只读模式。为了获得最佳性能,建议限制对从属设备的读取。
第 1 步:创建复制主服务器

第一步是启动master。

docker run --name postgresql-master \
  -e postgresql_replication_mode=master \
  -e postgresql_username=my_user \
  -e postgresql_password=password123 \
  -e postgresql_database=my_database \
  -e postgresql_replication_user=my_repl_user \
  -e postgresql_replication_password=my_repl_password \
  registry.cn-hangzhou.aliyuncs.com/qingcloudtech/postgresql:latest

在此命令中,我们使用参数将容器配置为主容器postgresql_replication_mode=master。postgresql_replication_user使用和参数指定复制用户postgresql_replication_password。

第2步:创建复制从站

接下来我们启动一个复制从属容器。

docker run --name postgresql-slave \
  --link postgresql-master:master \
  -e postgresql_replication_mode=slave \
  -e postgresql_master_host=master \
  -e postgresql_master_port_number=5432 \
  -e postgresql_replication_user=my_repl_user \
  -e postgresql_replication_password=my_repl_password \
  registry.cn-hangzhou.aliyuncs.com/qingcloudtech/postgresql:latest

在上面的命令中,容器被配置为slave使用postgresql_replication_mode参数。在复制从属启动之前,从属容器使用postgresql_master_host和postgresql_master_port_number参数连接到主服务器并从主服务器复制初始数据库。postgresql_replication_password和postgresql_replication_user用于向主服务器进行身份验证。为了更改pg_hba.conf默认设置,从站需要知道是否postgresql_password已设置。

通过这两个命令,您现在已经启动并运行了一个两节点 postgresql 主从流复制集群。您可以通过添加/删除从属服务器来扩展集群,而不会导致任何停机。

注意:集群会完整复制主节点,其中包括所有用户和数据库。

如果主服务器出现故障,您可以重新配置从服务器以充当主服务器,并通过创建触发器文件开始接受写入/tmp/postgresql.trigger.5432。例如,以下命令重新配置postgresql-slave为充当主服务器:

docker exec postgresql-slave touch /tmp/postgresql.trigger.5432

注意:集群中其他从站的配置需要更新,以便它们知道新的主站。–link postgresql-slave:master这将要求您按照我们的示例重新启动其他从站。

通过 docker compose,可以使用以下命令设置主从复制:

version: '2'

services:
  postgresql-master:
    image: 'registry.cn-hangzhou.aliyuncs.com/qingcloudtech/postgresql:latest'
    ports:
      - '5432'
    volumes:
      - 'postgresql_master_data:/bitnami/postgresql'
    environment:
      - postgresql_replication_mode=master
      - postgresql_replication_user=repl_user
      - postgresql_replication_password=repl_password
      - postgresql_username=my_user
      - postgresql_password=my_password
      - postgresql_database=my_database
  postgresql-slave:
    image: 'registry.cn-hangzhou.aliyuncs.com/qingcloudtech/postgresql:latest'
    ports:
      - '5432'
    depends_on:
      - postgresql-master
    environment:
      - postgresql_replication_mode=slave
      - postgresql_replication_user=repl_user
      - postgresql_replication_password=repl_password
      - postgresql_master_host=postgresql-master
      - postgresql_password=my_password
      - postgresql_master_port_number=5432

volumes:
  postgresql_master_data:

使用以下方法扩展从站数量:

docker-compose up --detach --scale postgresql-master=1 --scale postgresql-slave=3

上面的命令将 slave 的数量增加到3。您可以用同样的方法缩小规模。

注意:您不应增加/减少主节点的数量。始终只有一个主节点运行。

同步提交

默认情况下,从属实例配置为异步复制。为了保证更多的数据稳定性(以一些性能为代价),可以使用以下环境设置同步提交(即事务提交在被写入一组副本之前不会向客户端返回成功)变量。

  • postgresql_synchronous_commit_mode:建立同步提交的类型。可用选项有:on, remote_apply, remote_write, local and off。默认值为on。
  • postgresql_num_synchronous_replicas:建立将启用同步复制的副本数量。该数量不得超过您在集群中配置的从站数量。
    使用 docker compose,可以按如下方式设置具有同步提交的主从复制:
version: '2'

services:
  postgresql-master:
    image: 'registry.cn-hangzhou.aliyuncs.com/qingcloudtech/postgresql:latest'
    ports:
      - '5432'
    volumes:
      - 'postgresql_master_data:/bitnami/postgresql'
    environment:
      - postgresql_replication_mode=master
      - postgresql_replication_user=repl_user
      - postgresql_replication_password=repl_password
      - postgresql_username=my_user
      - postgresql_password=my_password
      - postgresql_database=my_database
      - postgresql_synchronous_commit_mode=on
      - postgresql_num_synchronous_replicas=1
    volumes:
      - '/path/to/postgresql-persistence:/bitnami/postgresql'
  postgresql-slave:
    image: 'registry.cn-hangzhou.aliyuncs.com/qingcloudtech/postgresql:latest'
    ports:
      - '5432'
    depends_on:
      - postgresql-master
    environment:
      - postgresql_replication_mode=slave
      - postgresql_replication_user=repl_user
      - postgresql_replication_password=repl_password
      - postgresql_master_host=postgresql-master
      - postgresql_master_port_number=5432
  postgresql-slave2:
    image: 'registry.cn-hangzhou.aliyuncs.com/qingcloudtech/postgresql:latest'
    ports:
      - '5432'
    depends_on:
      - postgresql-master
    environment:
      - postgresql_replication_mode=slave
      - postgresql_replication_user=repl_user
      - postgresql_replication_password=repl_password
      - postgresql_master_host=postgresql-master
      - postgresql_master_port_number=5432

在上面的示例中,提交需要写入主服务器和从服务器之一才能被接受。另一个从站将继续使用异步复制。使用以下 sql 查询检查它:

postgres=# select application_name as server, state,
postgres-#       sync_priority as priority, sync_state
postgres-#       from pg_stat_replication;
| server      | state     | priority | sync_state |
|-------------|-----------|----------|------------|
| walreceiver | streaming | 0        | sync       |
| walreceiver | streaming | 0        | async      |

注意:对于更高级的设置,您可以application_name通过设置postgresql_cluster_app_name环境变量来使用参数定义不同的复制组。

ldap认证

为了使用 ldap 身份验证,您需要将环境变量设置postgresql_enable_ldap为yes来启用它。

有两种设置 ldap 配置的方法:

  • 通过配置postgresql_ldap_url,您可以在其中配置url中的所有关联参数。

  • 独立设置参数postgresql_ldap_xxxx。
    ldap相关参数有:

  • postgresql_ldap_server:要连接的 ldap 服务器的 ip 地址或名称。用空格分隔。

  • postgresql_ldap_port:ldap 服务器上要连接的端口号

  • postgresql_ldap_scheme:设置为ldaps使用 ldaps。默认为无。

  • postgresql_ldap_tls:设置为1使用 tls 加密。默认为无。

  • postgresql_ldap_prefix:形成要绑定的 dn 时要添加到用户名前面的字符串。默认为无。

  • postgresql_ldap_suffix:形成要绑定的 dn 时附加到用户名的字符串。默认为无。

  • postgresql_ldap_base_dn:开始搜索用户的根 dn。默认为无。

  • postgresql_ldap_bind_dn:要绑定到 ldap 的用户的 dn。默认为无。

  • postgresql_ldap_bind_password:绑定ldap的用户密码。默认为无。

  • postgresql_ldap_search_attr:与搜索中的用户名匹配的属性。默认为无。

  • postgresql_ldap_search_filter:进行搜索+绑定身份验证时使用的搜索过滤器。默认为无。

  • postgresql_ldap_url:要连接的 url,格式为:ldap[s]😕/host[:port]/basedn[?[attribute][?[scope][?[filter]]]]。

保护 postgresql 流量

postgresql 支持使用 ssl/tls 协议对连接进行加密。如果您希望启用此可选功能,您可以使用以下环境变量来配置应用程序:

  • postgresql_enable_tls:是否对流量启用 tls。默认为no.
  • postgresql_tls_cert_file:包含 tls 流量证书文件的文件。没有默认值。
  • postgresql_tls_key_file:包含证书密钥的文件。没有默认值。
  • postgresql_tls_ca_file:包含证书 ca 的文件。如果提供,postgresql 将通过请求证书来验证 tls/ssl 客户端(请参阅ref)。没有默认值。
  • postgresql_tls_crl_file:包含证书吊销列表的文件。没有默认值。
  • postgresql_tls_prefer_server_ciphers:是否使用服务器的 tls 密码首选项而不是客户端的。默认为yes.
    启用 tls 时,postgresql 默认情况下将支持标准流量和加密流量,但更喜欢后者。下面是一些有关如何快速设置 tls 流量的示例:

使用docker run

```console
$ docker run \
    -v /path/to/certs:/opt/bitnami/postgresql/certs \
    -e allow_empty_password=yes \
    -e postgresql_enable_tls=yes \
    -e postgresql_tls_cert_file=/opt/bitnami/postgresql/certs/postgres.crt \
    -e postgresql_tls_key_file=/opt/bitnami/postgresql/certs/postgres.key \
    registry.cn-hangzhou.aliyuncs.com/qingcloudtech/postgresql:latest
```

修改docker-compose.yml文件:

```yaml
services:
  postgresql:
  ...
    environment:
      ...
      - postgresql_enable_tls=yes
      - postgresql_tls_cert_file=/opt/bitnami/postgresql/certs/postgres.crt
      - postgresql_tls_key_file=/opt/bitnami/postgresql/certs/postgres.key
    ...
    volumes:
      ...
      - /path/to/certs:/opt/bitnami/postgresql/certs
  ...
```

或者,您也可以在自定义配置文件中提供此配置。

配置文件

该图像在 中查找postgresql.conf文件/opt/bitnami/postgresql/conf/。您可以在以下位置安装卷/bitnami/postgresql/conf/并复制/编辑. 如果该目录为空,则默认配置将被填充到该目录中。postgresql.conf/path/to/postgresql-persistence/conf/conf/

/path/to/postgresql-persistence/conf/
└── postgresql.conf

0 directories, 1 file

由于 postgresql 映像是非 root 的,因此您需要为主机中的挂载目录设置适当的权限:

sudo chown 1001:1001 /path/to/postgresql-persistence/conf/
第 1 步:运行 postgresql 映像

运行 postgresql 映像,从主机安装目录。

docker run --name postgresql \
    -v /path/to/postgresql-persistence/conf/:/bitnami/postgresql/conf/ \
    registry.cn-hangzhou.aliyuncs.com/qingcloudtech/postgresql:latest

或使用 docker compose:

version: '2'

services:
  postgresql:
    image: 'registry.cn-hangzhou.aliyuncs.com/qingcloudtech/postgresql:latest'
    ports:
      - '5432:5432'
    volumes:
      - /path/to/postgresql-persistence/conf/:/bitnami/postgresql/conf/
第 2 步:编辑配置

使用您喜欢的编辑器编辑主机上的配置。

vi /path/to/postgresql-persistence/conf/postgresql.conf
第三步:重启postgresql

更改配置后,重新启动 postgresql 容器以使更改生效。

docker restart postgresql

或使用 docker compose:

docker-compose restart postgresql

有关配置选项的完整列表,请参阅服务器配置手册。

允许从默认文件以外的文件加载设置postgresql.conf

除了使用自定义之外postgresql.conf,您还可以包含.conf以conf.d目录结尾的文件/bitnami/postgresql/conf/。为此,默认postgresql.conf包含以下部分:

##------------------------------------------------------------------------------
## config file includes
##------------------------------------------------------------------------------

## these options allow settings to be loaded from files other than the
## default postgresql.conf.

include_dir = 'conf.d'  # include files ending in '.conf' from directory 'conf.d'

在您的主机中,您应该在目录下创建扩展配置文件conf.d:

mkdir -p /path/to/postgresql-persistence/conf/conf.d/
vi /path/to/postgresql-persistence/conf/conf.d/extended.conf

如果您使用自定义的postgresql.conf,您应该在配置文件中创建(或取消注释)上述部分,在这种情况下,/path/to/postgresql-persistence/conf/结构应该类似于

/path/to/postgresql-persistence/conf/
├── conf.d
│   └── extended.conf
└── postgresql.conf

1 directory, 2 files

指定 initdb 参数

使用以下环境变量可以轻松指定额外的 initdb 参数:

  • postgresql_initdb_args:指定 initdb 命令的额外参数。没有默认值。
  • postgresql_initdb_wal_dir:定义事务日志的自定义位置。没有默认值。
docker run --name postgresql \
  -e postgresql_initdb_args="--data-checksums" \
  -e postgresql_initdb_wal_dir="/bitnami/waldir" \
  registry.cn-hangzhou.aliyuncs.com/qingcloudtech/postgresql:latest

或者通过修改docker-compose.yml文件:

services:
  postgresql:
  ...
    environment:
      - postgresql_initdb_args=--data-checksums
      - postgresql_initdb_wal_dir=/bitnami/waldir
  ...

停止设置

您可以使用以下命令控制初始化过程中用于停止 postgresql 的参数:

  • postgresql_pgctltimeout这将设置命令的超时pg_ctl。
  • postgresql_shutdown_mode这将指示所使用的关闭模式。

安装额外的语言环境

dockerfile 提供了两个参数来在构建时配置额外的区域设置:

  • with_all_locales:启用所有支持的区域设置。默认值:否
  • extra_locales:要启用的额外区域设置的逗号分隔列表。无默认值
    例如,要构建支持区域设置的映像es_es.utf-8 utf-8,您可以将以下参数添加到构建命令中:
docker build --build-arg extra_locales="es_es.utf-8 utf-8" ...

环境变量别名

容器允许两组不同的环境变量。请参阅下表中的环境变量别名列表:

环境变量别名
postgresql_usernamepostgres_user
postgresql_databasepostgres_db
postgresql_passwordpostgres_password
postgresql_password_filepostgres_password_file
postgresql_postgres_passwordpostgres_postgres_password
postgresql_postgres_password_filepostgres_postgres_password_file
postgresql_port_numberpostgres_port_number
postgresql_initdb_argspostgres_initdb_args
postgresql_initdb_wal_dirpostgres_initdb_wal_dir
postgresql_data_dirpgdata
postgresql_replication_userpostgres_replication_user
postgresql_replication_modepostgres_replication_mode
postgresql_replication_passwordpostgres_replication_password
postgresql_replication_password_filepostgres_replication_password_file
postgresql_cluster_app_namepostgres_cluster_app_name
postgresql_master_hostpostgres_master_host
postgresql_master_port_numberpostgres_master_port_number
postgresql_num_synchronous_replicaspostgres_num_synchronous_replicas
postgresql_synchronous_commit_modepostgres_synchronous_commit_mode
postgresql_shutdown_modepostgres_shutdown_mode
alter database postgres_database owner to postgres_user;

可以更改 postgresql 用于执行 init 脚本的用户。为此,请使用以下环境变量:

环境变量描述
postgresql_initscripts_username将用于执行初始化脚本的用户
postgresql_initscripts_passwordpostgresql_initscript_username 中指定的用户的密码

默认 toast 压缩

默认的 toast 压缩是pglz,但您可以通过将环境变量设置postgres_default_compression为所需的值来修改它。例如:postgres_default_compression=‘lz4’。

日志

docker logs postgresql

或使用 docker compose:

docker-compose logs postgresql

你可以通过轻云uc部署工具直接安装部署,也可以手动按如下文档操作,该项目已经全面开源,可以从如下环境获取

开源地址: https://gitee.com/qingplus/qingcloud-platform

qinghub postgresql部署手册

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com