简体   繁体   English

当多个其他服务正在使用Redis和Elastic Search之类的服务时,是否应该为它们创建单独的Docker容器?

[英]Should you create separate docker containers for services like redis and elastic search when they are being used by more than one other service?

I have my entire stack in a docker compose container setup. 我将整个堆栈放在docker compose容器设置中。 Currently, load is such that it can all run on a single instance. 当前,负载可以全部在单个实例上运行。 I have two separate applications and they both use redis and elastic search. 我有两个单独的应用程序,它们都使用Redis和弹性搜索。

I have seen people suggesting that in cases like MySQL, proper container theory suggests that you should have two separate containers for two separate databases, if you have two separate applications using them. 我见过有人建议,在类似MySQL的情况下,正确的容器理论建议,如果您有两个使用它们的独立应用程序,则应该为两个独立的数据库拥有两个独立的容器。

Which I think is fine for MySQL because my understanding is that separate instances of MySQL doesnt really add much memory or processor overhead. 我认为这对MySQL很好,因为我的理解是,单独的MySQL实例实际上并不会增加太多的内存或处理器开销。

I'm wondering if this same strategy should apply to redis and elasticsearch. 我想知道是否应该将相同的策略应用于redis和elasticsearch。 My understanding is that both of these applications can come with considerable overhead. 我的理解是,这两个应用程序都可能带来相当大的开销。 So it seems like it might be inefficient to run more than one instance of them. 因此,似乎无法运行多个实例。

It's an interesting question, but I'm not sure there is an universal answer to this. 这是一个有趣的问题,但是我不确定对此是否有一个普遍的答案。 It mostly depends on your situation. 这主要取决于您的情况。

However, there are advantages and drawbacks you must know if you are using a unique container for multiple applications. 但是,如果您将唯一的容器用于多个应用程序,则必须了解其优缺点。 As an example, let's say you have only 2 applications containers : A and B , and a shared DB container, whatever the technology behind. 举个例子,假设您只有两个应用程序容器: AB ,以及一个共享的DB容器,无论使用哪种技术。

Advantages 好处

  • resource usage is limited. 资源使用受到限制。 Nonetheless, as you states in your question, if DB container overhead is not that important, then it's not really an advantage 但是,正如您在问题中指出的那样,如果数据库容器开销不是那么重要,那么这并不是真正的优势。

Drawbacks 缺点

If A and B are independent applications, then the main disadvantage by sharing DB is that you break that independency and tightly couple your applications via DB : 如果AB是独立的应用程序,则共享DB的主要缺点是您将打破这种独立性,并通过DB紧密耦合您的应用程序:

  • you cannot update independently the DB container. 您不能独立更新数据库容器。 Version of DB needs to be aligned for both applications. 需要针对两个应用程序调整数据库版本。 If A requires a new version of DB (new features needed for example), then DB must be upgraded, potentially breaking B 如果A需要新版本的DB (例如,需要新功能),则必须升级DB ,从而可能破坏B
  • configuration of DB cannot be different for A and B : if A is issuing more writes than read, and if B is intensively reading data, then you probably won't find a perfect configuration for both usages 对于ABDB的配置不能不同:如果A发出的写操作多于读取的操作,并且如果B密集地读取数据,那么您可能找不到两种用法的理想配置
  • crash of DB have impacts on both applications : A could even crash B by crashing DB 数据库崩溃对两个应用程序都有影响: A甚至可能通过使数据库崩溃而崩溃B
  • security concerns : even if A and B have separate database instances in the DB , A could possibly access B database instance, unless you're setting up different access/roles; 安全问题:即使AB具有在数据库单独的数据库实例中可能可能进入菜单B数据库实例,除非您设置不同的访问/角色; it's probably easier here to have one container per application, and don't worry about access if they are on the same network (and if DB cannot be accessed from outside, of course) 在这里,每个应用程序只有一个容器可能更容易,并且如果它们位于同一网络中,也不必担心访问(当然,如果不能从外部访问数据库
  • you have to put A , B and DB services inside the same docker-compose file 您必须将ABDB服务放在同一个docker-compose文件中

Conclusion 结论

If A and B are already tightly coupled apps, then you can probably go for 1 DB . 如果AB已经是紧密耦合的应用程序,那么您可能可以选择1 DB If you don't have many resources, you can also share DB . 如果您没有很多资源,也可以共享DB But don't forget that by doing this, you couple your apps, which you probably doesn't want. 但是不要忘记这样做,您可以耦合应用程序,而这可能是您不想要的。 Otherwise, the cleanest solution is to go for 1 DB per application. 否则,最干净的解决方案是每个应用程序使用1个DB

The main benefit I see which comes from having all linked services in the docker compose stack is that docker will then ensure that all required services are up. 我看到的主要好处是,泊坞窗组成堆栈中具有所有链接的服务,然后泊坞窗将确保所有必需的服务正常运行。 However with services like redis and elastic it is fine to have them installed stand-alone with the application just pointing to them via environment variables passed in the docker compose file. 但是,使用redis和elastic之类的服务时,最好将它们与应用程序一起独立安装,仅通过泊坞窗组成文件中传递的环境变量指向它们。

eg 例如

 myapp:
    image: myawesomerepo/myapp:latest
    depends_on:
     - someother_svc_in_same_docker_compose
    environment:
      - DB_HOST=172.17.2.73
      - REDIS_HOST=172.17.2.103
      - APP_ENV=QA
      - APM_ENABLE=false
      - APM_URL=http://172.17.2.103:8200
      - CC_HOST=cc-3102
    volumes:
      - /opt/deploy/cc/config:/server/app/config
      - /opt/deploy/cc/slogs:/server/app/logs
    command: node ./app/scheduler/app.js

In the future if you decide you want to have these services hosted, for example, you just need to point the URL in the right direction. 例如,将来如果您决定要托管这些服务,则只需将URL指向正确的方向即可。

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

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