Wednesday, January 31, 2024

How to Install PostgreSQL Database on Kubernetes (Part 1)


PostgreSQL, commonly known as Postgres, is a robust open-source database widely used in enterprise production environments. In this article, we will explore how to install PostgreSQL in a High-Availability (HA) configuration on a Kubernetes database. This setup ensures resilience and reliability, making it suitable for demanding applications. If you've been following our series of articles, you should already have a 3-node Kubernetes cluster with an NFS server, all running on the Ubuntu 22.04 Linux Operating System.


What We Will Create

Unlike a typical single-node installation, our High Availability (HA) deployment will consist of two copies of PostgreSQL, each with its own storage. One copy will serve as the Master, handling read/write transactions, while the other, known as the Replica, will be read-only. The Master keeps the Replica in sync by sending updates, and in the event of Master failure, the Replica automatically becomes the new Master through a process known as failover.


Three Kubernetes Services will be established:

  • rw - for read and write transactions through the Master
  • ro - for read-only transactions from the Replicas
  • r - for read-only transactions from any copy


These services enable microservices to connect based on their read-and-write requirements, and any Master failure seamlessly updates the RW service to point to the new Master without disrupting the microservices.


Setting Up with CloudNativePG Kubernetes Operator

To simplify the deployment and management of PostgreSQL in Kubernetes, we'll use the CloudNativePG Kubernetes operator. This operator automates various operational tasks, including deployment, upgrades, backups, etc.


Installing Krew:

The CloudNativePG deployment relies on a cnpg plugin extension to kubectl, controlled by the krew utility. Here's how to install krew on Ubuntu:

(

  set -x; cd "$(mktemp -d)" &&

  OS="$(uname | tr '[:upper:]' '[:lower:]')" &&

  ARCH="$(uname -m | sed -e 's/x86_64/amd64/' -e 's/\(arm\)\(64\)\?.*/\1\2/' -e 's/aarch64$/arm64/')" &&

  KREW="krew-${OS}_${ARCH}" &&

  curl -fsSLO "https://github.com/kubernetes-sigs/krew/releases/latest/download/${KREW}.tar.gz" &&

  tar zxvf "${KREW}.tar.gz" &&

  ./"${KREW}" install krew

)

After installation, add the following to your .bashrc file to include krew in your path:

export PATH="${KREW_ROOT:-$HOME/.krew}/bin:$PATH"

Reload with:

source .bashrc

Verify the installation with:

kubectl krew

If installed correctly, it should provide information about krew.

Conclusion

By following this guide, you'll be well on your way to setting up a High-Availability PostgreSQL deployment on Kubernetes using the CloudNativePG Kubernetes operator. This configuration enhances the reliability and resilience of your PostgreSQL database, ensuring uninterrupted operations even in the face of potential failures.

0 comments:

Post a Comment