Back to blog results

7월 6, 2023 By Greg Ziemiecki

How to use Kubernetes to deploy Postgres


Kubernetes is an orchestration platform that allows containers to be deployed in an automated and resilient way, abstracting many of the manual steps of rolling upgrades and scaling. You will usually want to deploy database applications (like PostgreSQL) as well, so that your applications can leverage their features within the cluster. Deploying in a Kubernetes cluster lets you build out your cloud-native PostgreSQL instance.

Read on for simple steps for deploying and running a PostgreSQL database on Kubernetes. Learn how to set up a single PostgreSQL instance for testing, and then get a sense of an advanced use case in which there are a few options for deploying a more configurable instance of PostgreSQL.

Prerequisites

In order to follow along, you will need to have:

  • a Kubernetes cluster. I’ve created mine using Digital Ocean, but you could use Kind or Minikube if you are working locally.

  • some working knowledge of Kubectl.

Before we start, we will explain the basic steps for deploying a single instance of PostgreSQL on Kubernetes.

Simple PostgreSQL deployment

Dockerize PostgreSQL

Kubernetes pulls Docker images from a registry and deploys them based on a configuration file. To finish this step, you need a Docker image for PostgreSQL. You can create one on your own using these basic steps. Or, better yet, you can use the official open-source image from Docker Hub. In this post, we will be using the latest Postgres 15.3 image.

Create your connection configuration and secrets

You need to store some connection configuration for the PostgreSQL instance using the Kubernetes secrets config. This is to ensure that sensitive information (like database credentials) is not stored in plain sight.

Note that this provider stores the secrets as base64 strings by default, so you’ll need to enable encryption at rest for better security.

Once you have everything configured, you need to create the configuration for the Kubernetes secret. We will use the following values for the Postgres password:

❯ echo "password" | base64

cGFzc3dvcmQK

Then, create a secrets config file and apply it on the cluster:

> cat postgres-secrets.yml

apiVersion: v1

kind: Secret

metadata:

name: postgres-secret-config

type: Opaque

data:

password: cG9zdGdyZXMK

Here, we used kind: Secret to instruct Kubernetes to use a secrets provider to store the data. The name that it needs to use to store those values is under the key postgres-secret-config. Finally, we provided the key/value pairs that we need to secretly store in the data section.

Now, we apply this config and then verify that the contents are stored correctly:

❯ kubectl apply -f postgres-secrets.yml

secret/postgres-secret-config created

❯ kubectl get secret postgres-secret-config -o yaml

apiVersion: v1

data:

password: cG9zdGdyZXMK

....

Create PersistentVolume and PersistentVolumeClaim

Next, you want to create permanent file storage for your database data. This is because the Docker instance does not have persistent storage for any information when the container no longer exists (by default).

The solution is to mount a filesystem to store the PostgreSQL data. Kubernetes has a different configuration format for those operations. First, you create a PersistentVolume manifest that describes the type of volumes you want to use. Next, you create a PersistentVolumeClaim that requests the usage for that particular PersistentVolume type based on the same storage class.

For our example, we will use the current node filesystem as a volume, but it’s better to use a StorageClass suitable for database operations.

First, we define the configuration for the PersistentVolume:

> cat pv-volume.yml

apiVersion: v1

kind: PersistentVolume

metadata:

name: postgres-pv-volume

labels:

type: local

spec:

storageClassName: manual

capacity:

storage: 5Gi

accessModes:

- ReadWriteOnce

hostPath:

path: "/mnt/data"

In this configuration, we instructed it to reserve 5GB of read-write storage at /mnt/data on the cluster’s node.

Now, we apply it and check that the persistent volume is available:

❯ kubectl apply -f pv-volume.yml

persistentvolume/postgres-pv-volume created

❯ kubectl get pv postgres-pv-volume

NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE

postgres-pv-volume 5Gi RWO Retain Available manual 51s

We need to follow up with a PersistentVolumeClaim configuration that matches the details of the previous manifest:

> cat pv-claim.yml

apiVersion: v1

kind: PersistentVolumeClaim

metadata:

name: postgres-pv-claim

spec:

storageClassName: manual

accessModes:

- ReadWriteOnce

resources:

requests:

storage: 1Gi

In this configuration, we requested a PersistentVolumeClaim for 1GB of data using the same storage class name. This is an important parameter because it lets Kubernetes reserve 1GB of the available 5GB of the same storage class for this claim.

Now, we apply it and check that the persistent volume claim is bound:

❯ kubectl apply -f pv-claim.yml

persistentvolumeclaim/postgres-pv-claim created

❯ kubectl get pvc postgres-pv-claim

NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE

postgres-pv-claim Bound postgres-pv-volume 1Gi RWO manual 5m32s

Create Kubernetes deployment

Next, we need to issue a deployment config for our instance that uses the settings from the Postgres-secret-config secret name. We also need to reference the PersistentVolume and PersistentVolumeClaim that we created earlier.

> cat postgres-deployment.yml

apiVersion: apps/v1

kind: Deployment

metadata:

name: postgres

spec:

replicas: 1

selector:

matchLabels:

app: postgres

template:

metadata:

labels:

app: postgres

spec:

volumes:

- name: postgres-pv-storage

persistentVolumeClaim:

claimName: postgres-pv-claim

containers:

- name: postgres

image: postgres:11

imagePullPolicy: IfNotPresent

ports:

- containerPort: 5432

env:

- name: POSTGRES_PASSWORD

valueFrom:

secretKeyRef:

name: postgres-secret-config

key: password

- name: PGDATA

value: /var/lib/postgresql/data/pgdata

volumeMounts:

- mountPath: /var/lib/postgresql/data

name: postgres-pv-storage

Here, we glued all of the configurations that we defined earlier with the Kubernetes secret config and the persistent volume mounts. We used the apiVersion: apps/v1 deployment config, which requires us to specify quite a few lines, such as selector and metadata fields. Then, we added details of the container image and the image pull policy. This is all necessary to ensure that we have the right volume and secrets used for that container.

Now, we apply the deployment and check that is available and healthy:

❯ kubectl apply -f postgres-deployment.yml

deployment.apps/postgres created

❯ kubectl get deployments

NAME READY UP-TO-DATE AVAILABLE AGE

postgres 1/1 1 1 28s

Create service

You can also create a service to expose the PostgreSQL server. You have several options to do so, like configuring a different port or exposing the NodePort or LoadBalancer. For the sake of simplicity, we will show you how to use NodePort, which exposes the service on the Node's IP at a static port.

You can use the following service manifest:

> cat postgres-service.yml

apiVersion: v1

kind: Service

metadata:

name: postgres

labels:

app: postgres

spec:

type: NodePort

ports:

- port: 5432

selector:

app: postgres


Next, apply the service and check that is available and has been assigned a port:

❯ kubectl apply -f postgres-service.yml

service/postgres created

❯ kubectl get service postgres

NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE

Test the connection to the Postgres database

You should be able to connect to the PostgreSQL database internally using the following commands:

❯ kubectl get pods

NAME READY STATUS RESTARTS AGE

postgres-57f4746d96-7z5q8 1/1 Running 0 30m

❯ kubectl exec -it postgres-57f4746d96-7z5q8 -- psql -U postgres

There is also a handy way to store the pod name in a variable:

POD=`kubectl get pods -l app=postgres -o wide | grep -v NAME | awk '{print $1}'`

You could also use another Docker container to connect through the psql command:

export POSTGRES_PASSWORD=$(kubectl get secret postgres-secret-config -o jsonpath="{.data.password}" | base64 --decode)

❯ kubectl run postgres-client --rm --tty -i --restart='Never' --image postgres:11 --env="PGPASSWORD=$POSTGRES_PASSWORD" --command -- psql -h postgres -U postgres

If you don't see a command prompt, try pressing enter.

postgres=#

Now, you’re ready to perform queries.

Advanced PostgreSQL deployment

In the previous example, we only deployed a single instance of PostgreSQL for development purposes. If you want a more enterprise-ready solution, you could:

Use the Bitnami PostgreSQL Deployment offerings: Bitnami supports multiple types of deployments (including helm) and supports many configuration options for large-scale deployments.

Use the Zalando PostgreSQL Operator offering: This is a high-availability solution from Zalando that relies on their existing Patroni -based cluster template.

Next steps

If you followed along with us, you’ve now mastered the process of deploying a simple PostgreSQL instance on Kubernetes and learned some best practices for handling secrets. For further optimizations, you may want to consider changing the PersistentVolume policy from delete to retain in order to prevent it from automatically deleting volumes when users delete volume claims.

Learn more about how Sumo Logic can help with your Kubernetes monitoring.

Complete visibility for DevSecOps

Reduce downtime and move from reactive to proactive monitoring.

Navigate Kubernetes with Sumo Logic

Monitor, troubleshoot and secure your Kubernetes clusters with Sumo Logic cloud-native SaaS analytics solution for K8s.

Learn more
Greg Ziemiecki

Greg Ziemiecki

Senior Technical Product Manager

Greg is a Product Manager with about 15 years of experience in IT, half of it being in a Product Management role. His experience includes SIEM, monitoring, and Network Management products in the Telco space. At Sumo Logic, he’s working in the observability area looking at how to provide best-in-class out-of-the-box insights into our customers' cloud infrastructure.

When not working, he enjoys motorcycle riding, hiking, and playing chess.

More posts by Greg Ziemiecki.

People who read this also enjoyed