By Sorcfllow
How to create replicas of running droplets for scheduled time? After schedule time droplets replicas should be remove.
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!
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:
Install Terraform:
brew install terraform
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
}
Create a Variables File (variables.tfvars
):
do_token = "YOUR_DIGITALOCEAN_API_TOKEN"
droplet_name = "base-droplet"
replica_count = 3
Initialize and Apply Terraform Configuration:
terraform init
terraform apply -var-file="variables.tfvars"
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
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
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:
doctl
on your local machine.Install doctl
:
brew install doctl
doctl
GitHub releases page and add it to your PATH.Authenticate doctl
:
doctl auth init
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
Make the Script Executable:
chmod +x create_replicas.sh
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
Make the Script Executable:
chmod +x destroy_replicas.sh
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
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
Heya,
You can utilize doctl
. Here’s a step-by-step guide to setting this up:
doctl
:First, ensure you have doctl
installed. If not, you can install it by following the instructions on the DigitalOcean documentation page.
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.
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.
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.
To automate the creation and deletion of the replicas at scheduled times, you can use cron
on Linux or Task Scheduler on Windows.
cron
job: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
crontab
:crontab -e
0 4 * * * /path/to/manage_droplets.sh
This example schedules the script to run daily at 4:00 AM.
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
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.