First Steps with BackstageStep 8 of 10: Prepare a Database Installation
All Labs

Prepare a Database Installation

In the meantime, we have a decent Backstage installation running, with GitHub integration and a Software Template to create new repositories. However, we are still running Backstage locally, which is not suitable for a production environment. Therefore, in the next steps, we will deploy Backstage to a Kubernetes cluster using the Backstage Helm Chart. As Backstage uses a database to store its data, we will prepare a PostgreSQL database installation in our Kubernetes cluster - in our case, we'll use CloudNative PG (a Cloud-Native PostgreSQL Operator) for this purpose.

Initially, ensure that you have a running Kubernetes cluster and have kubectl and helm installed and configured to access your cluster. In the next step, we'll add the CloudNative PG Helm repository and install the operator in our cluster. Therefore, run the following commands:

helm repo add cnpg https://cloudnative-pg.github.io/charts
helm upgrade --install cnpg \
  --namespace cnpg-system \
  --create-namespace \
  cnpg/cloudnative-pg

You can track the installation progress using:

kubectl -n cnpg-system get pods 

After some time, you should see the operator pod running. As with every operator, it's important to know that this only installs the operator itself, but not any actual database instances. In the next step, we'll create a PostgreSQL cluster using CloudNative PG.

Info

If you're using a Kubernetes cluster that doesn't support Persistent Volumes, please ensure that you have configured a suitable StorageClass in your cluster before proceeding. In the next step, we'll install Longhorn as a simple StorageClass for demonstration purposes. Please skip this step if your cluster already has a working StorageClass. Please note that we're using Longhorn here only for demonstration purposes. In a production environment, please make yourself familiar with it, evaluate if it fits your requirements and configure it properly.

Install Longhorn (if needed)

Longhorn is a simple, lightweight and easy-to-use distributed block storage system for Kubernetes. It is a great choice for development and testing environments, as it doesn't require any external storage systems. Run the following commands to install Longhorn in your cluster:

helm repo add longhorn https://charts.longhorn.io
helm upgrade --install longhorn \
  --namespace longhorn-system \
  --create-namespace \
  longhorn/longhorn

You can track the installation progress using:

kubectl -n longhorn-system get pods

After some time, you should see the Longhorn pods running. Longhorn will automatically create a StorageClass named longhorn, which we can use for our PostgreSQL cluster.

To test our installation, we'll create a simple PostgreSQL cluster with a single instance. Therefore, create a new file called postgres-cluster.yaml with the following content:

apiVersion: postgresql.cnpg.io/v1
kind: Cluster
metadata:
  name: backstage-database
spec:
  enableSuperuserAccess: true
  instances: 1
  monitoring:
    enablePodMonitor: true
  storage:
    size: 1Gi

This manifest defines a PostgreSQL cluster with 1 instance and 1Gi of storage per instance. You can adjust these values based on your requirements. You can inspect the progress of the cluster creation using:

Now, let's create a namespace for Backstage and deploy the PostgreSQL cluster in this namespace. Run the following commands:

kubectl create namespace backstage
kubectl apply -n backstage -f postgres-cluster.yaml
kubectl get pods,cluster

After some time, you should see the PostgreSQL pods running and the cluster status should be "Running". Now we have a running PostgreSQL cluster in our Kubernetes cluster. As we have now fulfilled the database prerequisite, we can move on to the container build and deployment of Backstage itself.