Skip to content

Commit

Permalink
Add support for pgbouncer (#62)
Browse files Browse the repository at this point in the history
* Add support for pgbouncer

* restart pgbouncer on config change

Co-authored-by: Colin Copeland <copelco@caktusgroup.com>

* support k8s_pgbouncer_replicas

Co-authored-by: Ronard <ronardcaktus@users.noreply.github.com>
Co-authored-by: Joel Sleppy <Jdsleppy@users.noreply.github.com>

---------

Co-authored-by: Colin Copeland <copelco@caktusgroup.com>
Co-authored-by: Ronard <ronardcaktus@users.noreply.github.com>
Co-authored-by: Joel Sleppy <Jdsleppy@users.noreply.github.com>
  • Loading branch information
4 people authored Sep 27, 2023
1 parent 3b63855 commit 42c0168
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
16 changes: 16 additions & 0 deletions defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,20 @@ k8s_redis_service_type: ClusterIP
# load balancer (if suppported by the provider):
# k8s_redis_load_balancer_ip: w.x.y.z

k8s_pgbouncer_enabled: false
k8s_pgbouncer_repo: "edoburu/pgbouncer"
k8s_pgbouncer_version: "1.18.0"
k8s_pgbouncer_replicas: 1
k8s_pgbouncer_service_type: ClusterIP
# If service_type is LoadBalancer, you can optionally assign a fixed IP for your
# load balancer (if suppported by the provider):
# k8s_pgbouncer_load_balancer_ip: w.x.y.z
# To use pgbouncer, you must define a k8s_pgbouncer_environment variable with
# at minimum the DATABASE_URL and any other settings you wish to customize.
# See: https://hub.docker.com/r/edoburu/pgbouncer/
# k8s_pgbouncer_environment:
# DATABASE_URL: postgres://...

k8s_elasticsearch_enabled: false
k8s_elasticsearch_cluster_name: "app-elasticsearch-cluster"
k8s_elasticsearch_version: "7.5.2"
Expand Down Expand Up @@ -246,6 +260,8 @@ k8s_templates:
state: "{{ k8s_dockerconfigjson | ternary('present', 'absent') }}"
- name: redis.yaml.j2
state: "{{ k8s_redis_enabled | ternary('present', 'absent') }}"
- name: pgbouncer.yaml.j2
state: "{{ k8s_pgbouncer_enabled | ternary('present', 'absent') }}"
- name: elasticsearch.yaml.j2
state: "{{ k8s_elasticsearch_enabled | ternary('present', 'absent') }}"
- name: memcached.yaml.j2
Expand Down
77 changes: 77 additions & 0 deletions templates/pgbouncer.yaml.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
apiVersion: v1
kind: Secret
metadata:
name: "pgbouncer-secrets"
labels:
app: "pgbouncer"
namespace: "{{ k8s_namespace }}"
type: Opaque
{% if k8s_pgbouncer_enabled %}
{# fail loudly if k8s_pgbouncer_environment not defined when enabled (see defaults/main.yml) #}
stringData: {{ k8s_pgbouncer_environment | to_json }}
{% else %}
stringData: {{ k8s_pgbouncer_environment | default({}) | to_json }}
{% endif %}
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: "pgbouncer"
namespace: "{{ k8s_namespace }}"
spec:
selector:
matchLabels:
app: "pgbouncer"
replicas: {{ k8s_pgbouncer_replicas }}
template:
metadata:
labels:
app: "pgbouncer"
spec:
affinity:
podAntiAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 100
podAffinityTerm:
labelSelector:
matchExpressions:
- key: app
operator: In
values:
- pgbouncer
topologyKey: kubernetes.io/hostname
containers:
- name: "pgbouncer"
image: "{{ k8s_pgbouncer_repo }}:{{ k8s_pgbouncer_version }}"
imagePullPolicy: IfNotPresent
env:
- name: GET_HOSTS_FROM
value: dns
- name: PGBOUNCER_SECRETS_HASH
value: "{{ k8s_pgbouncer_environment | default({}) | to_json | hash('sha256') }}"
envFrom:
- secretRef:
name: "pgbouncer-secrets"
ports:
- containerPort: 5432
---
apiVersion: v1
kind: Service
metadata:
name: "pgbouncer"
labels:
app: "pgbouncer"
namespace: "{{ k8s_namespace }}"
spec:
type: "{{ k8s_pgbouncer_service_type }}"
{% if k8s_pgbouncer_load_balancer_ip is defined %}
loadBalancerIP: "{{ k8s_pgbouncer_load_balancer_ip }}"
{% endif %}
ports:
- protocol: TCP
# Where other things in the cluster should try to connect to our application
port: 5432
# Where our application is listening:
targetPort: 5432
selector:
app: "pgbouncer"

0 comments on commit 42c0168

Please sign in to comment.