简体   繁体   English

何时使用 Docker-Compose 何时使用 Docker-Swarm

[英]When to use Docker-Compose and when to use Docker-Swarm

I'm trying to understand the differences or similarities between Docker-Compose and Docker-Swarm .我试图了解Docker-ComposeDocker-Swarm之间的差异或相似之处。

By reading the documentation I have understood that docker-compose provides a mechanism to bind different containers together and work in collaboration, as a single service (I'm guessing it's using the same functionality as --link command used to link two containers)通过阅读文档,我了解到 docker-compose 提供了一种将不同容器绑定在一起并作为单个服务协同工作的机制(我猜它使用与用于链接两个容器的--link命令相同的功能)

Also, my understanding of docker-swarm is that it allows you to manage a cluster of different docker-hosts , each of which is running several container instances of some docker-images.此外,我对 docker-swarm 的理解是,它允许您管理不同docker-hosts的集群,每个集群都运行一些 docker-images 的多个容器实例。 We could define connections as overlay-networks between different containers in the swarm (even if they across two docker-hosts in the swarm) to connect them as a unit.我们可以将连接定义为 swarm 中不同容器之间的覆盖网络(即使它们跨越 swarm 中的两个 docker-hosts)以将它们连接为一个单元。

What I'm trying to understand is has docker-swarm succeeded docker-compose and overlay networks is the new (recommended) way to connect containers?我想了解的是 docker-swarm 是否成功了 docker-compose 和覆盖网络是连接容器的新(推荐)方式?

Or is it that docker-compose is still an integral part of the entire docker family and it is expected and advisable to use it to connect containers to work in collaboration.还是说 docker-compose 仍然是整个 docker 家族中不可或缺的一部分,使用它来连接容器以进行协作是预期和可取的。 If so does docker-compose work with containers across different nodes in the swarm??如果是这样,docker-compose 是否可以与集群中不同节点的容器一起使用?

Or is it that overlay networks is for connecting containers across different hosts in the swarm and docker-compose is for creating internal links??还是覆盖网络用于连接群中不同主机之间的容器,而 docker-compose 用于创建内部链接?

Besides I also see that it is mentioned in the docker documentation that --links not recommended anymore and will be obsolete soon.此外,我还看到docker文档中提到--links不再推荐并且很快就会过时。

I'm a bit confused???我有点糊涂???

Thanks Alot!多谢!

It will probably help to start with a few definitions:从一些定义开始可能会有所帮助:

  • docker-compose : Command used to configure and manage a group of related containers. docker-compose :用于配置和管理一组相关容器的命令。 It is a frontend to the same api's used by the docker cli, so you can reproduce it's behavior with commands like docker run .它是 docker cli 使用的相同 api 的前端,因此您可以使用docker run类的命令重现它的行为。
  • docker-compose.yml : Definition file for a group of containers, used by docker-compose and now also by swarm mode. docker-compose.yml :一组容器的定义文件,由 docker-compose 使用,现在也由 swarm 模式使用。
  • swarm mode : Used to manage a group of docker engines as a single entity and provide orchestration (constantly trying to correct any differences between the current state and the target state). swarm 模式:用于将一组 docker 引擎作为单个实体进行管理并提供编排(不断尝试纠正当前状态和目标状态之间的任何差异)。
  • service : One or more containers for the same image and configuration within swarm, multiple containers provide scalability. service : swarm 中相同镜像和配置的一个或多个容器,多个容器提供可扩展性。
  • stack : One or more services within a swarm, these may be defined using a DAB or a docker-compose.yml file. stack :swarm 中的一个或多个服务,可以使用 DAB 或 docker-compose.yml 文件定义这些服务。
  • bridge network : Network managed by a single docker engine where multiple containers may communicate with each other.桥接网络:由单个 docker 引擎管理的网络,其中多个容器可以相互通信。 You may have multiple networks managed by an engine, and containers can be attached to zero or more networks.您可能有多个由引擎管理的网络,并且容器可以连接到零个或多个网络。
  • overlay network : Similar to a bridge network but spanning multiple docker engines.覆盖网络:类似于桥接网络,但跨越多个 docker 引擎。 These require a key/value store to maintain their state.这些需要一个键/值存储来维护它们的状态。 Swarm mode provides this, but if swarm mode is disabled, you may also use etcd, consul, or zookeeper. Swarm 模式提供了这一点,但如果禁用了 swarm 模式,您也可以使用 etcd、consul 或 zookeeper。
  • links : a method to connect containers together that predates the bridged network. links :一种在桥接网络之前将容器连接在一起的方法。 Its usage is no longer recommended.不再推荐其使用。
  • classic swarm : A predecessor to the integrated swarm mode that runs as a container, allows multiple engines to appear as one, but does not provide orchestration or include its own k/v store.经典 swarm :作为容器运行的集成 swarm 模式的前身,允许多个引擎显示为一个,但不提供编排或包含自己的 k/v 存储。

To answer the questions:回答问题:

has docker-swarm succeeded docker-compose and overlay networks is the new (recommended) way to connect containers? docker-swarm 是否成功了 docker-compose 和覆盖网络是连接容器的新(推荐)方式吗?

Or is it that docker-compose is still an integral part of the entire docker family and it is expected and advisable to use it to connect containers to work in collaboration.还是说 docker-compose 仍然是整个 docker 家族中不可或缺的一部分,使用它来连接容器以进行协作是预期和可取的。 If so does docker-compose work with containers across different nodes in the swarm??如果是这样,docker-compose 是否可以与集群中不同节点的容器一起使用?

They provide different functionality and will continue to both serve a purpose.它们提供不同的功能,并将继续为一个目的服务。 docker-compose cannot start containers inside swarm mode, but a newer version of the docker-compose.yml file (version 3) can be used to define a stack directly in swarm mode without using docker-compose itself. docker-compose 无法在 swarm 模式下启动容器,但是可以使用较新版本的 docker-compose.yml 文件(版本 3)直接在 swarm 模式下定义堆栈,而无需使用 docker-compose 本身。 docker-compose is needed to manage containers outside of swarm mode, on a single docker engine or with classic swarm.需要 docker-compose 在 swarm 模式之外、在单个 docker 引擎上或使用经典 swarm 来管理容器。

Or is it that overlay networks is for connecting containers across different hosts in the swarm and docker-compose is for creating internal links??还是覆盖网络用于连接群中不同主机之间的容器,而 docker-compose 用于创建内部链接?

Besides I also see that it is mentioned in the docker documentation that --links not recommended anymore and will be obsolete soon.此外,我还看到 docker 文档中提到 --links 不再推荐并且很快就会过时。

docker-compose starting with version 2 of the yml file connects multiple containers together by default with a new bridged network per project (the project defaults to the directory name). docker-compose 从 yml 文件的第 2 版开始,默认情况下将多个容器连接在一起,每个项目都有一个新的桥接网络(项目默认为目录名称)。 With classic swarm, that would default to an overlay network using an external k/v store.使用经典 swarm,这将默认为使用外部 k/v 存储的覆盖网络。 And with a swarm mode stack, this would be an overlay network.使用群模式堆栈,这将是一个覆盖网络。

Using docker networks is the preferred way to have containers communicate with each other.使用 docker 网络是让容器相互通信的首选方式。 You want a network per group of containers you wish to isolate from the rest of your docker environment.您希望每组容器都有一个网络,您希望将其与 docker 环境的其余部分隔离。 docker-compose automates this network creation, but you can also do it from the command line with docker networks create . docker-compose 自动创建此网络,但您也可以使用docker networks create从命令行执行此操作。

Linking have been largely replaced by docker networks with built-in DNS discovery.链接已在很大程度上被具有内置 DNS 发现的 docker 网络所取代。 When you remove links from your docker-compose.yml, you may need to replace them with a depends_on section to enforce container startup order.当您从 docker-compose.yml 中删除链接时,您可能需要将它们替换为depends_on部分以强制执行容器启动顺序。 Otherwise, there are very few scenarios where linking makes sense and all the usage I've seen is from someone following outdated documentation.否则,链接有意义的场景很少,我看到的所有用法都来自遵循过时文档的人。

compose or swarm or swarm overlay networks组合或群或群覆盖网络

You would find that you need to use all of the above if you're doing anything other than a demo on your laptop etc.如果您在笔记本电脑等上进行演示以外的任何其他操作,您会发现需要使用上述所有内容。

I deliberately separated out swarm & swarm overlay networks, because you need not use both, but you cannot get an overlay network without having a swarm underneath it.我特意将 swarm 和 swarm 覆盖网络分开,因为您不需要同时使用两者,但是如果没有 swarm 在它下面,您就无法获得覆盖网络。

Compose is for bringing up multiple containers together. Compose 用于将多个容器组合在一起。 Now it makes sense that they are related to each other, although they may not be.现在它们彼此相关是有道理的,尽管它们可能不是。 But let's suppose a typical case when the containers are for services that are related to each other, then you would want them to talk to each other in some way, but yet control how they talk to each other using networks.但是让我们假设一个典型的案例,当容器用于彼此相关的服务时,您会希望它们以某种方式相互通信,但又要控制它们如何使用网络相互通信。 For example, take a 3 tier app that has a webserver, appserver and db.例如,以一个具有 web 服务器、应用服务器和数据库的 3 层应用程序为例。 Let's say all three components are dockerized and you are using compose to bring them up togetherm instead of running docker run.. three times with different parameters etc. All three would come up, but you would want to control how they connect to each other.假设所有三个组件都已 dockerized,并且您正在使用 compose 将它们组合在一起,而不是运行docker run..使用不同的参数三次等。所有三个都会出现,但您需要控制它们如何相互连接。 You want the webserver to be able to talk to the appserver, but not to the db directly.您希望网络服务器能够与应用服务器通信,但不能直接与数据库通信。 And you would want the appserver to talk (ping) the db server container and also ping the web server.并且您希望应用服务器与(ping)db 服务器容器并ping web 服务器。 All connections are two way, but restricted to only those services that you want to be able to communicate with each other.所有连接都是双向的,但仅限于您希望能够相互通信的那些服务。 For such an arrangement, you would typically setup 2 networks - say frontend and backend .对于这样的安排,您通常会设置 2 个网络 - 比如frontendbackend The web and app containers are connected to the frontend network. Web 和应用程序容器连接到前端网络。 The app and db containers are connected to the backend network. app 和 db 容器连接到后端网络。 Because there is no common network between the db and web containers they cannot touch (ping) each other, which is your intent.因为 db 和 web 容器之间没有公共网络,所以它们不能相互接触(ping),这是您的意图。

Now, if you want these 3 services to be able to run on your cluster of 100's of machines, and you also want to scale across them, you would need a network that spans multiple hosts.现在,如果您希望这 3 个服务能够在由 100 台机器组成的集群上运行,并且您还希望在它们之间进行扩展,您将需要一个跨越多个主机的网络。 That is where overlay networking (in swarm) comes into picture.这就是覆盖网络(在 swarm 中)出现的地方。 Overlay networking is nothing but multi-host networking build over VxLAN technology.覆盖网络只不过是基于 VxLAN 技术构建的多主机网络。 You do not have to know about VxLAN, except that it is a standard network topology that is supported in almost all modern networking infrastructure.您不必了解 VxLAN,除了它是几乎所有现代网络基础设施都支持的标准网络拓扑。

I hope that clarifies.我希望澄清。

Edit: I did not see that you got an answer already!编辑:我没有看到你已经得到了答案!

I think you have most of the understanding correct as to what each is, but some tweaking is required.我认为您对每个是什么都有大部分正确的理解,但是需要进行一些调整。

You're correct docker-compose is to bring up multi-container applications.你是对的 docker-compose 是为了启动多容器应用程序。 Earlier you used to do docker run .. to start every container.之前你曾经用docker run ..来启动每个容器。 Usually modern applications embracing the micro-services paradigm can be made up of dozens of services and using docker run .. will get very tiresome very soon.通常,包含微服务范式的现代应用程序可以由数十个服务组成,并且使用docker run ..很快就会变得非常烦人。 Hence docker-compose allows you to express all the containers and their properties and how they connect to each other as a yaml or json file so you can manage it in an easier fashion.因此,docker-compose 允许您将所有容器及其属性以及它们如何作为yamljson文件相互连接,以便您可以更轻松地对其进行管理。

So, docker-compose is the container orchestration part in the docker ecosystem.因此,docker-compose 是 docker 生态系统中的容器编排部分。

Links are different, they are just a part of docker-compose or docker run commands and are deprecated in favor of software defined networks of which overlay networks are just one of them.链接是不同的,它们只是 docker-compose 或docker run命令的一部分,被弃用,以支持software defined networks ,其中overlay networks只是其中之一。

Swarm is the scheduling component in docker. Swarm 是 docker 中的调度组件。 What is scheduling - it is nothing but figuring out where to "place" your containers in your cluster of docker hosts.什么是调度 - 只是弄清楚将容器“放置”在 docker 主机集群中的何处。 You can have a cluster of hundreds of servers, and you may have hundreds of containers, each encapsulating a service for a dozen different applications.您可以拥有数百个服务器的集群,也可能拥有数百个容器,每个容器都为十几个不同的应用程序封装了一个服务。 Now how should these containers be distributed across your cluster of hundreds of servers, should some containers be placed only on certain hosts because they satisfy a particular criteria or maybe they should be closer to (or not) other containers which are somehow related... all these are part of the scheduling component which is performed by docker Swarm.现在,这些容器应该如何分布在数百个服务器的集群中,是否应该将某些容器仅放置在某些主机上,因为它们满足特定标准,或者它们应该更接近(或不)其他以某种方式相关的容器......所有这些都是由 docker Swarm 执行的调度组件的一部分。

I suggest you go through the getting started documentation on docker.com here: https://docs.docker.com/engine/getstarted-voting-app/我建议您在此处阅读 docker.com 上的入门文档: https ://docs.docker.com/engine/getstarted-voting-app/

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

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