Logo

dev-resources.site

for different kinds of informations.

Automating PostgreSQL Clusters: A Guide to Deployment with ArgoCD and CloudNativePG

Published at
12/9/2024
Categories
kubernetes
gitops
argocd
postgres
Author
azaurus
Categories
4 categories in total
kubernetes
open
gitops
open
argocd
open
postgres
open
Author
7 person written this
azaurus
open
Automating PostgreSQL Clusters: A Guide to Deployment with ArgoCD and CloudNativePG

Use case:

To be able to deploy and manage PostgreSQL clusters with GitOps and to provide an auditable and easily understandable system for developers to manage their own Postgres clusters from the same git repo.

Brief description of the tech involved:

GitOps with ArgoCD:

GitOps is a software and deployment approach where the definition and lifecycle for the entire infrastructure and applications are managed through Git repositories, this makes the repo a single source of truth for our infrastructure, and also allows us to use CI/CD practices with our infrastructure. ArgoCD is a GitOps continuous delivery tool for Kubernetes, ArgoCD will listen to our repo and deploy the changes for us to reach our desired state.

Kubernetes Operators:

Operators are extensions to Kubernetes that creates custom resource definitions (CRDs) which are used to manage applications and components of those applications, Operators follow the Kubernetes control loop principle, where a controller will check the current state of its related resources vs the desired state and communicates with the Kube-API server to make those changes. For more detail see: Operator pattern and Controllers

CloudNativePG:

CloudNativePG is a Kubernetes operator for covering the lifecycle of PostgreSQL clusters, it provides many features for deploying and managing high availability database clusters. CloudNativePG is an "Autopilot" capability Operator, which is the highest level of an operator, allowing for:

  • Basic installs
  • Seamless Upgrading
  • Full Lifecycle
  • Deep insights with metrics
  • Horizontal and Vertical scaling + auto-config tuning Read more about the CloudNativePG operator at OperatorHub

For our use case, CloudNativePG provides the controller and CRDs for managing our PostgreSQL clusters.

How the system works:

The CloudNativePG operator will create the CRDs needed for our clusters in our Kubernetes cluster, the main one being Cluster this CRD defines everything for our cluster, an example of a basic cluster would be:

apiVersion: postgresql.cnpg.io/v1
kind: Cluster
metadata:
  name: cluster-example
spec:
  instances: 3

  storage:
    size: 1Gi
Enter fullscreen mode Exit fullscreen mode

However, if we are going to deploy this in a GitOps manner, we will need to build the yaml manifests in our directory which ArgoCD will be monitoring for changes. An example of this will be the pg-clusters/cluster1.yaml manifest, which for example would look like:

pg-clusters/cluster1.yaml:

apiVersion: postgresql.cnpg.io/v1
kind: Cluster
metadata:
  name: generator-cluster1
  namespace: default
spec:
  description: "Generated Cluster 1"
  imageName: ghcr.io/cloudnative-pg/postgresql:15.1
  instances: 1
  startDelay: 10
  stopDelay: 10
  primaryUpdateStrategy: unsupervised

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

  superuserSecret:
    name: cluster-example-superuser

  storage:
    size: "1Gi"
  monitoring:
    enablePodMonitor: true
Enter fullscreen mode Exit fullscreen mode

We will now need to create a new app in ArgoCD, to do this and to make sure it builds the resources from the correct directory (in our example this will need to be the pg-clusters directory), we will provide the following Application manifest:

apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  name: pg-clusters
spec:
  destination:
    name: ''
    namespace: default
    server: 'https://kubernetes.default.svc'
  source:
    path: pg-clusters
    repoURL: 'https://github.com/azaurus1/gitops-pgclusters'
    targetRevision: HEAD
  sources: []
  project: default
Enter fullscreen mode Exit fullscreen mode

Once this Application is deployed into ArgoCD, a sync will begin to reconcile the desired and current states and begin to deploy our Cluster as defined in the earlier manifest.

Image description

The above is an example of the deployed pg-cluster application in the unsynced state, we can build our cluster by syncing the cluster resource, an example of syncing the generator-cluster1 cluster would look like below:

Image description

As we can see, we now have a nice view of all the resources required for our PostgreSQL Cluster that have been provisioned in our cluster, and they match our manifest specifications, including the 1 Instance, 1Gi PersistentVolumeClaim. On top of this, the clusters are all in the same Application and can be easily managed from one view.

Graphical Representation:

Image description

Sources:

Image Credit:

Sunder Muthukumaran

gitops Article's
30 articles in total
Favicon
How to Solve Common Kubernetes Multi-Cluster Deployment Issues
Favicon
Effortless vCluster Management with Sveltos: An Event-Driven Approach
Favicon
Google's Intent-Based Deployment with Prodspec and Annealing
Favicon
5 Best Practices for Multi-Cluster Kubernetes Add-on Management
Favicon
Emerging Trends in Git: GitOps, Monorepos, Distributed Repositories, and AI Integration
Favicon
Automating PostgreSQL Clusters: A Guide to Deployment with ArgoCD and CloudNativePG
Favicon
Advanced Kubernetes Deployment with GitOps: A Hands-On Guide To Terraform, Ansible, ArgoCD And Observability Tools
Favicon
GitOps and FluxCD: Continuous Deployment for Kubernetes on AWS
Favicon
GitOps vs Traditional DevOps: Understanding the Key Differences and Benefits
Favicon
Consistent Deployment Strategies for Kubernetes
Favicon
Why GitOps is Revolutionizing DevOps: A Guide for Agile Teams
Favicon
GitOps: A Strategic Advantage for Automation, Collaboration, and Cost Savings
Favicon
Setting Up Machine Learning Pipelines with GitOps Principles
Favicon
Automating CI/CD Pipelines for Kubernetes with Argo Rollouts, Argo CD, Argo Workflow & Events
Favicon
How Talking to a College Senior taught me Devops/Gitops
Favicon
Leveraging ArgoCD for Kubernetes Applications: Implementation, Use Cases, and Best Practices
Favicon
🗂️ Master SQL Review with GUI 🎛️, GitOps 🖇️ and API 🔌
Favicon
GitOps Across Clusters — How ArgoCD and Kustomize Makes It Simple
Favicon
Portainer + gitops ❤️: A simple way to deploy and manage your self-hosted applications
Favicon
Kargo - promote your application changes in a controlled (GitOps) way!
Favicon
Tekton - A Kubernetes-native CI/CD : Day 46 of 50 days DevOps Tools Series
Favicon
GitOps + ArgoCD: A Perfect Match for Kubernetes Continuous Delivery
Favicon
FluxCD - A lightweight GitOps CD tool: Day 44 of 50 days DevOps Tools Series
Favicon
CI/CD Pipeline for a Next.js Application Using GitHub Actions, ArgoCD, and MicroK8s
Favicon
GitOps Adoption: A Simple Guide to Modern Cloud Management
Favicon
A Production Ready EKS Deployment with GitOps - Part 1 - Introduction
Favicon
Kubernetes Multi-Cluster Management 📦
Favicon
Stop using "GitOps" to sell your products
Favicon
GitOps: ArgoCD vs FluxCD
Favicon
What is GitOps ?

Featured ones: