跳到主要内容

PostgreSQL 连接池

PostgreSQL 是多进程架构,它会为每个用户连接创建一个单独的后端进程,当用户连接数较多时,会占用大量内存,降低数据库的吞吐量和稳定性。为解决 PostgreSQL 连接过多而导致的问题,KubeBlocks 为 PostgreSQL 集群引入了连接池,PgBouncer。

使用 KubeBlocks 创建 PostgreSQL 集群时,会默认安装 PgBouncer。

步骤

  1. 查看 PostgreSQL 集群的状态,确保为 Running

    kubectl get cluster mycluster -n demo
  2. 查看 PostgreSQL 集群详细信息,其中会包含两条连接信息。

    其中 5432 端口用于直接连接数据库主节点,6432 端口用于连接 PgBouncer。

    kubectl get services mycluster-postgresql -n demo
    >
    NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
    mycluster-postgresql ClusterIP 10.97.123.178 <none> 5432/TCP,6432/TCP 39m
  3. 获取 kubectl exec 命令所需的 usernamepassword

    kubectl get secrets -n demo mycluster-conn-credential -o jsonpath='{.data.\username}' | base64 -d
    >
    postgres

    kubectl get secrets -n demo mycluster-conn-credential -o jsonpath='{.data.\password}' | base64 -d
    >
    shgkz4z9
  4. 通过 PgBouncer 连接集群。

    该命令会展示访问集群的方式,默认使用 5432 端口访问集群,可以将端口替换为 6432 通过 PgBouncer 访问集群。

    kubectl -n demo port-forward service/mycluster-postgresql 6432:6432
  5. 打开一个新的终端窗口,执行 psql 命令进行连接。

    Fill the password obtained from step 3 into the PGPASSWORD.

    PGPASSWORD=shgkz4z9 psql -h127.0.0.1 -p 6432 -U postgres postgres
  6. psql 中执行如下命令进行验证。

    如果可以连接至 pgbouncer 库并正确执行 show help 命令,说明已经成功连接 PgBouncer。

    postgres=# \c pgbouncer
    pgbouncer=# show help;
    >
    NOTICE: Console usage
    DETAIL:
    SHOW HELP|CONFIG|DATABASES|POOLS|CLIENTS|SERVERS|USERS|VERSION
    SHOW PEERS|PEER_POOLS
    SHOW FDS|SOCKETS|ACTIVE_SOCKETS|LISTS|MEM|STATE
    SHOW DNS_HOSTS|DNS_ZONES
    SHOW STATS|STATS_TOTALS|STATS_AVERAGES|TOTALS
    SET key = arg
    RELOAD
    PAUSE [<db>]
    RESUME [<db>]
    DISABLE <db>
    ENABLE <db>
    RECONNECT [<db>]
    KILL <db>
    SUSPEND
    SHUTDOWN
    WAIT_CLOSE [<db>]
    SHOW