To myself...

Gone are the days that we write long scripts to provision and run our databases in an on-premise server, that is, for most cases that don't need to comply with a lot of regulatory policies. It's worth looking at how we can deploy a database in the cloud in just a few lines of code.

Steps

Install a Kubernetes operator in a cloud-based VM

Command

 helm repo add postgres-operator https://raw.githubusercontent.com/zalando/postgres-operator/master/charts/postgres-operator helm install postgres-operator postgres-operator/postgres-operator 

Output

 /workspace $ helm repo add postgres-operator https://raw.githubusercontent.com/zalando/postgres-operator/master/charts/postgres-operator "postgres-operator" has been added to your repositories  /workspace $ helm install postgres-operator postgres-operator/postgres-operator manifest_sorter.go:192: info: skipping unknown hook: "crd-install" manifest_sorter.go:192: info: skipping unknown hook: "crd-install" manifest_sorter.go:192: info: skipping unknown hook: "crd-install" NAME: postgres-operator LAST DEPLOYED: Wed Sep 15 02:44:28 2021 NAMESPACE: default STATUS: deployed REVISION: 1 TEST SUITE: None NOTES: To verify that postgres-operator has started, run:    kubectl --namespace=default get pods -l "app.kubernetes.io/name=postgres-operator"  

Great. The PostgreSQL is now deployed in Kubernetes default namespace.

To check if it's running,

 /workspace $ kubectl get pods -l "app.kubernetes.io/name=postgres-operator" NAME                                READY   STATUS    RESTARTS   AGE postgres-operator-978857b4d-z6g88   1/1     Running   0          10m 

Install the admin dashboard (optional)

I won't discuss how to get into the dashboard. I've added this so I can refer to this article in the future if I need to configure a dashboard for Postgresql.

Command

 helm repo add postgres-operator-ui https://raw.githubusercontent.com/zalando/postgres-operator/master/charts/postgres-operator-ui helm install postgres-operator-ui postgres-operator-ui/postgres-operator-ui --set service.type="NodePort" --set service.nodePort=31255 

Output

 /workspace $ helm repo add postgres-operator-ui https://raw.githubusercontent.com/zalando/postgres-operator/master/charts/postgres-operator-ui "postgres-operator-ui" has been added to your repositories  /workspace $ helm install postgres-operator-ui postgres-operator-ui/postgres-operator-ui --set service.type="NodePort" --set service.nodePort=31255 NAME: postgres-operator-ui LAST DEPLOYED: Wed Sep 15 02:57:51 2021 NAMESPACE: default STATUS: deployed REVISION: 1 TEST SUITE: None NOTES: To verify that postgres-operator has started, run:    kubectl --namespace=default get pods -l "app.kubernetes.io/name=postgres-operator-ui" 

Check if it's running

 /workspace $ kubectl get pods -l "app.kubernetes.io/name=postgres-operator-ui" NAME                                    READY   STATUS    RESTARTS   AGE postgres-operator-ui-6b4dd8cfbb-gvkqb   1/1     Running   0          90s 

Verify that we have installed the PostgreSQL operator and UI

Command

 kubectl get pods,services,deployments,replicasets 

Output

 /workspace $ kubectl get pods,services,deployments,replicasets NAME                                        READY   STATUS              RESTARTS   AGE pod/postgres-operator-ui-6b4dd8cfbb-6lrd2   0/1     ContainerCreating   0          19s pod/postgres-operator-978857b4d-qflvs       1/1     Running             0          32s  NAME                           TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE service/kubernetes             ClusterIP   10.43.0.1       <none>        443/TCP        46s service/postgres-operator      ClusterIP   10.43.145.110   <none>        8080/TCP       35s service/postgres-operator-ui   NodePort    10.43.7.84      <none>        80:31255/TCP   19s  NAME                                   READY   UP-TO-DATE   AVAILABLE   AGE deployment.apps/postgres-operator-ui   0/1     1            0           19s deployment.apps/postgres-operator      1/1     1            1           35s  NAME                                              DESIRED   CURRENT   READY   AGE replicaset.apps/postgres-operator-ui-6b4dd8cfbb   1         1         0       19s replicaset.apps/postgres-operator-978857b4d       1         1         1       32s 

Describe how the database server should be created

Add the following configuration in the /workspace/db.yaml file

 /workspace $ ls db.yaml  metallb-config  /workspace $ cat db.yaml apiVersion: "acid.zalan.do/v1" kind: postgresql metadata:   name: dataops-bootcamp-cluster   namespace: default spec:   teamId: "dataops-bootcamp"   volume:     size: 1Gi   numberOfInstances: 2   users:     dataops:  # database owner     - superuser     - createdb     learner_user: []  # role for application foo   databases:     dataops: learner  # dbname: owner   postgresql:     version: "12"  

Apply the configuration to the cluster

 /workspace $ kubectl apply -f /workspace/db.yaml  postgresql.acid.zalan.do/dataops-bootcamp-cluster created 

Get the status of the PostgreSQL resource. It should show running.

 /workspace $ kubectl get postgresql --watch NAME                       TEAM               VERSION   PODS   VOLUME   CPU-REQUEST   MEMORY-REQUEST   AGE   STATUS dataops-bootcamp-cluster   dataops-bootcamp   12        2      1Gi                                     53s   Running 

Notice that it has 2 pods because we declare in the configuration file that the instances is 2.

Let's view the details of the cluster

 /workspace $ kubectl describe postgresql Name:         dataops-bootcamp-cluster Namespace:    default Labels:       <none> Annotations:  <none> API Version:  acid.zalan.do/v1 Kind:         postgresql Metadata:   Creation Timestamp:  2021-09-15T03:09:13Z   Generation:          1   Managed Fields:     API Version:  acid.zalan.do/v1     Fields Type:  FieldsV1     fieldsV1:       f:metadata:         f:annotations:           .:           f:kubectl.kubernetes.io/last-applied-configuration:       f:spec:         .:         f:databases:           .:           f:dataops:         f:numberOfInstances:         f:postgresql:           .:           f:version:         f:teamId:         f:users:           .:           f:dataops:           f:learner_user:         f:volume:           .:           f:size:     Manager:      kubectl-client-side-apply     Operation:    Update     Time:         2021-09-15T03:09:13Z     API Version:  acid.zalan.do/v1     Fields Type:  FieldsV1     fieldsV1:       f:status:         .:         f:PostgresClusterStatus:     Manager:         postgres-operator     Operation:       Update     Time:            2021-09-15T03:09:13Z   Resource Version:  1045   Self Link:         /apis/acid.zalan.do/v1/namespaces/default/postgresqls/dataops-bootcamp-cluster   UID:               96b5b159-bf07-413f-8ff8-5a2204998a07 Spec:   Databases:     Dataops:            learner   Number Of Instances:  2   Postgresql:     Version:  12   Team Id:    dataops-bootcamp   Users:     Dataops:       superuser       createdb     learner_user:   Volume:     Size:  1Gi Status:   Postgres Cluster Status:  Running Events:   Type    Reason       Age    From               Message   ----    ------       ----   ----               -------   Normal  Create       3m24s  postgres-operator  Started creation of new cluster resources   Normal  Endpoints    3m24s  postgres-operator  Endpoint "default/dataops-bootcamp-cluster" has been successfully created   Normal  Services     3m24s  postgres-operator  The service "default/dataops-bootcamp-cluster" for role master has been successfully created   Normal  Services     3m24s  postgres-operator  The service "default/dataops-bootcamp-cluster-repl" for role replica has been successfully created   Normal  Secrets      3m23s  postgres-operator  The secrets have been successfully created   Normal  StatefulSet  3m23s  postgres-operator  Statefulset "default/dataops-bootcamp-cluster" has been successfully created   Normal  StatefulSet  2m32s  postgres-operator  Pods are ready  

Wrap up

In this scenario, we looked into how we can install PostgreSQL in the cloud with ease. We used helm to install a Kubernetes PostgreSQL Operator and UI. Then we created a k8s config file to declare 2 instances of the database. Finally, we instructed k8s with a simple apply command to spin up the 2 stateful pods based on the config file.


This free site is ad-supported. Learn more