Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit5d8abcd

Browse files
committed
Add pgbouncer
1 parent1d6d78e commit5d8abcd

File tree

2 files changed

+116
-1
lines changed

2 files changed

+116
-1
lines changed

‎README.md

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ PostgreSQL uses environment variables for configuration. The most important one
6969

7070
```bash
7171
$# create secret from literal
72-
$ kubectl create secret generic postgresql-secrets --from-literal=POSTGRES_PASSWORD=tes6Aev8
72+
$ kubectl create secret generic postgresql-secrets \
73+
--from-literal=POSTGRES_PASSWORD=tes6Aev8
7374

7475
$# describe secret
7576
$ kubectl describe secrets postgresql-secrets
@@ -193,3 +194,67 @@ Address: 172.17.0.3
193194
/ # exit
194195
pod "busybox" deleted
195196
```
197+
198+
## Connection pooling
199+
200+
> [Postgres connection pool on Kubernetes in 1 minute](https://jmrobles.medium.com/postgres-connection-pool-on-kubernetes-in-1-minute-80b8020315ef)
201+
202+
First we create a _Config Map_ for the following values:
203+
204+
- `DB_HOST`: DNS hostname of our PostgreSQL service
205+
- `DB_USER`: PostgreSQL database user
206+
207+
`DB_PASSWORD` will be set using the previously created secret. `POOL_MODE` is set to `transaction` and `SERVER_RESET_QUERY` to `DISCARD ALL` by default in the respective deployment manifest.
208+
209+
```bash
210+
$ kubectl create configmap pgbouncer-configs \
211+
--from-literal=DB_HOST=postgresql-svc.postgres.svc.cluster.local \
212+
--from-literal=DB_USER=postgres
213+
```
214+
215+
Now we can apply our deployment for [PgBouncer](https://www.pgbouncer.org/) that is based on this [Docker image](https://hub.docker.com/r/edoburu/pgbouncer/) for PgBouncer 1.15.0.
216+
217+
```bash
218+
$ kubectl apply -f pgbouncer.yaml
219+
deployment.apps/pgbouncer created
220+
221+
$ # now we create a service for the deployment
222+
$ kubectl expose deployment pgbouncer --name=pgbouncer-svc
223+
service/pgbouncer exposed
224+
```
225+
226+
Let’s check the stats.
227+
228+
```bash
229+
$ kubectl run -it --rm pg-psql --image=postgres:13.3 --restart=Never \
230+
--env="PGPASSWORD=tes6Aev8" -- \
231+
psql -h pgbouncer-svc.postgres.svc.cluster.local -U postgres -d pgbouncer
232+
If you don't see acommand prompt, try pressing enter.
233+
pgbouncer=# \x
234+
Expanded display is on.
235+
236+
pgbouncer=# SHOW SERVERS;
237+
-[ RECORD 1 ]+------------------------
238+
type| S
239+
user| postgres
240+
database| postgres
241+
state| used
242+
addr| 172.17.0.5
243+
port| 5432
244+
local_addr| 172.17.0.6
245+
local_port| 59960
246+
connect_time| 2021-08-04 11:25:19 UTC
247+
request_time| 2021-08-04 11:25:59 UTC
248+
wait| 0
249+
wait_us| 0
250+
close_needed| 0
251+
ptr| 0x7fa02cb54100
252+
link|
253+
remote_pid| 183
254+
tls|
255+
256+
pgbouncer=# \q
257+
pod"pg-psql" deleted
258+
```
259+
260+
As we can so see PgBouncer only detects one server so far. The reason for that is, each server is listening on the same host and port. We need to fix that.

‎pgbouncer.yaml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
apiVersion:apps/v1
2+
kind:Deployment
3+
metadata:
4+
labels:
5+
app:pgbouncer
6+
name:pgbouncer
7+
spec:
8+
replicas:1
9+
selector:
10+
matchLabels:
11+
app:pgbouncer
12+
template:
13+
metadata:
14+
labels:
15+
app:pgbouncer
16+
spec:
17+
containers:
18+
-env:
19+
-name:DB_HOST
20+
valueFrom:
21+
configMapKeyRef:
22+
key:DB_HOST
23+
name:pgbouncer-configs
24+
-name:DB_PASSWORD
25+
valueFrom:
26+
secretKeyRef:
27+
key:POSTGRES_PASSWORD
28+
name:postgresql-secrets
29+
-name:DB_USER
30+
value:postgres
31+
-name:POOL_MODE
32+
value:transaction
33+
-name:SERVER_RESET_QUERY
34+
value:DISCARD ALL
35+
image:edoburu/pgbouncer:1.15.0
36+
lifecycle:
37+
preStop:
38+
exec:
39+
command:
40+
-/bin/sh
41+
--c
42+
-killall -INT pgbouncer && sleep 120
43+
name:pgbouncer
44+
ports:
45+
-containerPort:5432
46+
securityContext:
47+
allowPrivilegeEscalation:false
48+
capabilities:
49+
drop:
50+
-all

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp