load-balancingdigital-oceandroplet

I am trying to setup load balancer on digital ocean and need some advice


How do I keep all droplets with the same content?

I have 3 droplets and I thought of copying the contents of the first one to the other 2. Is there any option that does this automatically?

Or how to create LB when my mobile app backend in on PHP & the data gets updated frequently so need help in this case.


Solution

  • It's difficult to provide specific answers because your question is quite broad.

    But...

    As you know, it's preferable to clone instances so that, behind a LB, each instance behaves identically. There are many ways to do this. To my knowledge, Digital Ocean does not provide a general-purpose mechanism1.

    NB Other cloud platforms include the concept of instance groups in which you define a template(d instance) and then have the platform automatically maintain X instances for you (adding|deleting as necessary).

    Let's decompose the problem: you have some static content (e.g. PHP, web server) and some dynamic content (e.g. your data). It would be useful to separate these in your app too.

    Static Content

    You have many options here. The easiest is probably to create a master Droplet and then make clones of the master using Snapshots. I would recommend that you script (e.g. doctl) the creation of the master so that you are able to reproduce its creation if needed. Also, once you have code, check it into source control tool (for posterity). You may also use e.g. doctl scripts to create multiple, identical Droplets from scratch.

    Because wanting to automate the creation of (cloud) resources is such a common practice, there are tools available that facilitate this. One, popular tool is Terraform and, though I've not used it (see provider with DigitalOcean, you should be to able to use it to create, not only the Droplets but also the Load-balancer. There's a learning curve but it's likely a good investment (and Terraform works beyond Digital Ocean).

    A complementary approach is to consider containerizing your application. Your deployment scripts would then be able to pull/run the container image rather than install the software. This should (!) create a more predictable deployment mechanism and it enables you to consider using a container-oriented (Linux) OS (e.g. CoreOS, RancherOS). The advantages these confer include having a smaller attach surface, easier upgrades and much simpler deployment (create container OS, run PHP app container) at the expense of you having to become familiar with them.

    Dynamic Content

    If the dynamic content is file-based, a common solution would be to use some form of shared volume that permits managing this content in one place but sharing it across multiple Droplets. It is my understanding (!?) that you can't currently share one Volume across multiple Droplets. To solve this problem you may wish to explore running something like NFS that would enable multiple Droplets to share files. Alternatively, you may be able to use Spaces. Worst case, you could copy the files across multiple Droplets but be mindful of the consequences in having potentially inconsistent results.

    If the dynamic content is in a database, you could either run the database (cluster) yourself and point each Droplet to its endpoint. Or you could use Digital Ocean's Managed Databases (service).

    1- Kubernetes

    Digital Ocean includes a Kubernetes managed service.

    This provides a way for you to deploy X instances of a solution including defining a Load-balancer and sharing (persistent) volumes, but using Kubernetes may be overkill for your needs (and would additionally require you to containerize your app).

    That said, see here and please note that this was written before DigitalOcean provided its managed Kubernetes offering; the principles remain the same.

    I recommend however that you read up on Kubernetes.

    In my opinion (!), it should be the way most cloud-based apps are deployed.