https://docs.docker.com/get-started/part3/#introduction

Introduction

In part 3, we scale our application and enable load-balancing. To do this, we must go one level up in the hierarchy of a distributed application: the service.

part3,我们将拓展应用,支持负载均衡。做到这点,我们必须使用更上级分布式应用层:the service

About services

In a distributed application, different pieces of the app are called “services.” For example, if you imagine a video sharing site, it probably includes a service for storing application data in a database, a service for video transcoding in the background after a user uploads something, a service for the front-end, and so on.

分布式应用程序中,不同的app部分称为services。如,你设想一个视频分享网站,它可能包含数据存储服务,用户上传视频转码服务,前端服务等等。

Services are really just “containers in production.” A service only runs one image, but it codifies the way that image runs—what ports it should use, how many replicas of the container should run so the service has the capacity it needs, and so on. Scaling a service changes the number of container instances running that piece of software, assigning more computing resources to the service in the process.

Services 其实是生产中containers。一个service只运行一个镜像,但定义了镜像的运行方式--端口的使用,服务需要的container副本数,等等。拓展一个服务只是改变运行软件container实例的个数,进程中指定更多的计算资源指向服务。

Luckily it’s very easy to define, run, and scale services with the Docker platform -- just write a docker-compose.yml file.

幸运的是通过Docker平台可以非常简单的定义,运行,拓展服务--只需定义一个docker-compose.yml文件。

Your first docker-compose.yml file

docker-compose.yml file is a YAML file that defines how Docker containers should behave in production.

docker-compose.yml是YAML 文件定义了Docker containers在生产中的行为。

Save this file as docker-compose.yml wherever you want. Be sure you have pushed the image you created in Part 2 to a registry, and update this .yml by replacingusername/repo:tag with your image details.

保存docker-compose到任意位置。确保你已经上传了part2中镜像。用你镜像详情替换.yml中username/repo:tag代码。

version: "3"
services:
  web:
    # replace username/repo:tag with your name and image details
    image: username/repo:tag
    deploy:
      replicas: 5
      resources:
        limits:
          cpus: "0.1"
          memory: 50M
      restart_policy:
        condition: on-failure
    ports:
      - "4000:80"
    networks:
      - webnet
networks:
  webnet:

This docker-compose.yml file tells Docker to do the following:

docker-compose.yml文件告诉Docker做以下事情:

  • Pull the image we uploaded in step 2 from the registry.

  • 从注册获取step2中我们上传的镜像

  • Run 5 instances of that image as a service called web, limiting each one to use, at most, 10% of the CPU (across all cores), and 50MB of RAM.

  • 运行5个镜像实例作为web服务,每个使用cpu10%,50M内存。

  • Immediately restart containers if one fails.

  • 当一个发生错误,立即重启containers

  • Map port 4000 on the host to web’s port 80.

  • 映射主机4000端口到web 80端口

  • Instruct web’s containers to share port 80 via a load-balanced network called webnet. (Internally, the containers themselves publish to web’s port 80 at an ephemeral port.)

  • 指示web containers通过webnet负载均衡共享80端口。(在内部,containers 发布临时端口到web 80端口)

  • Define the webnet network with the default settings (which is a load-balanced overlay network).

  • 定义webnet network使用默认设置(是负载均衡的覆盖网络)

Run your new load-balanced app 

Before we can use the docker stack deploy command we first run:

执行docker stack deploy之前,我们先运行:

docker swarm init

Note: We get into the meaning of that command in part 4. If you don’t run docker swarm init you get an error that “this node is not a swarm manager.”

注:我们将在part4中说明命令含义。如果你不执行此命令会报错“this node is not a swarm manager.”

Now let’s run it. You need to give your app a name. Here, it is set to getstartedlab:

现在我们运行deploy命令,给app定义名字,这里作getstartedlab:

docker stack deploy -c docker-compose.yml getstartedlab

Our single service stack is running 5 container instances of our deployed image on one host. Let’s investigate.

在一台主机上,我们单个服务栈运行了5个部署镜像的container实例。我们来研究下。

Get the service ID for the one service in our application:

获取应用服务service ID:

docker service ls

Look for output for the web service, prepended with your app name. If you named it the same as shown in this example, the name is getstartedlab_web. The service ID is listed as well, along with the number of replicas, image name, and exposed ports.

查看输出的web服务信息,对照你预想的app名称。如果是命名也实例一致的话,名称显示应为getstartedlab_web.同是会列出service ID,副本数,镜像,和暴漏端口。

A single container running in a service is called a task. Tasks are given unique IDs that numerically increment, up to the number of replicas you defined in docker-compose.yml. List the tasks for your service:

一个服务运行一个container称作task。tasks被指定数字自增的唯一ids,取决于docker-compose.yml中定义的副本数。列出服务tasks:

docker service ps getstartedlab_web

ps:这里也可以通过service Id查询docker service ps zmopu3p4iezh

Tasks also show up if you just list all the containers on your system, though that is not filtered by service:

tasks也可通过列出所有containers命令展示,尽管没有被服务过滤:

docker container ls -q

You can run curl -4 http://localhost:4000 several times in a row, or go to that URL in your browser and hit refresh a few times.

你可以运行curl -4 http://localhost:4000多次,也可以在浏览器访问刷新多次。

docker官网手册译part 3

Either way, the container ID changes, demonstrating the load-balancing; with each request, one of the 5 tasks is chosen, in a round-robin fashion, to respond. The container IDs match your output from the previous command (docker container ls -q).

不管怎样,container id是变化的,展示了负载均衡;每次请求,5个任务中的一个被选择,循环响应。输出的container id和你docker container ls -q命令输出id匹配。

Running Windows 10?

Windows 10 PowerShell should already have curl available, but if not you can grab a Linux terminal emulator like Git BASH, or download wget for Windowswhich is very similar.

win10PowerShell下curl可用,如果不可你可获取Linux terminal模拟器如 Git BASH,或者类似的wget for Windows工具。

Slow response times?

Depending on your environment’s networking configuration, it may take up to 30 seconds for the containers to respond to HTTP requests. This is not indicative of Docker or swarm performance, but rather an unmet Redis dependency that we address later in the tutorial. For now, the visitor counter isn’t working for the same reason; we haven’t yet added a service to persist data.

响应慢?

根据网络情况,可能花费30秒容器响应http请求。这不代表docker或swarm的性能情况,也不是没有满足redis依赖导致这里随后会讲到。现在,计数器一致没有工作的原因一致;我们还没有添加持久数据的服务。

Scale the app

You can scale the app by changing the replicas value in docker-compose.yml, saving the change, and re-running the docker stack deploy command:

你可拓展app通过改变docker-compose.yml中replicas数值,重新运行docker部署命令:

docker stack deploy -c docker-compose.yml getstartedlab

Docker performs an in-place update, no need to tear the stack down first or kill any containers.

docker执行即可更新,无需拆解stack或杀死containers

Now, re-run docker container ls -q to see the deployed instances reconfigured. If you scaled up the replicas, more tasks, and hence, more containers, are started.

再次运行docker container ls -q 查看重新设置的部署实例。如增加副本,会有更多的任务,也会有更多的containers启动。

Take down the app and the swarm

Take the app down with docker stack rm:

docker stack rm getstartedlab

Take down the swarm.

docker swarm leave --force

 It’s as easy as that to stand up and scale your app with Docker. You’ve taken a huge step towards learning how to run containers in production. Up next, you learn how to run this app as a bonafide swarm on a cluster of Docker machines.

用Docker很简单的创建和拓展app。你已经有了很大进步学习了在生产中运行containers.接下来,你会学习到如何在Docker machines集群作为集群运行app。

Note: Compose files like this are used to define applications with Docker, and can be uploaded to cloud providers using Docker Cloud, or on any hardware or cloud provider you choose with Docker Enterprise Edition.

注:docker用Compose文件定义应用,可以上传到Docker Cloud,或其他硬件及 Docker Enterprise Edition.选择的其他云提供者。

To recap, while typing docker run is simple enough, the true implementation of a container in production is running it as a service. Services codify a container’s behavior in a Compose file, and this file can be used to scale, limit, and redeploy our app. Changes to the service can be applied in place, as it runs, using the same command that launched the service: docker stack deploy.

Some commands to explore at this stage:

docker stack ls                                            # List stacks or apps
docker stack deploy -c <composefile> <appname>  # Run the specified Compose file
docker service ls                 # List running services associated with an app
docker service ps <service>                  # List tasks associated with an app
docker inspect <task or container>                   # Inspect task or container
docker container ls -q                                      # List container IDs
docker stack rm <appname>                             # Tear down an application
docker swarm leave --force      # Take down a single node swarm from the manager

相关文章: