I have a django app running on kubernetes with postgrsql / pgbouncer
I encounter the following error on some requests when I run 2 Django replicas (this doesn't seem to happen if I only set 1 replica)
I only have one database
Django side
django.db.utils.OperationalError: ERROR: no more connections allowed (max_client_conn)
PGBouncer side
1 WARNING C-0x55d76aeb6330: (nodb)/(nouser)@xx.xx.xx.xx:37002 pooler error: no more connections allowed (max_client_conn)
I have the following settings
postgres
containers:
- name: acapela-cloud-database
image: postgres:12.0
imagePullPolicy: Always
ports:
- containerPort: 5432
args:
- postgres
- -c
- max_connections=400
SHOW max_connections;
max_connections
-----------------
400
(1 row)
pgbouncer yaml file (kubernetes)
apiVersion: apps/v1
kind: Deployment
metadata:
name: pgbouncer
labels:
app: pgbouncer
spec:
selector:
matchLabels:
app: pgbouncer
template:
metadata:
labels:
app: pgbouncer
spec:
containers:
- name: pgbouncer
image: edoburu/pgbouncer:1.9.0
imagePullPolicy: Always
ports:
- containerPort: 5432
env:
- name: DB_HOST
value: "database"
- name: DB_PORT
value: "5432"
- name: DB_USER
value: ""
- name: DB_PASSWORD
value: ""
- name: DB_DATABSE
value: ""
- name: PGBOUNCER_LISTEN_PORT
value: "6432"
- name: PGBOUNCER_MAX_CLIENT_CONN
value: "800"
- name: PGBOUNCER_DEFAULT_POOL_SIZE
value: "400"
- name: POOL_MODE
value: transaction
- name: SERVER_RESET_QUERY
value: DISCARD ALL
livenessProbe:
tcpSocket:
port: 5432
periodSeconds: 60
lifecycle:
preStop:
exec:
# Allow existing queries clients to complete within 120 seconds
command: ["/bin/sh", "-c", "killall -INT pgbouncer && sleep 120"]
securityContext:
allowPrivilegeEscalation: false
capabilities:
drop: ['all']
---
apiVersion: v1
kind: Service
metadata:
name: pgbouncer
labels:
app: pgbouncer
spec:
type: ClusterIP
ports:
- port: 5432
targetPort: 5432
protocol: TCP
name: pgbouncer
selector:
app: pgbouncer
I suppose this comes for these settings but cannot figure out what to set.
Can someone give me what values would work ?
Once fixed and with this architecture (Django + PGBouncer + Postgrsql) could I launch 5/10/15... replicas and the database/bouncer will handle it?