11.2. Install PostgreSQL with Helm#

In this lab you’ll install PostgreSQL using Helm charts packaged by Bitnami. Helm charts make it easy to install whole applications that are ready to go. Bitnami packages applications with a lot of useful functionality. The Postgres Helm chart has built in replication and autoscaling, making it perfect for large or small applications. In order for your application to scale it needs a scalable database.

Introduction#

This process has three important steps:

  1. Find the configuration values you need in the README file on GitHub and put them into the values-postgres.yaml file.

  2. Install the Postgres Helm chart.

Postgres Configuration#

Helm charts are configured by YAML files. The exact values depend on the Helm chart. This configuration is sufficient to get postgres working in your cluster. Put this in the root of your repository in values-postgres.yaml:

# Configuration for Postgres
auth:
  username: mysiteuser
  password: this-is-a-bad-password
  database: mysite
# GKE Autopilot requires requests and limits.
primary:
  resources:
    requests:
      memory: "512Mi"
      cpu: "500m"
      ephemeral-storage: "100Mi"
    limits:
      memory: "512Mi"
      cpu: "500m"
      ephemeral-storage: "100Mi"

Install the Postgres Chart#

Start by checking out how Helm works:

$ helm install postgres oci://registry-1.docker.io/bitnamicharts/postgresql --values values-postgres.yaml --debug --dry-run

The last command generated the YAML files that will be applied to Kubernetes. You can see how much YAML you get for a small amount of configuration. Now install postgres:

$ helm install postgres oci://registry-1.docker.io/bitnamicharts/postgresql --values values-postgres.yaml 

Watch the resources come up:

$ kubectl get all 

Notice that a PVC is created:

$ kubectl get pvc 

Install the Postgres Client#

Note

The psql command is already installed in Google Cloud Shell Editor

Databases are structured storage. That means that the data isn’t as simple to access as files, where any program can see the data. Databases have clients that make the data accessible. In order to access the database on your dev box install the Postgres client using apt:

$ sudo apt install postgresql-client

Connect to Postgres#

The service type of the postgres-svc service is ClusterIP. That means that there will be no public IP address allocated for the Postgres. That’s correct because you don’t want to expose your data to the Internet. However, we want to connect to Postgres now and look around. We can do that with a kubectl port-forward command.

$ kubectl port-forward service/postgres-postgresql 5432:5432 

You need to open a second terminal to connect.

Connect to Postgres and Make a Table#

With the port forwarded we can connect in a second terminal like this:

$ psql -h localhost -U mysiteuser mysite 
Password for user mysiteuser: 
psql (16.1 (Debian 16.1-1.pgdg110+1))
Type "help" for help.

mysite=> 

Let’s create and examine some data the SQL way. At the end of the last part you should have connected to your database and seen the Postgres command prompt. In a relational database data is stored in tables called relations. Run this command to create a table:

postgres=# create table mydata (id INT PRIMARY KEY, name TEXT); 

Now let’s see the table we created:

postgres=# \d
         List of relations
 Schema |  Name  | Type  |  Owner   
--------+--------+-------+----------
 public | mydata | table | postgres
(1 row)

Let’s store some data in our table:

postgres=# insert into mydata values (1, 'Mike'), (2, 'Bike'); 
INSERT 0 2

Now let’s view the data we stored:

postgres=# select * from mydata; 
 id | name 
----+------
  1 | Mike
  2 | Bike
(2 rows)

Now let’s quit the database shell:

postgres=# \q

Clean Up#

When you uninstall a Helm chart any PersistentVolumeClaims that were created are kept. This helps avoid an accident that would cause data loss. However, if you want to reconfigure postgres with a different username or password you should remove it and delete the PersistentVolumeClaim.

$ helm uninstall postgres

Warning

Helm does not remove the data! That’s precious. If you try to re-install postgres with a different password the old data will cause a failure.

Remove the DB by deleting the PVC:

$ kubectl delete pvc/data-postgres-postgresql-0