Question

How to create replicas of Droplet on schedule and later remove

How to create replicas of running droplets for scheduled time? After schedule time droplets replicas should be remove.


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Bobby Iliev
Site Moderator
Site Moderator badge
May 14, 2024
Accepted Answer

Hey!

This is not available out of the box via the DigitalOcean UI, but you can achieve this using different tools like Terraform, doctl or even only the DigitalOcean API itself.

Here is an example on how to do this with Terraform:

Prerequisites

  1. DigitalOcean Account: Ensure you have an active DigitalOcean account and API access token.
  2. Terraform: Install Terraform on your local machine.
  3. cron: Unix-based scheduling utility (or an equivalent for Windows).

Steps

1. Install and Configure Terraform

  1. Install Terraform:

    • On macOS: brew install terraform
    • On Windows: Download the binary from the Terraform website and add it to your PATH.
  2. Create a Terraform Configuration File:

    provider "digitalocean" {
      token = var.do_token
    }
    
    variable "do_token" {}
    
    variable "droplet_name" {
      default = "example-droplet"
    }
    
    variable "region" {
      default = "nyc3"
    }
    
    variable "image" {
      default = "ubuntu-20-04-x64"
    }
    
    variable "size" {
      default = "s-1vcpu-1gb"
    }
    
    resource "digitalocean_droplet" "replica" {
      count  = var.replica_count
      name   = "${var.droplet_name}-replica-${count.index}"
      region = var.region
      image  = var.image
      size   = var.size
    }
    
  3. Create a Variables File (variables.tfvars):

    do_token      = "YOUR_DIGITALOCEAN_API_TOKEN"
    droplet_name  = "base-droplet"
    replica_count = 3
    
  4. Initialize and Apply Terraform Configuration:

    terraform init
    terraform apply -var-file="variables.tfvars"
    

2. Schedule Creation and Deletion of Droplet Replicas

  1. Create a Shell Script for Terraform Apply (create_replicas.sh):

    #!/bin/bash
    cd /path/to/terraform/config
    terraform apply -var-file="variables.tfvars" -auto-approve
    
  2. Create a Shell Script for Terraform Destroy (destroy_replicas.sh):

    #!/bin/bash
    cd /path/to/terraform/config
    terraform destroy -var-file="variables.tfvars" -auto-approve
    
  3. Schedule the Scripts Using cron:

    • Open the crontab editor:

      crontab -e
      
    • Add the following lines to schedule the creation and deletion of droplets:

      # Schedule droplet replicas creation at 9 AM daily
      0 9 * * * /path/to/create_replicas.sh
      
      # Schedule droplet replicas deletion at 5 PM daily
      0 17 * * * /path/to/destroy_replicas.sh
      

An alternative approach would be to just use the doctl CLI:

Prerequisites

  1. DigitalOcean Account: Ensure you have an active DigitalOcean account and API access token.
  2. doctl: Install doctl on your local machine.

Installation

  1. Install doctl:

  2. Authenticate doctl:

    doctl auth init
    

Steps

1. Create a Script to Create Droplet Replicas

  1. Create a Shell Script for Creating Droplets (create_replicas.sh):

    #!/bin/bash
    
    # Configuration variables
    DROPLET_NAME="base-droplet"
    REGION="nyc3"
    IMAGE="ubuntu-20-04-x64"
    SIZE="s-1vcpu-1gb"
    REPLICA_COUNT=3
    
    for i in $(seq 1 $REPLICA_COUNT); do
      doctl compute droplet create "${DROPLET_NAME}-replica-${i}" \
        --region $REGION \
        --image $IMAGE \
        --size $SIZE \
        --wait
    done
    
  2. Make the Script Executable:

    chmod +x create_replicas.sh
    

2. Create a Script to Delete Droplet Replicas

  1. Create a Shell Script for Deleting Droplets (destroy_replicas.sh):

    #!/bin/bash
    
    # Configuration variables
    DROPLET_NAME="base-droplet"
    REPLICA_COUNT=3
    
    for i in $(seq 1 $REPLICA_COUNT); do
      DROPLET_ID=$(doctl compute droplet list --format ID,Name --no-header | grep "${DROPLET_NAME}-replica-${i}" | awk '{print $1}')
      if [ -n "$DROPLET_ID" ]; then
        doctl compute droplet delete $DROPLET_ID --force
      fi
    done
    
  2. Make the Script Executable:

    chmod +x destroy_replicas.sh
    

3. Schedule the Scripts Using cron

  1. Open the crontab editor:

    crontab -e
    
  2. Add the following lines to schedule the creation and deletion of droplets:

    # Schedule droplet replicas creation at 9 AM daily
    0 9 * * * /path/to/create_replicas.sh
    
    # Schedule droplet replicas deletion at 5 PM daily
    0 17 * * * /path/to/destroy_replicas.sh
    

Feel free to share more details on the exact goal that you want to achieve and I will be happy to advise you furhter.

Best,

Bobby

alexdo
Site Moderator
Site Moderator badge
June 27, 2024

Heya, @sorcfllow

Another thing to mention is that you can use the DigitalOcean API after the droplet is removed to clear the snapshots as well.

You can check this article on how to set this:

https://docs.digitalocean.com/products/snapshots/how-to/delete/

Regards

KFSys
Site Moderator
Site Moderator badge
May 15, 2024

Heya,

You can utilize doctl. Here’s a step-by-step guide to setting this up:

1. Installing doctl:

First, ensure you have doctl installed. If not, you can install it by following the instructions on the DigitalOcean documentation page.

2. Authenticating doctl:

Once installed, you need to authenticate doctl with your DigitalOcean account using an API token:

doctl auth init

You will be prompted to enter your DigitalOcean API token.

3. Creating a Snapshot of the Droplet:

Before creating a replica, you need a snapshot of the Droplet. This can be automated using doctl. Here’s how you can create a snapshot:

doctl compute droplet-action snapshot <droplet-id> --snapshot-name "snapshot-name"

Replace <droplet-id> with your Droplet’s ID and "snapshot-name" with the desired name of your snapshot.

4. Creating a Replica Droplet from the Snapshot:

Once the snapshot is created, you can create new Droplets based on this snapshot:

doctl compute droplet create "replica-droplet-name" --size "s-1vcpu-2gb" --image "snapshot-name" --region "nyc1"

Modify "replica-droplet-name", "s-1vcpu-2gb", "snapshot-name", and "nyc1" according to your requirements.

5. Scheduling the Creation and Deletion:

To automate the creation and deletion of the replicas at scheduled times, you can use cron on Linux or Task Scheduler on Windows.

Example of a cron job:

  • Create a Bash script (manage_droplets.sh):
#!/bin/bash

# Create Droplet from snapshot
doctl compute droplet create "replica-droplet-name" --size "s-1vcpu-2gb" --image "snapshot-name" --region "nyc1"

# Sleep for the scheduled duration
sleep <time_in_seconds>

# Get Droplet ID
DROPLET_ID=$(doctl compute droplet list --format ID,Name --no-header | grep "replica-droplet-name" | awk '{print $1}')

# Delete Droplet
doctl compute droplet delete $DROPLET_ID --force
  • Edit your crontab:
crontab -e
  • Add a line to run your script at a scheduled time:
0 4 * * * /path/to/manage_droplets.sh

This example schedules the script to run daily at 4:00 AM.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel