CYBERTEC PostgreSQL Logo

Deploying the Zalando Kubernetes operator

11.2024 / Category: / Tags: | |

Kubernetes (and OpenShift) have become highly popular deployment methods to run PostgreSQL at scale. While Kubernetes is an excellent way of running things it also helps to make things such as consistency, compliance and a lot more easier to achieve. In this post you will learn how to deploy the Zalando operator for PostgreSQL and make things work quickly and easily.

Deploying Minikube and cloning the operator

To make things simple for you, I have used Minikube which is an easy to use way to run Kubernetes locally on your development environment or even on your laptop.

If you are new to Minikube I highly recommend to take a look at the "Getting started" guide. It explains the most basic steps in detail.

Let us assume Minikube is already deployed on your machine. You can easily start the system by calling the following command:

We can verify if things are working as follows:

This command should return list of pods in 'default' namespace.

Once we have ensured that Kubernetes is indeed running we can download the Zalando operator from the Github repository which is easy to do:

The repository is public and can be cloned directly. In the next step we can simply apply templates. The way this works is that such templates basically describes what we want from Kubernetes. All the vital information of the service we desire to deploy is there.

Before looking at the content of the file we can simply apply the template. The first thing to throw at Kubernetes is the configmap:

The next thing to set is identities and permissions which can be stored in a simple YAML file as well. Here is how it works:

In the next step we deploy the operator that is going to control all our future PostgreSQL instances and clusters:

Those calls are expected to work flawlessly. However, it is important to understand what this file actually contains:

The template contains a handful of important variables such as number of replicas, number of CPUs, memory and a lot more. It is the most basic description of service there is.

Verifying your Kubernetes installation

Under normal circumstances the process is expected to work flawlessly. However, it is better to be safe than sorry. It is always a good idea to verify what we have just done:

If the status is shown as "Running" we have achieved success.

Deploying your first PostgreSQL instance

The great moment has come! It is time to spin up the first PostgreSQL instances and see how they operate. If the operator pod is running it listens to new events related to "postgresql" resources. For the purpose of demonstration it makes sense to edit "manifests/minimal-postgres-manifest.yaml" and set numberOfInstances: 1 in line 9. Now we can apply our first postgres manifest:

After the cluster manifest has been submitted and the validation has been passed, the operator will create "Service" and "Endpoint" resources as well as a "StatefulSet" which spins up new Pod(s) given the number of instances specified in the manifest.

The following command shows what is going on:

But, let us dig a little deeper and check created database pods and service resources:

After those checks we can move on to the next big thing: Connecting to the database instance we have just created.

Connecting to PostgreSQL in Kubernetes

The final stage has to do with networking. Keep in mind that thousands of services might be running inside a single Kubernetes cluster. Therefore we have to make sure that networking is right. In order to connect to the database, we can use port-forwarding. This can be done with some tool like k8s, or manually:

After wiring up the network we can fetch the password from Kubernetes secrets and pass them to psql in order to connect:

Voila, you have just successfully deployed the Zalando operator and created your first database instance in your personal Kubernetes cluster.

Adding a replica to Kubernetes

After this success we can even sophisticate our deployment by adding a replica. From now on deployment is really easy and we can make changes to our environment quickly and easily. Now let's add an replica to the cluster. For this let us edit "manifests/minimal-postgres-manifest.yaml" and set numberOfInstances:2 in line 9.

What we have essentially done here is to simply change the desired specification of our deployment. Kubernetes has made sure that those changes which are necessary to transition to the desired state happen behind the scenes. The system has made everything needed to transition to whatever we asked it to do.

Finally …

The operator has many more sample scripts that allow us to run various operations including backups, recovery and a a lot more. Make sure you check out our blog on a regular basis to find out more.

Leave a Reply

Your email address will not be published. Required fields are marked *

CYBERTEC Logo white
CYBERTEC PostgreSQL International GmbH
Römerstraße 19
2752 Wöllersdorf
Austria

+43 (0) 2622 93022-0
office@cybertec.at

Get the newest PostgreSQL Info & Tools


    This site is protected by reCAPTCHA and the Google Privacy Policy & Terms of Service apply.

    ©
    2024
    CYBERTEC PostgreSQL International GmbH
    phone-handsetmagnifiercrosscross-circle
    linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram