简体   繁体   English

AWS EB:负载平衡和容器化数据库

[英]AWS EB: load balancing and containerized database

I have a Rails app running with Docker on a EBS instance.我有一个在 EBS 实例上与 Docker 一起运行的 Rails 应用程序。 It's a multi container app, so I have one container for my Rails app, and another one for my PostgreSQL database.这是一个多容器应用程序,所以我的 Rails 应用程序有一个容器,我的 PostgreSQL 数据库有另一个容器。

I avoided using RDS for my database because of the expensive monthly price.由于每月价格昂贵,我避免将 RDS 用于我的数据库。

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?在这种情况下,是否会有 2 个不同的数据源不同步?

  • I'm running a cron script in my web app container that backs up my database every hour.我在我的 Web 应用程序容器中运行一个 cron 脚本,它每小时备份一次我的数据库。 If 2 instances are up because of the load balancer, is the script going to be executed twice?如果 2 个实例因为负载均衡器而启动,脚本是否会执行两次?

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. AWS 负载均衡器本身不包含任何其他实例,因此如果您只是在 EC2 实例前面放置一个 ELB 或 ALB 以使其可从外部访问,则不会发生任何变化。

                           +-----------------------------+
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.这可能意味着不要通过 Docker Compose 启动数据库并在config/database.yml文件中指向它。 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.如果您之前需要一个 4 核 m5.xlarge 用于此设置,您可以将其拆分为两个 2 核 m5.large 实例。 This last diagram also looks very similar to what an RDS-based setup would look like;最后一张图看起来也与基于 RDS 的设置非常相似; 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).您必须确保像 cron 作业这样的事情只执行一次(或者如果它们运行多次,它们不会相互干扰)。 Neither Docker nor AWS directly helps with this. Docker 和 AWS 都没有直接帮助解决这个问题。

If you're willing to invest in learning Kubernetes, running this under EKS can help some of the issues here.如果您愿意投资学习 Kubernetes,在 EKS 下运行它可以帮助解决这里的一些问题。 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.您可以使用本地 Kubernetes 概念,如 LoadBalancer 类型服务、StatefulSets 来运行数据库,以及 Kubernetes CronJobs 以确保它们只运行一次。 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.这与您可能在 Docker Compose 中所做的设置截然不同,但您可以使用 Docker Desktop Kubernetes 或单节点 Kubernetes 安装(如 minikube 或 kind)在本地进行试验。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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