fbpx
How to Deploy Postgres High Availability on Kubernetes

We will cover the deployment of Postgres High Availability (HA) on K3s in great detail in this article. We’ll go over the requirements, detailed instructions, and troubleshooting advice. You’ll have a reliable, scalable, and highly available Postgres setup on your K3s cluster by the end of this guide.

PostgreSQL, also referred to as Postgres, is a significant player in the world of open-source, object-relational database management systems (ORDBMS). It is well known for its ability to handle a wide range of workloads, from single machines to data warehouses or web services with many concurrent users, as well as its extensibility, standards compliance, and capacity.

On the other hand, K3s is a certified, lightweight Kubernetes distribution designed specifically for IoT, Edge, and ARM computing. It is simple to use, utilizes only half the memory, and has a binary size of less than 100MB.

Prerequisites

  1. You should have a fundamental understanding of Kubernetes and PostgreSQL because we’ll be working with these two technologies a lot.
  2. A Running K3s Cluster: A functioning K3s cluster is a must. If you don’t have one, follow our K3s installation guide.
  3. Helm Installed: Helm is a Kubernetes package manager, that makes it easier to deploy apps on the container-based platform. Here is a tutorial on how to install Helm.
  4. Access to a Command Line Interface (CLI): A CLI is necessary to execute the required commands. This can be your local terminal or a CLI provided by a cloud service.

Deploying the Postgres High Availability

With your K3s cluster and Helm ready, you can now deploy your Postgres HA. For this, we’ll use a Helm chart.

Packages of pre-configured Kubernetes resources are known as helm charts. They’re a great way to distribute applications because they’re so simple to deploy and distribute.

Although there are several Helm charts for Postgres, we’ll use the Bitnami Postgres High Availability chart for this tutorial. With separate primary and standby nodes, this chart deploys a Highly Available PostgreSQL cluster.

First, add the Bitnami repository to your Helm repos before deploying the Postgres High Availability:

helm repo add bitnami https://charts.bitnami.com/bitnami

Next, update your Helm repos:

helm repo update
How to Deploy Postgres High Availability on Kubernetes

Now you can deploy the Postgres High Availability using the following command:

helm install my-postgres bitnami/postgresql-ha

This command deploys your Postgres High Availability with the default configuration. If you want to customize your deployment, you can create a custom yaml file and pass it to the helm install command.

deploy Postgres High Availability

Verifying the Deployment

When the deployment is done, you can verify it by looking at the status of your pods. The simplest and smallest units that you can create or deploy in the Kubernetes object model are called pods.

To find out the status of your pods, enter the following command:

kubectl get pods
Postgres High Availability pods

You should see your Postgres pods running. You can examine the logs for any mistakes if they are not running. Use the kubectl logs <pod-name> command will give you access to a pod’s logs.

Postgres High Availability logs

Connecting to Your Postgres High Availability

You can establish a connection to your Postgres HA once it has been operationally tested. To begin, you must obtain the Postgres password. The following command can be used to retrieve it:

kubectl get secret my-postgres-postgresql-ha-postgresql --namespace default -o json
find Postgres High Availability password secret

This command will return the base64 encoded password.

Decode the Password: The password is stored in base64 format, so you’ll need to decode it. You can do this with the following command:

echo "Ym5BNU40SnZvaw==" | base64 --decode
bnA5N4Jvok

Next, you can connect to your Postgres High Availability database using the following command:

kubectl run my-postgres-postgresql-client --rm --tty -i --restart='Never' --namespace default --image docker.io/bitnami/postgresql:11.7.0-debian-10-r9 --env="PGPASSWORD=bnA5N4Jvok" --command -- psql --host my-postgres-postgresql-ha-postgresql -U postgres -d postgres -p 5432

Make sure to change PGPASSWORD= with your values.

Connecting to Your Postgres HA

Final Thoughts

If you’re unfamiliar with Kubernetes or databases, setting up a Postgres HA on K3s may seem like a daunting task. However, it can be made simple with the right resources and advice. Starting with setting up your K3s cluster and ending with connecting to your Postgres HA, this guide has walked you through every step of the procedure.

Remember that knowing what each tool does and how they work together is the key to a successful deployment. If you have any questions or would like to learn more, don’t be afraid to look through the official documentation of each tool.

You can deploy a highly available Postgres database that can act as the foundation for your applications by utilizing the strength of K3s and Helm, giving you a solid, scalable, and dependable data storage solution. K3s and Postgres offer a powerful combination that can meet your needs, whether you’re a developer looking to deploy your applications or a business looking for a dependable data storage solution.


0 Comments

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.