简体   繁体   中英

AWS EB: load balancing and containerized database

I have a Rails app running with Docker on a EBS instance. It's a multi container app, so I have one container for my Rails app, and another one for my PostgreSQL database.

I avoided using RDS for my database because of the expensive monthly price.

It's working fine so far in a development environment, but I'm wondering about this architecture for production, because of a few things I'm not sure about:

  • if my load balancer creates a new instance, does it mean that another database container is created? In that case, are there going to be 2 different data sources that are not synchronized?

  • I'm running a cron script in my web app container that backs up my database every hour. If 2 instances are up because of the load balancer, is the script going to be executed twice?

I'm actually a real beginner with load balancing, so any tips would be greatly appreciated! Thanks for reading

An AWS load balancer doesn't in itself contain any other instances, so if you're just putting an ELB or ALB in front of an EC2 instance to make it externally accessible, nothing changes.

                           +-----------------------------+
R53: myapp.example.com     | EC2                         |
          +-----+          | +-------+    +------------+ |
--------> | ELB | -------> | | Rails | -> | PostgreSQL | |
          +-----+          | +-------+    +------------+ |
                           +-----------------------------+

If you do want to run multiple copies of the application container (for redundancy, scale, upgrades) then they have to share a single database. That probably means not launching the database via Docker Compose and pointing to it in your config/database.yml file. You do have to decide where it runs; since the database has different requirements from the application, running it on a smaller instance but with local disk might make sense.

                           +-------+
R53: myapp.example.com +-> | EC2   | --+   +------------+
          +-----+      |   | Rails |   |   | EC2        |
--------> | ELB | -----+   +-------+   +-> | PostgreSQL |
          +-----+      |   +-------+   |   +------------+
                       |   | EC2   |   |
                       +-> | Rails | --+
                           +-------+

Assuming you weren't already using the very smallest instance sizes, you can mitigate the cost of this by picking smaller instances. If you needed a 4-core m5.xlarge for this setup before, you might be able to split it into two 2-core m5.large instances. This last diagram also looks very similar to what an RDS-based setup would look like; the essential tradeoff there is whether you want to pay more to Amazon, or spend your own time installing the database and managing snapshots.

You do have to make sure things like cron jobs only execute once (or that they don't interfere with each other if they run multiple times). Neither Docker nor AWS directly helps with this.

If you're willing to invest in learning Kubernetes, running this under EKS can help some of the issues here. You can use native Kubernetes concepts like LoadBalancer-type Services, StatefulSets to run the database, and Kubernetes CronJobs to ensure they only get run once. This is a significantly different setup from what you might do in Docker Compose, but you can experiment with it locally using the Docker Desktop Kubernetes or single-node Kubernetes installations like minikube or kind.

                           +-----------------------------+
                           | EKS                         |
   LoadBalancer Service    |                             |
          +.....+          | Deployment     StatefulSet  |
--------> : ELB : -------> |   Rails    -->   PostgreSQL |
          +.....+          |   Rails                     |
                           |                             |
                           | CronJob                     |
                           +-----------------------------+
                           |    EC2   |  EC2   |   EC2   |
                           +-----------------------------+

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM