Skip to main content

2. Deploying to DigitalOcean

This chapter will give a walkthrough of deploying Quick Start to DigitalOcean.

Prerequisites

1. Software Setup

To run the DAG locally using Docker or to deploy it to DigitalOcean, you'll need to ensure you have these installed and up-to-date:

  1. Git
  2. Python 3.10+ — To check your version run python3 --version. For installation instructions, visit Python 3 Installation & Setup Guide or Managing Multiple Python Versions With pyenv.
  3. pipx — For installation instructions, visit Torque Docs
  4. Docker with Docker Compose — To install Docker (with Docker Compose), visit docker.com/get-started website and follow the instructions. And make sure Docker is up & running by running docker info or by checking a visible Docker image on the MacOS status bar.
  5. Torque CLI - For installation instructions, visit Torque Docs

2. DigitalOcean Account and Personal Access Token

To deploy the DAG to the DigitalOcean, you have to take care of a few things. The list includes:

  1. You need a DigitalOcean account.
  2. You need to obtain a DigitalOcean Personal Access Token with read and write scopes.

Regardless of whether you have a DigitalOcean account, we strongly recommend you create a new DigitalOcean account for this guide because:

  1. We want to avoid anything happening to your production DigitalOcean account.
  2. DigitalOcean supports only one Container Registry per account, and the torque.container_registry.V1Provider supports only the creation of a new registry. Re-using the existing registry is currently not supported.

To open a new DigitalOcean account, you can use Torque's referral link and get $200 worth of credits:
Use Torque's Referral

(You can learn more about DigitalOcean's referral program at DigitalOcean Referral Program.)


⚠️ Important: How much will this cost me?

When you sign up for DigitalOcean, it will ask you for your credit card as a part of the account verification. If you use our referral link and get $200 worth of credits, your credit card won't be charged for following this "Getting Started" guide.

On the other hand, here is an estimate of how much it cost to deploy the DAG from this guide to DigitalOcean and to keep it up and running for a whole month:

=========================================  ==========
Service Cost
========================================= ==========
Container Registry (Starter) $0/mo
Kubernetes (1 Basic Node) $12/mo
Load Balancer (starts from) $12/mo
Managed Database (PostgreSQL Basic) $15/mo
**Total if kept running for 1 month.** **$39/mo**
========================================= ==========

It takes only one command to take all DigitalOcean provisioned resources down. You do not need to worry about dangling resources on DigitalOcean after finishing this tutorial.

Estimates were calculated from DigitalOcean's pricing page: www.digitalocean.com/pricing


Next, you'll need your DigitalOcean Personal Access Token with the read and write scopes. With your Personal Access Token, the DigitalOcean provider can deploy your DAG to DigitalOcean. Please follow the official guide: docs.digitalocean.com/reference/api/create-personal-access-token/

Now that you have a DigitalOcean Personal Access Token, you can use it for the DO_TOKEN environment variable used by the Torque CLI.

Step-by-step Deployment

  1. Download or clone GitHub repository locally.
git clone https://github.com/torquetech/quick-start-do-k8s.git
  1. Make the cloned repository your working directory.
cd quick-start-do-k82
  1. Make sure you are running Docker locally. You can test if Docker is running with docker info command or by checking a visible Docker icon on the MacOS status bar.

  2. Set DO_TOKEN environment variable.

export DO_TOKEN=<dop_v1_replace_with_your_personal_access_token>
  1. First we need to build prod deployment before applying it.
torque deployment build prod
  1. Apply the prod deployment.
torque deployment apply prod

The apply command will be waiting for all instances to be created on DigitalOcean and it might take up to half an hour until everything is up and running. But you do not need to worry about it, providers will do all the work.

After the apply command finishes, you can visit your DigitalOcean Dashboard to check created K8s cluster. DigitalOcean does a great job of explaining how to set up the kubectl config and use it to observe your K8s cluster.

⚠️ Important: The same rules for managing infrastructure with Infrastructure as Code solutions apply: do not change anything manually. If you need to change something, do it through torque commands.

Testing the Deployment

To test the deployment, we are going to make a simple curl request to one of the backend services. The IP address of your K8s cluster load balancer can be found at the DigitalOcean Dashboard:

  1. Open your prod-* project from the DigitalOcean Dashboard.

    Select project.

  2. Select the prod-*-k8s cluster.

    Select cluster.

  3. Select the Resources tab.

    Select Resources tab.

  4. Select lb-impl balancer inside the Load Balancers group.

    Select Load Balancer group.

  5. The IP address will be at the end of the navigation breadcrumbs. Click on the IP address to copy it to your clipboard.

    Copy IP address.

Now that you have the IP address, you can run:

curl -H "Host: api.example.com" https://<IP address>/backend-service -k

☝️ Note: It might take up to a minute for the container to spin up for the first time. So if you get a 503 Service Temporarely Unavailable error from Nginx, wait a bit and try again.

The output should be the current database time.

Database time: 2023-01-09T10:49:30.536818Z%

Deleting the Deployment

torque deployment delete command strips down the deployed objects. In the case of prod deployment, the delete command will strip down all deployed services from DigitalOcean. You want to execute the delete command before abandoning this tutorial to make sure all provisioned resources are deleted from the DigitalOcean account.

torque deployment delete prod