pgbouncer

by apecloud

Helm chartv1.0.0App 1.25.2Verified publisher

A standalone PgBouncer connection pool for PostgreSQL

About this chart

PgBouncer

This Addon runs PgBouncer 1.25.2 as an independent, stateless KubeBlocks Cluster. Each instance targets one existing PostgreSQL cluster and one backend role:

  • rw: the PostgreSQL primary, through the postgresql Service.
  • ro: PostgreSQL replicas, through an explicitly selected replica ClusterService. Create this service on demand before creating the pool. There is no fallback to the primary when replicas are unavailable.

Create separate PgBouncer clusters for independent read/write and read-only addresses, pool settings, resources, or application workloads. Applications choose an address; PgBouncer does not classify or split SQL statements.

Prerequisites

  • KubeBlocks newer than 1.0.2.
  • PgBouncer Addon 1.0.0.
  • An existing PostgreSQL cluster with a primary component Service and a postgres system account. Read-only pools also require a ClusterService that selects only PostgreSQL replicas; PostgreSQL does not create this service by default.
  • Both clusters in the same Kubernetes namespace. Native KubeBlocks service references cannot resolve another namespace's account credentials.

Create

From this repository, install the Addon and create connection pools:

helm dependency build addons/pgbouncer
helm install kb-addon-pgbouncer addons/pgbouncer --namespace kb-system
helm dependency build addons-cluster/pgbouncer
helm install app-pool-rw addons-cluster/pgbouncer --namespace demo \
  --set postgresql.cluster=pg-cluster --set backend.role=rw
# Create replica access only when a read-only pool is needed.
kubectl apply -f examples/pgbouncer/expose-readonly.yaml
kubectl wait --for=jsonpath='{.status.phase}'=Succeed \
  opsrequest/pg-expose-readonly --namespace demo --timeout=180s
helm install app-pool-ro addons-cluster/pgbouncer --namespace demo \
  --set postgresql.cluster=pg-cluster --set backend.role=ro \
  --set postgresql.service=postgresql-internal-ro

The default is two replicas, each with 0.5 CPU and 0.5 GiB memory. Set replicas, cpu, memory, and requests independently for each pool. PgBouncer has no data volume and does not require a backup policy. Deleting a PgBouncer Cluster does not delete its PostgreSQL backend.

The Cluster chart takes postgresql.cluster, postgresql.namespace, postgresql.component, postgresql.service, and postgresql.port. The service defaults to the postgresql component Service for rw. For ro, set it to the name of an existing replica ClusterService in the backend Cluster specification. The read-only service reference omits service.component; its credential still uses the PostgreSQL component's postgres system account. Keep the referenced service enabled while the pool is in use.

Raw Cluster examples are in examples/pgbouncer.

Connect and authenticate

The internal pool addresses are:

app-pool-rw-pgbouncer.demo.svc:6432
app-pool-ro-pgbouncer.demo.svc:6432

Use the PostgreSQL database name and business account credentials at either address. Create and manage those accounts on PostgreSQL. PgBouncer uses PostgreSQL's postgres account to look up login-capable, non-expired accounts; MD5 and SCRAM password authentication are supported by PgBouncer's md5 auth mode. Backend database permissions remain effective.

The pgbouncer account is reserved for the pool's local management database, also named pgbouncer. Its independent password is generated by KubeBlocks. Monitoring and readiness recovery use this management account, rather than exposing the backend superuser as a pool administrator. Do not create a business account named pgbouncer for use through this pool.

Client-facing TLS is not enabled by this Addon. Backend TLS can be enabled with server_tls_sslmode; for certificate verification, configure component TLS with the PostgreSQL CA and use the Service name covered by its certificate.

Configure

The managed pgbouncer-configuration configuration contains pgbouncer.ini. Supported parameters are:

ParameterDefaultMeaning
pool_modesessionsession, transaction, or statement pooling
max_client_conn500Client connections per PgBouncer instance
default_pool_size20Backend connections per user/database pool
min_pool_size5Minimum retained backend connections per pool
reserve_pool_size5Additional connections available under load
max_db_connections80Backend connections per database and instance; zero is unlimited
max_user_connections80Backend connections per user and instance; zero is unlimited
server_tls_sslmodedisabledisable, require, verify-ca, or verify-full

Use a KubeBlocks Reconfiguring OpsRequest as shown in configure.yaml. Configuration reload uses SIGHUP. Verify SHOW CONFIG on each instance after reconfiguration; some limits affect new connections rather than immediately terminating existing ones. Size the combined pools for the PostgreSQL server's connection budget.

Backend role changes

Readiness checks a connection through the pool every five seconds. A pool is ready only when the sampled backend has its expected role. When it detects a wrong-role backend, it sends RECONNECT to retire backend connections as they are released and leaves the instance unready until a later successful probe. Backend outages do not restart the PgBouncer process; its management endpoint remains available to monitoring.

An endpoint is a routing choice, not an authorization boundary. Readiness samples the postgres database pool; other pools can have different existing connections. RECONNECT does not interrupt occupied sessions or active transactions. In session pooling, a client can remain attached to a replica that has been promoted until that client disconnects. Applications must handle connection failures and reconnect after role changes. Use database privileges for accounts that must never write, and use the read/write endpoint when read-after-write consistency is required.

Backend and management credentials are loaded on process startup. After changing the referenced credential, ensure the pool Pods are restarted and verify authentication with a new connection.

Deploy pgbouncer with OpenTenant

Each tenant gets its own release of this chart, with its own namespace, secrets and domain. The builder below already has pgbouncer on a Hetzner server. Change the host, add apps from the marketplace, then copy the generated files. Providers and prices are samples, and nothing here is sent anywhere.

  1. Pick a serverConnect Hetzner, OVHcloud or AWS. The builder starts on Hetzner, the cheapest sample rate.
  2. Place the chartpgbouncer is pinned at v1.0.0 in opentenant.yaml. Drag more apps from the marketplace onto a rack.
  3. ApplySave opentenant.yaml and main.tf from the console, then run the commands below.
# opentenant.yaml, main.tf and dns.tf come from the builder below
opentenant validate opentenant.yaml
terraform init && terraform apply