<?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>Forem: gangaprasad</title>
    <description>The latest articles on Forem by gangaprasad (@gangaprasad_07bcb0289de5d).</description>
    <link>https://forem.com/gangaprasad_07bcb0289de5d</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%2F1586032%2F99ca9c84-04dd-4521-8f5b-770c76196851.jpg</url>
      <title>Forem: gangaprasad</title>
      <link>https://forem.com/gangaprasad_07bcb0289de5d</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/gangaprasad_07bcb0289de5d"/>
    <language>en</language>
    <item>
      <title>Deploy Postgres on any Kubernetes using CloudNativePG</title>
      <dc:creator>gangaprasad</dc:creator>
      <pubDate>Thu, 06 Jun 2024 14:47:40 +0000</pubDate>
      <link>https://forem.com/gangaprasad_07bcb0289de5d/deploy-postgres-on-any-kubernetes-using-cloudnativepg-3bn4</link>
      <guid>https://forem.com/gangaprasad_07bcb0289de5d/deploy-postgres-on-any-kubernetes-using-cloudnativepg-3bn4</guid>
      <description>&lt;p&gt;There are many ways to setup Postgres in Kubernetes, but all methods will not solve all problems, here are some.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Backup data to object storage&lt;/li&gt;
&lt;li&gt;On-demand backup&lt;/li&gt;
&lt;li&gt;Schedule backup&lt;/li&gt;
&lt;li&gt;Point-in-time recovery (PITR)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The best method to counter these problems is &lt;a href="https://cloudnative-pg.io/"&gt;CloudNativePG&lt;/a&gt; operator, this operator manages &lt;a href="https://www.postgresql.org/"&gt;PostgreSQL&lt;/a&gt; workloads on any supported &lt;a href="https://kubernetes.io/"&gt;Kubernetes&lt;/a&gt; cluster.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;pre-requisite&lt;/strong&gt;: any running Kubernetes cluster&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step-1&lt;/strong&gt;: &lt;br&gt;
Install CloudNativePG operator on your running Kubernetes, best way to deploy using &lt;a href="https://helm.sh/"&gt;Helm&lt;/a&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;helm repo add cnpg https://cloudnative-pg.github.io/charts

helm upgrade - install cnpg \
 - namespace cnpg-system \
 - create-namespace \
 cnpg/cloudnative-pg
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will install cnpg operator in cnpg-system namespace in your Kubernetes cluster, to check the pod is running or not run below command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;kubectl get pods -l app.kubernetes.io/name=cloudnative-pg -n cnpg-system
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step-2&lt;/strong&gt;:&lt;br&gt;
cnpg will also install new Kubernetes resource called Cluster representing a PostgreSQL cluster made up of a single primary and an optional number of replicas that co-exist in a chosen Kubernetes namespace.&lt;/p&gt;

&lt;p&gt;Once the operator is running, now we have to install Postgres in Kubernetes cluster using resource called Cluster created by cnpg.&lt;br&gt;
we use the manifest below cluster.yaml to create postgres cluster&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;apiVersion: v1
data:
  password: VHhWZVE0bk44MlNTaVlIb3N3cU9VUlp2UURhTDRLcE5FbHNDRUVlOWJ3RHhNZDczS2NrSWVYelM1Y1U2TGlDMg==
  username: YXBw
kind: Secret
metadata:
  name: cluster-example-app-user
type: kubernetes.io/basic-auth
---
apiVersion: v1
data:
  password: dU4zaTFIaDBiWWJDYzRUeVZBYWNCaG1TemdxdHpxeG1PVmpBbjBRSUNoc0pyU211OVBZMmZ3MnE4RUtLTHBaOQ==
  username: cG9zdGdyZXM=
kind: Secret
metadata:
  name: cluster-example-superuser
type: kubernetes.io/basic-auth
---
apiVersion: v1
kind: Secret
metadata:
  name: backup-creds
data:
  ACCESS_KEY_ID: a2V5X2lk
  ACCESS_SECRET_KEY: c2VjcmV0X2tleQ==
---
apiVersion: postgresql.cnpg.io/v1
kind: Cluster
metadata:
  name: cluster-example-full
spec:
  description: "Example of cluster"
  imageName: ghcr.io/cloudnative-pg/postgresql:16.2
  instances: 3
  startDelay: 300
  stopDelay: 300
  primaryUpdateStrategy: unsupervised

  postgresql:
    parameters:
      shared_buffers: 256MB
      pg_stat_statements.max: '10000'
      pg_stat_statements.track: all
      auto_explain.log_min_duration: '10s'

  bootstrap:
    initdb:
      database: app
      owner: app
      secret:
        name: cluster-example-app-user

  enableSuperuserAccess: true
  superuserSecret:
    name: cluster-example-superuser

  storage:
    storageClass: standard
    size: 1Gi

  backup:
    barmanObjectStore:
      destinationPath: s3://cluster-example-full-backup/
      endpointURL: http://custom-endpoint:1234
      s3Credentials:
        accessKeyId:
          name: backup-creds
          key: ACCESS_KEY_ID
        secretAccessKey:
          name: backup-creds
          key: ACCESS_SECRET_KEY
      wal:
        compression: gzip
        encryption: AES256
      data:
        compression: gzip
        encryption: AES256
        immediateCheckpoint: false
        jobs: 2
    retentionPolicy: "30d"

  resources:
    requests:
      memory: "512Mi"
      cpu: "1"
    limits:
      memory: "1Gi"
      cpu: "2"

  affinity:
    enablePodAntiAffinity: true
    topologyKey: failure-domain.beta.kubernetes.io/zone

  nodeMaintenanceWindow:
    inProgress: false
    reusePVC: false

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

&lt;/div&gt;



&lt;p&gt;In the above manifest we are creating two secrets because one secret is for initial database and another secret is for superuser access, you can read more about roles in Postgres &lt;a href="https://www.postgresql.org/docs/current/database-roles.html"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The Third secret we created to access object store, here we are using AWS S3.&lt;/p&gt;

&lt;p&gt;The supported object storages can be found &lt;a href="https://cloudnative-pg.io/documentation/1.23/appendixes/object_stores/"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now apply the manifest in your Kubernetes cluster&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;kubectl create -f cluster.yaml -n namespace
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now you can see postgres pods are running in your Kubernetes cluster&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;kubectl get pods -n namespace
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can get postgres cluster by&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;kubectl get cluster -n namespace
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the next tutorial we will configure On-demand backup, schedule backup and recovery from existing data&lt;/p&gt;

</description>
      <category>kubernetes</category>
      <category>postgres</category>
      <category>devops</category>
    </item>
  </channel>
</rss>
