fbpx
How to Deploy PostgreSQL on K3s

This guide will provide a detailed walkthrough on how to deploy PostgreSQL on K3s cluster. Everything will be covered, from the necessary steps to the deployment procedure, testing, and connection to the PostgreSQL database.

K3s is a certified, lightweight Kubernetes distribution designed for the Internet of Things and edge computing. K3s, created by Rancher Labs, has grown in popularity as a result of its ease of use and low resource requirements, making it the ideal option for developers and businesses looking to deploy applications in environments with limited resources.

On the other hand, PostgreSQL is a powerful open-source object-relational database system. It has had more than 30 years of continuous development, and it has a solid track record for dependability, sturdiness, and performance.

Prerequisites

Make sure you have the following prerequisites before we start the deployment process:

  • Knowledge of the fundamental Kubernetes concepts, such as Pods, Services, Deployments, and StatefulSets, is essential.
  • Installed Helm: Helm, the Kubernetes package manager, makes it easier to deploy applications on the container-based platform. Installed on your system must be Helm 3.x. You can learn here how to install Helm.
  • For this deployment, you’ll need to have access to a PostgreSQL Docker image. It is sufficient to use the official PostgreSQL image from Docker Hub.

Step 1: Add the Bitnami Helm repository

You must add the Bitnami Helm repository to your Helm client after K3s and Helm have been installed. Pre-configured Helm charts are available from Bitnami for a variety of well-known open-source programs, including PostgreSQL. The Bitnami Helm repository can be added as follows:

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

To ensure that your Helm client has the most recent version of the charts, you should update it after adding the Bitnami Helm repository:

helm repo update
How to Deploy PostgreSQL on K3s

Step 2: Deploying PostgreSQL on K3s

Now that Helm has been installed and the environment has been set up, PostgreSQL on K3s can be deployed. Running the following command accomplishes this:

helm install my-postgresql bitnami/postgresql
Deploying PostgreSQL on K3s

This command uses the Bitnami PostgreSQL Helm chart to deploy PostgreSQL on K3s cluster. You can use any name you like for the my-postgresql argument, which is the name of the Helm release.

The command outputs some information about the deployment, including the PostgreSQL admin password. It’s important to note down this password as you’ll need it later to connect to the PostgreSQL database.

You can also find the PostgreSQL password by following these steps:

You’ll need to decode the password since it is stored in base64 format. The encoded value isn’t directly disclosed by the kubectl describe secret command, though. The encrypted password can be retrieved by using the kubectl get secret command with the -o jsonpath option.

Get the Encoded Password: Use the following command to get the encoded password:

kubectl get secret my-postgresql -o jsonpath="{.data.postgres-password}"
PostgreSQL on K3s

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 '<base64-password>' | base64 --decode

Replace <base64-password> with the base64-encoded password you got from the previous command.

find the PostgreSQL on k3s password

Step 3: Verifying the Deployment

Run the following command to see if PostgreSQL has been successfully deployed:

kubectl get pods

This command will list all the pods running in your K3s cluster. You should see a pod with a name that starts with my-postgresql.

The following command can be used to check the PostgreSQL service’s status:

kubectl get svc my-postgresql

The PostgreSQL service’s ClusterIP and port will be included in the output from this command. This data can be used to establish a connection to PostgreSQL.

Step 4: Connecting to PostgreSQL on K3s

To connect to PostgreSQL on K3s, you can use the psql command line utility. You can install it on your system using your package manager. For example, on Ubuntu, you can install it with the following command:

sudo apt-get install postgresql-client

Once you have psql installed, you can connect to PostgreSQL by running the following command:

PGPASSWORD=<password> psql --host <ClusterIP> --port <port> --username postgres --dbname postgres

Replace <password> with the PostgreSQL admin password, <ClusterIP> with the ClusterIP of the PostgreSQL service, and <port> with the port of the PostgreSQL service.

Connecting to PostgreSQL on K3s

Final Thoughts

If you have the proper tools and knowledge, installing PostgreSQL on K3s is a simple process. Using the Bitnami PostgreSQL Helm chart, this guide has shown you how to set up your environment, install Helm, deploy PostgreSQL, check the deployment, and connect to PostgreSQL. With this information, you should be able to deploy PostgreSQL on K3s without difficulty, giving your applications a solid and dependable database solution.


1 Comment

How To Install PostgreSQL On Ubuntu 22.04 - Virtono Community · August 21, 2023 at 2:12 PM

[…] Congratulations! You’ve created your first PostgreSQL on Ubuntu database and user as well as successfully installed PostgreSQL on Ubuntu 22.04. You can now build robust, sophisticated databases for your applications using PostgreSQL. Consider exploring PostgreSQL’s numerous features to see how you can tailor it to meet your unique needs. Keep in mind that PostgreSQL is highly customizable. […]

Leave a Reply

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