Running PostgreSQL in a Cloud on Oracle Containers Engine for Kubernetes
In this post I am going to show a few steps to deploy and run PostgreSQL database in a K8S cluster on OKE.
The deployment is going to be based on postgres:11.1 Docker image which requires a few environment variables to be configured: POSTGRES_DB (database name), POSTGRES_USER and POSTGRES_PASSWORD. I am going to store values of these variables in K8S ConfigMap and Secret:
These configuration K8S resources are referenced by the StatefulSet which actually takes care of the lifespan of a pod withpostgres-db container:
StatefulSet K8S resource has been specially designed for stateful applications like database that save their data to a persistent storage. In order to define a persistent storage for our database we use another K8s resource Persistent Volume and here in the manifest file we are defining a claim to create a 3Gi Persistent Volume with name db. The volume is called persistent because its lifespan is not maintained by a container and not even by a pod, it’s maintained by a K8s cluster. So it can outlive any containers and pods and save the data. Meaning that if we kill or recreate a container or a pod or even the entire StatefulSet, the data will be still there. We are referring to this persistence volume in the container definition mounting a volume on path/var/lib/postgresql/data. This is where PostgreSQL container stores its data.
In order to access the database we are going to create aservice:
This is aLoadBalancer service which is accessible from outside of the K8S cluster:
Assuming that we have created a K8s cluster on OKE and ourkubectl is configured to communicate with it we can apply the manifest files to the cluster:
Having done that, we can connect to the database from our favorite IDE on our laptop
The manifest files for this post are available onGitHub.
Published on Java Code Geeks with permission by Eugene Fedorenko , partner at our JCG program. See the original article here: Running PostgreSQL in a Cloud on Oracle Containers Engine for Kubernetes
Opinions expressed by Java Code Geeks contributors are their own.