<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Alauddin Al Azad</title>
    <description>The latest articles on DEV Community by Alauddin Al Azad (@azad_cse_aust).</description>
    <link>https://dev.to/azad_cse_aust</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F65181%2Fb6204b3c-5f64-4a23-a443-c63aff3f9273.jpeg</url>
      <title>DEV Community: Alauddin Al Azad</title>
      <link>https://dev.to/azad_cse_aust</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/azad_cse_aust"/>
    <language>en</language>
    <item>
      <title>Scaling PostgreSQL Cloud SQL Connections w/ PgBouncer &amp; Kubernetes</title>
      <dc:creator>Alauddin Al Azad</dc:creator>
      <pubDate>Mon, 24 Oct 2022 06:43:03 +0000</pubDate>
      <link>https://dev.to/azad_cse_aust/scaling-postgresql-cloud-sql-connections-w-pgbouncer-kubernetes-glh</link>
      <guid>https://dev.to/azad_cse_aust/scaling-postgresql-cloud-sql-connections-w-pgbouncer-kubernetes-glh</guid>
      <description>&lt;p&gt;We have an &lt;a href="https://portal.ellingson.app/"&gt;ag tech service&lt;/a&gt; running on GCP and we have a microservice that we do maintain consisting of Cloud SQL, Cloud Run, Cloud Functions. When we are developing and scaling our services, from the beginning we have been facing issues with Cloud SQL, mostly connection limitations. We have one Cloud SQL instance and as different services interact with DB, we are looking for a cheap solution that is scalable and which will act as a global pooler. &lt;/p&gt;

&lt;p&gt;From that point, I have found an exciting solution that I have read in this  3 part article from &lt;a href="https://medium.com/futuretech-industries"&gt;FutureTech Industries&lt;/a&gt; about using Helm, Kubernetes, PgBouncer, and CloudSQL to drastically increase the connections a Postgresql DB can handle. Although it was very informative, as I was new to Kubernetes and not extremely versed in Helm and Kubernetes. So I chose a simpler solution with only Kubernetes.&lt;/p&gt;

&lt;h3&gt;
  
  
  So, what is PgBouncer actually?
&lt;/h3&gt;

&lt;p&gt;PgBouncer is a lightweight connection pool manager for Greenplum and PostgreSQL. PgBouncer maintains a pool for connections for each database and user combination. PgBouncer either creates a new database connection for a client or reuses an existing connection for the same user and database.&lt;br&gt;
Source: &lt;a href="https://www.pgbouncer.org/"&gt;https://www.pgbouncer.org/&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Setup Cloud SQL
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;You will need a Cloud SQL instance (postgresql). This is easy to create as gcp has tons of informations regarding this.&lt;/li&gt;
&lt;li&gt;Creating Cloud SQL Instance (Be sure to place it in the same region as your Kubernetes Cluster!)&lt;/li&gt;
&lt;li&gt;Create a DB User for PgBouncer&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Create a kubernetes cluster
&lt;/h2&gt;

&lt;p&gt;Go to kubernetes engine and create and select "Autopilot: Google manages your cluster (Recommended)&lt;br&gt;
" and you are good to go as Autopilot mode will take care of everything.&lt;/p&gt;

&lt;h2&gt;
  
  
  Connecting to Kubernetes Cluster that you have created
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;gcloud container clusters get-credentials YOUR_CLUSTER_NAME --region us-central1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now you can access your kubernetes cluster with kubectl from localhost.&lt;/p&gt;

&lt;h2&gt;
  
  
  Let's get started!
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Create namespace
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;kubectl create namespace pgb-namespace 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Set namespace
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;kubectl config set-context --current --namespace=pgb-namespace
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Storing service account json file as secret. Place your json in root folder, rename your service account as  &lt;strong&gt;postgres-sql-credential.json&lt;/strong&gt; and run this command.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;kubectl create secret generic cloudsql-instance-credentials \
   --from-file=credentials.json=postgres-sql-credential.json
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Create &lt;strong&gt;pgbouncer.ini&lt;/strong&gt; file and paste the content there.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[databases]
* = host=localhost port=5432 user=postgres password=YOUR_PASSWORD

[pgbouncer]
listen_port=6432
listen_addr=0.0.0.0
auth_file=/opt/bitnami/pgbouncer/conf/userlist.txt
auth_type=md5
pidfile=/opt/bitnami/pgbouncer/tmp/pgbouncer.pid
logfile=/opt/bitnami/pgbouncer/logs/pgbouncer.log
admin_users=postgres
client_tls_sslmode=disable
server_tls_sslmode=disable
pool_mode=transaction
server_reset_query = DISCARD ALL
ignore_startup_parameters = extra_float_digits
application_name_add_host = 1
max_client_conn = 10000
autodb_idle_timeout = 3600
default_pool_size = 20
max_db_connections = 80
max_user_connections = 80

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Explanation: &lt;/p&gt;

&lt;p&gt;Suppose your database has maximum 100 connections. Leaving 20% connection for super user, so set max_db_connections = 80 and max_user_connections = 80&lt;/p&gt;

&lt;p&gt;max_client_conn = 10000 , pgbouncer can handle 10000 incoming connections!&lt;br&gt;
default_pool_size = 20 , suppose you have 4 databases in cloud sql , so 4 * 20 = max_db_connection&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;Create &lt;strong&gt;userlist.txt&lt;/strong&gt; file and paste this content,
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;md545f2603610af569b6155c45067268c6b
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Explanation: This md5 is made from username and password combination.&lt;br&gt;
For this md5, &lt;strong&gt;username&lt;/strong&gt;: &lt;strong&gt;admin&lt;/strong&gt; and &lt;strong&gt;password&lt;/strong&gt;: &lt;strong&gt;1234&lt;/strong&gt;.&lt;br&gt;
 &lt;a href="https://www.pgbouncer.org/config.html#authentication-file-format"&gt;Follow this doc to make your own&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;Now store this newly created pgbouncer.ini and userlist.txt in secret. Make sure to place both pgbouncer.ini and userlist.txt file in root folder.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;kubectl create secret generic pgb-configuration \
   --from-file=pgbouncer.ini --from-file=userlist.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Storing your db user and pass as secret.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;kubectl create secret generic db-credentials \
   --from-literal=username=postgres --from-literal=password=YOUR_DB_PASS
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Create &lt;strong&gt;kube_pgb_proxy.yaml&lt;/strong&gt; file and paste this content
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;apiVersion: apps/v1
kind: Deployment
metadata:
  name: pgproxy
  namespace: pgb-namespace
spec:
  replicas: 1
  selector:
    matchLabels:
      app: pgproxy
  revisionHistoryLimit: 1
  strategy:
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: pgproxy
        tier: backend
    spec:
      securityContext:
        runAsUser: 0
        runAsNonRoot: false
      containers:
        - name: cloudsql-proxy
          resources:
            requests:
              memory: "500Mi"
              cpu: "500m"
              ephemeral-storage: "1Gi"
            limits:
              memory: "1000Mi"
              cpu: "1000m"
              ephemeral-storage: "1Gi"
          image: gcr.io/cloudsql-docker/gce-proxy:1.11
          command:
            [
              "/cloud_sql_proxy",
              "--dir=/cloudsql",
              "-instances=**YOUR_INSTANCE_NAME_STRING**=tcp:5432",
              "-credential_file=/secrets/cloudsql/credentials.json",
            ]
          volumeMounts:
            - name: cloudsql-instance-credentials
              mountPath: /secrets/cloudsql
              readOnly: true
            - name: cloudsql
              mountPath: /cloudsql
        - name: pgproxy
          env:
            - name: POSTGRESQL_HOST
              value: localhost
            - name: POSTGRESQL_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: db-credentials
                  key: password
            - name: POSTGRESQL_USERNAME
              valueFrom:
                secretKeyRef:
                  name: db-credentials
                  key: username
          volumeMounts:
            - name: pgb-configuration
              mountPath: /bitnami/pgbouncer/conf
              readOnly: true
          image: bitnami/pgbouncer:latest
          lifecycle:
            preStop:
              exec:
                command:
                  - /bin/sh
                  - -c
                  - killall -INT pgbouncer &amp;amp;&amp;amp; sleep 120
          ports:
            - containerPort: 6432
      volumes:
        - name: cloudsql-instance-credentials
          secret:
            secretName: cloudsql-instance-credentials
        - name: pgb-configuration
          secret:
            secretName: pgb-configuration
        - name: cloudsql
          emptyDir:

---
apiVersion: v1
kind: Service
metadata:
  name: pgproxy
  namespace: pgb-namespace
  annotations:
    cloud.google.com/load-balancer-type: "Internal"
spec:
  type: LoadBalancer
  selector:
    app: pgproxy
  ports:
    - port: 6432
      targetPort: 6432

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Leave all the values as it is, except the db string. You will find this in GCP cloud sql &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--zIq3QaC2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1666551563963/uNOw0DgvN.png%2520align%3D%2522left%2522" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zIq3QaC2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1666551563963/uNOw0DgvN.png%2520align%3D%2522left%2522" alt="Screenshot 2022-10-24 at 12.58.43 AM.png" width="" height=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Now apply the created yaml file.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;kubectl apply -f kube_pgb_proxy.yaml
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;If everything works well, you will find 1 workloads and 1 service in your kubernetes engine.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--oWGbcIun--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gfepmf1eo304ov92cthg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--oWGbcIun--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gfepmf1eo304ov92cthg.png" alt="Kubernetes workloads" width="880" height="305"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Vi79RPM0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b7nkuvmgjhsarjjm7h3d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Vi79RPM0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b7nkuvmgjhsarjjm7h3d.png" alt="Kubernetes services" width="880" height="269"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Congrats! Now you have your pgbouncer server running with the &lt;/p&gt;

&lt;p&gt;db_host: 10.148.0.34&lt;/p&gt;

&lt;p&gt;db_port: 6432&lt;/p&gt;

&lt;p&gt;db_username: admin&lt;/p&gt;

&lt;p&gt;db_pass: 1234&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;Now you have a running pgbouncer cluster, lets create horizontal pod autoscaler. Create &lt;strong&gt;hpa.yaml&lt;/strong&gt; file and paste this content there.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
  name: pgb-hpa
  namespace: pgb-namespace
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: pgproxy
  minReplicas: 1
  maxReplicas: 10
  targetCPUUtilizationPercentage: 75

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Explanation:&lt;/p&gt;

&lt;p&gt;Here minReplicas 1 and maxReplicas 10 and targetCPUUtilizationPercentage 75. So when your pod will exceeds 75% cpu utilization, it will create another pod to meet the on demand connections.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt; Now execute this command to apply.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;kubectl apply -f hpa.yaml
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;I hope this article help you!&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
