Last week we manage to configure MicroK8s for having our own Kubernetes (k8s) cluster, so for learning a bit more on it we are going to learn how to install a PostgreSQL server that will be run natively in our cloud.
For installing PostgreSQL we are going to use a k8s operator, but first let’s understand what an operator is.
Operators are software extensions to k8s that make use of custom resources to manage applications and their components. K8s’ controllers concept lets you extend the cluster’s behaviour without modifying the code of k8s itself. Operators are clients of the k8s API that act as controllers for a Custom Resource.
So basically we extended the functionality of our cluster to easily deploy applications that could automatize how they are deployed and configure.
Why use a operator to deploy a database server? Because databases in a cloud are really hard, we need to provision storage, manage replication, credentials for the clients, and a dozen of complicated and delicate details, an operator will automatize all of this for us.
For this we are going to use this postgres-operator.
Adding the Operator
First we need to clone the repo:
Note: Currently on version 1.2.0 of the operator there is a bug that is fixed but not released, we will patch the installation before. Edit the file manifests/postgres-operator.yaml :
Now we will install the operator using the manifests:
Now to check that the operator has started we could do:
Creating a Database
Operators work with resource, when we provide to k8s a resource with the data for a register operator it will use it to install it.
For example les create a database named movies with and admin user name moviesdba and a user named moviesuser. For this we will create a file that will could name movies-db.yml :
Now let’s create our database using :
Now we we need to wait that our database is running, we could check until we get this output :
And the inspect the nodes four our database with :
As we could see our database has a master and a replica running.
Accessing our database
For testing our database lest just use the postgresql client, we could install it with :
We will login in our server with our moviesdba user so we need to get it password, we could get it with:
The operator has configured our database server to only be accessed within the cluster so in order to test it we will forward the postgres port 5432 on our master to our localhost port 6432, this will run until we do ctrl+c :
Finally we can connect to our database with with the provide user and password using psql in another shell :
We have verify that our users are setup in our database, now we are ready for more steps that I’ll try to cover in the following days creating an application that run on our k8s cluster and connect to our database.
Finally if we look to look more about how this operator works, how we could scale our replicas and repair our database we should take a look to the operator documentation.