简体   繁体   English

我应该运行多少个 Docker 容器

[英]How many Docker containers should I run

Docker tutorials often say I can run more containers in order to increase the availability of my webapp. Docker 教程经常说我可以运行更多的容器来增加我的 webapp 的可用性。 However, that's a really broad statement and doesn't explain how many containers should I actually run in different situations.但是,这是一个非常广泛的陈述,并没有解释在不同情况下我应该实际运行多少个容器。 How do I know if I need 1, 5, 10, 100 or 1000 instances of container?我如何知道我是否需要 1、5、10、100 或 1000 个容器实例?

Example - I have just one server and I'm running nginx and php-fpm with docker.示例 - 我只有一台服务器,我正在运行 nginx 和带有 docker 的 php-fpm。 One container for each of them.每人一个容器。 With this simple setup, my webapp works and seems responsive.通过这个简单的设置,我的 web 应用程序可以正常工作并且看起来反应灵敏。 Would I benefit in any way from spinning up additional php-fpm replicas (on the same server)?我会从增加额外的 php-fpm 副本(在同一台服务器上)中受益吗? Lets say one container for nginx and 10 containers for php-fpm.假设一个容器用于 nginx 和 10 个容器用于 php-fpm。

It's my understanding that 10 containers of php-fpm would simply split the amount of available computing resources between them.据我了解,10 个 php-fpm 容器只会在它们之间分配可用计算资源的数量。 So I can have either one container with lots of resources or 10 containers with fewer resources.因此,我可以拥有一个具有大量资源的容器或 10 个资源较少的容器。 What's better and why?什么更好,为什么?

There are pros and cons for both cases.两种情况各有利弊。

If you are running 1 Pod of your app with lots of resources, you have 1 point of failure.如果您正在运行具有大量资源的应用程序的 1 个 Pod,则您有 1 个故障点。 If your pod dies (and at one point it will die);如果您的 pod 死了(并且在某一时刻它会死); for example during a rolling update, you are going to have a small downtime.例如,在滚动更新期间,您将有一小段停机时间。

If you have many Pods running a replica of your app, take into consideration that these are containers, so there is a base image and dependencies, besides your app.如果您有许多 Pod 运行您的应用程序的副本,请考虑到这些是容器,因此除了您的应用程序之外,还有一个基本映像和依赖项。 So every replica is one more base image plus dependencies you are deploying, therefor that's more resource you are spending on your app.因此,每个副本都是一个基础映像加上您正在部署的依赖项,因此您在应用程序上花费的资源更多。

Usually, it is better to have more then one replica, but you shouldn't go crazy with it.通常,最好拥有一个以上的副本,但您不应该为它疯狂。 Make a research of what's consuming your app, set the right resource requests.研究什么在消耗您的应用程序,设置正确的资源请求。 Set up a HPA for better resiliency and better management of the cluster resources.设置 HPA 以获得更好的弹性和更好地管理集群资源。

How do I know if I need 1, 5, 10, 100 or 1000 instances of container?我如何知道我是否需要 1、5、10、100 或 1000 个容器实例?

Its totally depend on your use-case or system load.它完全取决于您的用例或系统负载。 If you have a hundred of the users then 1 container might be enough.如果您有一百个用户,那么1容器可能就足够了。

But one can not simply answer this question without proper load test .但是如果没有适当的负载测试,就不能简单地回答这个问题。

So I can have either one container with lots of resources or 10 containers with fewer resources.因此,我可以拥有一个具有大量资源的容器或 10 个资源较少的容器。 What's better and why?什么更好,为什么?

One container with fewer resources is recommend with many cloud provider as AWS recommend to have 300 to 500 MB memory so you can run multiple containers on one instance.许多云提供商建议使用一个资源较少的容器,因为 AWS 建议拥有 300 到 500 MB memory,这样您就可以在一个实例上运行多个容器。

define hard and/or soft memory limits in MiB for your container.为您的容器定义以 MiB 为单位的硬和/或软 memory 限制。 Hard and soft limits correspond to the memory and memoryReservation parameters, respectively, in task definitions.硬限制和软限制分别对应于任务定义中的memorymemoryReservation参数。 ECS recommends 300-500 MiB as a starting point for web applications. ECS 建议将 300-500 MiB 作为 web 应用程序的起点。

In a production environment, you'd want to set up some sort of monitoring system to report error rates and request latency.在生产环境中,您需要设置某种监控系统来报告错误率和请求延迟。 If requests are starting to get unacceptably slow, you'd stand up more replicas, and more hardware.如果请求开始变得慢得令人无法接受,那么您需要建立更多的副本和更多的硬件。 It's not usually useful to run more replicas than your hardware supports or than your current load justifies.运行比硬件支持或当前负载合理的更多副本通常没有用。

The flip side of this is that running 2 replicas is much harder than running 1;另一方面,运行 2 个副本比运行 1 个要困难得多。 running 3, or 10, isn't much harder than running 2. So I'd suggest:运行 3 或 10 并不比运行 2 难。所以我建议:

  • If running just 1 replica works for your traffic load and availability needs, just run 1 replica.如果仅运行 1 个副本可以满足您的流量负载和可用性需求,则只需运行 1 个副本。
  • If you're running in an environment like Kubernetes that provides load balancing and zero-downtime upgrades as part of the environment, run 2 or 3 replicas;如果您在像 Kubernetes 这样提供负载平衡和零停机升级作为环境一部分的环境中运行,请运行 2 或 3 个副本; don't go overboard, but you gain some resiliency if 1 copy crashes if there's another one running to fail over to.不要 go 落水,但是如果有另一个副本正在运行以进行故障转移,则如果 1 个副本崩溃,您将获得一些弹性。
  • Run the fewest number of replicas required to meet your response time requirements.运行满足您的响应时间要求所需的最少数量的副本。

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

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