Skip to content

Instantly share code, notes, and snippets.

@anthonysterling
Last active July 13, 2023 01:11
Show Gist options
  • Save anthonysterling/9c680b2e1295adf58a34fdaa585c474c to your computer and use it in GitHub Desktop.
Save anthonysterling/9c680b2e1295adf58a34fdaa585c474c to your computer and use it in GitHub Desktop.
Simple bash script to create 4 Digital Ocean Droplets, each with Persistent Volumes, with a dedicated Load Balancer
#!/usr/bin/env bash
set -o errexit
set -o nounset
set -o pipefail
function log {
local now=$(date +'%Y-%m-%d %H:%M:%S')
echo "[$now] $1"
}
function create_droplet {
doctl compute droplet create "$1" --wait --size "$2" --image "$3" --region "$4" --ssh-keys "$5" --tag-names "$6"
}
function create_volume {
doctl compute volume create "$1" --region "$2" --size "$3" --desc "Volume for $1"
}
function attach_volume_to_droplet {
local volume=$(doctl compute volume list | grep "$1" | awk '{ print $1 }')
local droplet=$(doctl compute droplet list | grep "$2" | awk '{ print $1 }')
doctl compute volume-action attach "$volume" "$droplet"
}
function create_load_balancer {
doctl compute load-balancer create --name "$1" --region "$2" --tag-name "$3" --forwarding-rules "entry_protocol:http,entry_port:80,target_protocol:http,target_port:9000"
}
function delete_droplet {
doctl compute droplet delete --force "$1"
}
function delete_volume {
doctl compute volume delete "$1"
}
function delete_load_balancer {
doctl compute load-balancer delete --force "$1"
}
DROPLET_SIZE="512mb"
DROPLET_IMAGE=$(doctl compute image list | grep "16.04.2 x64" | awk '{ print $1 }')
DROPLET_REGION="fra1"
DROPLET_SSHKEY=$(doctl compute ssh-key list | grep "[email protected]" | awk '{ print $1 }')
DROPLET_TAG="minio-cluster"
VOLUME_SIZE="100GiB"
case "$1" in
install)
for idx in $(seq 1 4); do
log "Creating minio-node-$idx in $DROPLET_REGION tagged with $DROPLET_TAG"
create_droplet "minio-node-$idx" "$DROPLET_SIZE" "$DROPLET_IMAGE" "$DROPLET_REGION" "$DROPLET_SSHKEY" "$DROPLET_TAG"
log "Creating Volume for minio-node-$idx"
create_volume "$DROPLET_TAG-volume-node-$idx" $DROPLET_REGION "$VOLUME_SIZE"
log "Attaching Volume for minio-node-$idx to Droplet"
attach_volume_to_droplet "$DROPLET_TAG-volume-node-$idx" "minio-node-$idx"
done;
log "Creating $DROPLET_TAG load balancer for Droplets tagged with $DROPLET_TAG"
create_load_balancer "$DROPLET_TAG" "$DROPLET_REGION" "$DROPLET_TAG"
;;
uninstall)
log "Deleting all Droplets tagged with $DROPLET_TAG"
for droplet in $(doctl compute droplet list | grep "$DROPLET_TAG" | awk '{ print $1 }'); do
log "Deleting Droplet $droplet"
delete_droplet "$droplet";
done;
log "Deleting all Volumes with $DROPLET_TAG in their name"
for volume in $(doctl compute volume list | grep "$DROPLET_TAG" | awk '{ print $1 }'); do
log "Deleting Volume $volume"
delete_volume "$volume";
done;
log "Deleting all Load Balancers named $DROPLET_TAG"
for lb in $(doctl compute load-balancer list | grep "$DROPLET_TAG" | awk '{ print $1 }'); do
log "Deleting Load Balancer $lb"
delete_load_balancer "$lb";
done;
;;
*)
echo "Usage: cluster {install|uninstall}" >&2
exit 3
;;
esac
@anthonysterling
Copy link
Author

The plan is to provision a Distributed Minio cluster, see http://docs.minio.io/docs/distributed-minio-quickstart-guide

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment