DEV Community

Shanika Wickramasinghe for Arctype

Posted on

Deploy and Manage PostgreSQL on Kubernetes

PostgreSQL database is an enterprise-grade, open-source object-relational database system focused on providing a highly scalable and robust platform while conforming to SQL standards. Kubernetes is an open-source container orchestration platform designed to be easily deployable in any environment. It enables users to easily deploy and manage containerized applications.

Utilizing Kubernetes to deploy a PostgreSQL instance allows users to leverage the strengths of both platforms. Furthermore, it enables users to create a highly portable and scalable PostgreSQL instance in a Kubernetes cluster. In this article, we will discuss how to easily deploy a PostgreSQL pod in Kubernetes.

What options are available to deploy PostgreSQL?

When it comes to deploying PostgreSQL on Kubernetes, there are two options. One is to create a Kubernetes deployment from scratch, and the other is to use a helm chart to carry out the deployment.

Option 01 - Creating deployment configuration from scratch

This option requires the user to set up the ConfigMaps, create and set up persistent storage volumes and then create the PostgreSQL deployment and service. This process involves a higher degree of planning and technical knowledge to get the service up and running. Therefore, only use this option if you have specific requirements that require custom configurations from the ground up.

Therefore, only use this option if you have specific requirements that require custom configurations such as complex persistent storage configurations, network and security policies, multi cluster connectivity etc...

Option 02 - Use a helm chart

Helm charts can be considered a type of package that can be used to easily deploy applications to Kubernetes clusters. These charts can be used in any kind of deployment, from a single pod application to complex applications consisting of multiple pods with different software and services.

In this article, we will focus on using a helm chart to deploy a PostgreSQL instance. Before that, we should know what Helm is and how to configure Helm in a development environment.

What is Helm?

Helm is considered as the package manager for Kubernetes. It is an open-source project started with the goal of providing a more efficient way to manage Kubernetes YAML files created when deploying an application.

Currently, Helm has evolved into a fully-fledged package manager that streamlines the installation, upgrading, dependency management, and configuration of applications on a Kubernetes cluster using simple CLI commands (Helm Client).

Helm Architecture

Helm creates packages by creating helm charts that bundle one or more Kubernetes manifests (YAML files). These charts contain all the dependencies of the project and allow users to deploy an application with a single command. Additionally, charts can be versioned so that users can create different configurations for the same helm chart under different versions.

Helm is composed of three main concepts

  • Chart - Contains Information required to create an application instance in Kubernetes
  • Config - Contains the configuration information that can be merged with a chart to create a releasable object.
  • Release - A running instance of a chart, combined with a specific config.

Helm executable can be broken down into two components as follows;

  • The Helm Client: A command-line tool used by end-users to create and manage helm charts and interfaces with the Helm library.

  • The Helm Library: Interfaces with the Kubernetes API and executes all the helm operations.

Both of these are written in the Go programming language. To connect with Kubernetes, Helm utilizes the Kubernetes client library using REST API.

How to Install Helm

Before installing Helm, we need to have a Kubernetes cluster configured and running. It is recommended to use the latest stable release of Kubernetes.

Installing Helm is a simple process. You can use a package manager like Homebrew for Mac, Chocolatey/Scoop for Windows, or apt/Snap for Debian/Ubuntu. Another way to install Helm is by downloading the binary for the related operating system.

The following example demonstrates how to install Helm using the chocolatey package manager in a windows environment.

choco install kubernetes-helm

RESULT

That’s it, and you have completed installing Helm. Next, let’s verify the installation by checking the helm version using the command below.

helm version

RESULT

Finding Helm Charts

You have the option to create helm charts from scratch. However, there are also prebuilt charts that can be deployed instantly for popular software like PostgreSQL. There, Helm charts are stored and shared using a chart repository.

Artifact Hub is the official distributed community Helm chart repository. For third-party repositories, we need to add the repository to our local environment before we can use it.

Available PostgreSQL charts

First, we have to find a suitable PostgreSQL helm chart for deployment. For that, we can search the Artifact Hub using the following command in the CLI interface.

helm search hub postgresql

RESULT

The above command will search for all the available charts in the Artifact Hub for a PostgreSQL chart. The “hub” parameter points to the Artifact Hub.

To search in a third-party repository, first, we need to add the repository to our helm installation. It can be done through the “repo add” command. The following example shows how to add the bitnami repository to our helm installation and search for the repository.

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

helm search repo bitnami

RESULT

In the above search command, we use the parameter “repo” to carry out the search in the newly added repository apart from the artifact hub. Now let’s search for PostgreSQL instances in this repo using the following command.

helm search repo postgresql

RESULT

Installing Helm Charts

By now, we have located a helm chart, and the next step would be to install the chart using the “helm install command,” as shown below.

Syntax
helm install <Pod Name> <Chart Name>

Code
helm install postgresql-dev bitnami/postgresql

RESULT

Wait till the installation is completed. Then, check the Kubernetes environment, and you will see that a PostgreSQL instance has been deployed successfully.

kubectl get all

RESULT

Now we have deployed a PostgreSQL container. The next step would be to connect to the database, and it can be done using the following commands.

We need to extract the password of the instance and save it as an environmental variable. That password will be used to create the connection.

export POSTGRES_PASSWORD=$(kubectl get secret --namespace default postgresql-test -o jsonpath="{.data.postgresql-password}" | base64 --decode)

kubectl run postgresql-test-client --rm --tty -i --restart='Never' --namespace default --image docker.io/bitnami/postgresql:11.11.0-debian-10-r31 --env="PGPASSWORD=$POSTGRES_PASSWORD" --command -- psql --host postgresql-test -U postgres -d postgres -p 5432

RESULT

After connecting to the database successfully, we can verify our installation by checking the server version.

Configuration Options

Depending on the selected helm chart, the available configuration options may differ. The bitnami PostgreSQL chart offers a whole host of options from simple user creation to complex security configurations like setting up certificates and policies. All the available options are listed on the helm chart GitHub page.

Let's look at some common options, such as changing the username, password, database name, and port. This can be done by providing the following parameters while installing the helm chart.

helm install postgresql-test01 bitnami/postgresql --set
global.postgresql.postgresqlUsername=testadmin --set
global.postgresql.postgresqlPassword=testadmin123 --set
global.postgresql.postgresqlDatabase=testdb --set
global.postgresql.servicePort=5555
Enter fullscreen mode Exit fullscreen mode

RESULT

The above output indicates that the new port is configured, and a new user is created. If we export the password for the “testadmin” user, it will return the user-defined password.

export POSTGRES_PASSWORD=$(kubectl get secret --namespace default postgresql-test01 -o jsonpath="{.data.postgresql-password}" | base64 --decode)

RESULT

Another common scenario is to configure persistent storage for PostgreSQL deployment. We need to create persistent storage volume before installing the helm chart. The below code block shows how to create a simple storage volume and a volume claim and then attach it to the PostgreSQL deployment.

test-pv.yaml

apiVersion: v1
kind: PersistentVolume
metadata:
  name: postgresql-pv
  labels:
    type: local
spec:
  storageClassName: manual
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/mnt/data"
Enter fullscreen mode Exit fullscreen mode

test-pvc.yaml

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: postgresql-pv-claim
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 10Gi
Enter fullscreen mode Exit fullscreen mode

Create the volume and claim
kubectl apply -f test-pv.yaml
kubectl apply -f test-pvc.yaml

kubectl get pvc

RESULT

Then, we can attach the persistent volume claim to the helm chart using the following command. We will set the “volumePermissions.enabled” to true for mitigating any permission issues that can occur when writing to the storage volume.

helm install postgresql-test02 bitnami/postgresql --set
persistence.existingClaim=postgresql-pv-claim --set
volumePermissions.enabled=true
Enter fullscreen mode Exit fullscreen mode

RESULT

Upgrading Helm Chart

When upgrading the PostgreSQL helm, it is essential to provide the existing postgresqlPassword and replication.password parameters. It will ensure these values are not updated with random details, which will lead to an inaccessible database.

Syntax

helm upgrade <Pod Name>  <Chart Name> --set
postgresqlPassword=<POSTGRESQL_PASSWORD> --set
replication.password=<REPLICATION_PASSWORD>
Enter fullscreen mode Exit fullscreen mode

Code

helm upgrade postgresql-test bitnami/postgresql --set
postgresqlPassword=testadmin123 --set
replication.password=12345
Enter fullscreen mode Exit fullscreen mode

RESULT

Deleting a Helm Chart

We can simply delete a helm chart by providing the chart name with the delete command, as shown below.

Syntax
helm delete <Chart Name>

Code
helm delete postgresql-test

RESULT

However, this would not delete any PVC associated with the chart. So we need to delete the unused PVCs manually.
kubectl get pvc

kubectl delete pvc data-postgresql-test-postgresql-0

RESULT

Conclusion

In this article, we learned how to deploy a PostgreSQL database in a Kubernetes cluster using a helm chart, change the configurations, and upgrade and delete the PostgreSQL chart. Using helm charts is a relatively straightforward method to deploy commonly used applications in a Kubernetes cluster. This frees up the users to concentrate on the development of the database without having to create complex Kubernetes configurations.

Top comments (0)