digitalocean/droplet_kit

Add a new droplet when charge is high?

Closed this issue · 1 comments

Thanks for awesome work.

I wonder what's the best way to add a droplet to a load balancer when charge is high and remove it when charge is low?

My pseudo-code to add a droplet:

if client.droplets.all.to_a.all?  {|e| e.cpu_usage > 0.50 } 
  created = client.droplets.create(client.droplets.all.first)
  client.load_balancers.add_droplets(created.id, id: 'id')
end

My pseudo-code to remove a droplet:

if client.droplets.all.to_a.all?  {|e| e.cpu_usage < 0.50 } 
  last_added = client.droplets.all.to_a.last
  client.load_balancers.remove_droplets(last_added.id, id: 'id')
  client.droplet_actions.shutdown(droplet_id: last_added.id)
  client.droplets.delete(id: last_added.id)
end

Is my reasoning correct? Is 50% cpu usage a good threshold? What's a good interval to run this code? Every minutes? What will be the best way to get the cpu usage? Is using load averages instead of cpu usage actually better? What the best way to make sure no requests are lost when requesting droplet shutdows and removing it from the load balancer? Sorry lot of questions!

P.S.: Planning to create a load balance consisting of Ubuntu 18.04 to serve web requests. Ruby on Rails served with Puma/Nginx. Some requests take a long time to process (1-2 minutes).

Hi @hartator 👋. From what I understand you are trying to add more droplets when you application is under load and remove the droplets when it's not? If that's the case, how/when you scale will be dependant on your application. e.g. you could run out of memory before maxing your cpu. This is something you'd need to investigate for your application.

There are various ways you could get cpu, mem, etc stats. Use some library/gem, or since you mentioned using Ubuntu, you can look at the /proc directory/subdirectories.

As for the load balancing aspect, removing the target and waiting until all your requests have finished processing on the removed droplet before destroying it would be the way to minimize failed requests.

Please feel free to re-open if this didn't answer your question