Skip to main content

Database

Introduction

This guide walks you through migrating a Postgres database from Heroku to Azure using Massdriver, with an emphasis on using private network access to enhance security. We’ll use Azure’s services, and Massdriver will handle most of the infrastructure setup and orchestration.

Prerequisites

Before starting, ensure you have the following:


Step 1: Set Up Azure Infrastructure with Massdriver

We will start by configuring the Azure infrastructure using Massdriver.

  1. Create a Massdriver environment:

    • Navigate to your Massdriver dashboard and create a new project if you don’t already have one. Create a new environment, naming it something appropriate for this migration (e.g., staging).
  2. Deploy a Private Virtual Network:

    • In Massdriver, search for the Azure Virtual Network bundle and deploy it. This will provide network isolation for your infrastructure.
    • Choose a region, configure CIDR blocks, and other network settings.
    • Deploy the virtual network and wait for it to complete.
  3. Set Up an Azure Postgres Database:

    • Using the Azure Postgres bundle in Massdriver, set up the database instance. Make sure it’s in the same region as your network.
    • Configure the resource group, database version (ensure it's compatible with your current Postgres version on Heroku), and enable private network access.
    • You may also enable automatic alarms and monitoring if needed.
    • Save and deploy the Postgres database.

Step 2: Back Up Your Database

  1. Create a Backup of Your Heroku Postgres Database:

    • Set up your environment variables (modify as needed):
    herokuappname=<your-heroku-app-name>
    • Use the Heroku CLI to create a backup of your current database:
    heroku pg:backups capture --app $herokuappname
    • Once the backup is complete, download it:
    heroku pg:backups:download --app $herokuappname
  2. Verify the Backup:

    • After downloading the backup (usually named latest.dump), verify its contents using the pg_restore command:
    pg_restore --list latest.dump

Step 3: Configure Kubernetes Cluster on Azure

  1. Deploy Azure Kubernetes Service (AKS):

    • In Massdriver, deploy an AKS cluster using the appropriate bundle. Select the region to match your existing infrastructure and network settings.
    • Enable ingress controllers and DNS if necessary.
  2. Access the Kubernetes Cluster:

    • Download the kubeconfig file from Massdriver and connect to the cluster by following this guide.
  3. Set Up a Jump Box Pod for Database Migration:

    • Create a new file called haiku-havoc-hero.yaml with the following content:
apiVersion: v1
kind: Pod
metadata:
name: haiku-havoc-hero
namespace: default
spec:
containers:
- name: postgres
image: mclacore/haiku-havoc-hero:postgres-v1
env:
- name: POSTGRES_PASSWORD
value: password
note

The haiku-havoc-hero pod includes tools like heroku, aws, az, and gcloud by default. You can specify the specific database tooling you need in the manifest file.

  1. Deploy the Jump Box Pod:

    kubectl apply -f haiku-havoc-hero.yaml

Step 4: Migrate Database to Azure

  1. Use Kubernetes as a Jump Box:

    • Once your jump box pod is running, exec into it:
    kubectl exec -it haiku-havoc-hero -c postgres -- bash
  2. Restore the Backup to Azure Postgres:

    • Setup your environment variables:
    herokuappname=<your-heroku-app-name>
    pgrg=<azure postgres resource group>
    pgname=<azure postgres name>
    database=<database-name>
    • Log into Heroku with interactive (-i) mode and use your Heroku API key as the password:
    heroku login -i
    • Download the backup from Heroku:
    heroku pg:backups:download --app $herokuappname
    • Log into Azure CLI and set FQDN and username environment variables:
    az login
    fqdn=$(az postgres flexible-server show --resource-group $pgrg --name $pgname --query "fullyQualifiedDomainName" --output tsv)
    username=$(az postgres flexible-server show --resource-group $pgrg --name $pgname --query "administratorLogin" --output tsv)
    • Restore the backup to Azure Postgres:
    pg_restore --verbose --no-owner -h $fqdn -U $username -d $database latest.dump
  3. Verify the Migration:

    • Once the migration is complete, connect to the Azure Postgres instance and verify that the data has been transferred correctly:
    psql -h $fqdn -U $username -d $database -c "\dt"

Step 5: Clean Up and Final Configuration

  1. Remove Temporary Jump Box:

    • Once you have verified that the migration is successful, clean up the jump box pod:
    kubectl delete pod haiku-havoc-hero
  2. Update Application Environment:

    • Check out our guide on setting up environment variables in your massdriver.yaml file for your application to connect to the new Azure Postgres database.
    • We also have a video guide on deplying a 3 tier web architecture on Azure with AKS that goes over setting up environment variables.

Conclusion

You have now successfully migrated your database from Heroku to Azure using Massdriver. By leveraging Massdriver’s bundles, secure private network access, and Kubernetes as a jump box, this process can be streamlined, ensuring a secure and efficient database migration.