In a distributed application, different pieces of the app are called “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.
服务建立在容器之上,它定义了运行容器的数量,分配给容器运行的资源,暴露于外的端口等。
Luckily it’s very easy to define, run, and scale services with the Docker platform -- just write a docker-compose.yml file.
这一系列工作都可以通过定义docker-compose.yml完成。
#docker-compose.yml
version: "3"
services:
web:
# replace username/repo:tag with your name and image details
image: kevinacoder/friendlyhello
deploy:
#run 5 instance of container
replicas: 5
#limit resource of each running container
resources:
limits:
#10% of a single core of CPU time
cpus: "0.1"
memory: 50M
#immediately restart container if failed
restart_policy:
condition: on-failure
#Map port 4000 on the host to web’s port 80.
ports:
- "4000:80"
#Define the webnet network with the default settings (which is a load-balanced overlay network).
networks:
- webnet
networks:
webnet:
初始化Docker Swarm,用YML文件启动服务。
docker swarm init
>Swarm initialized: current node (ou9c3obmu0wbf6ho98awpyvlt) is now a manager.
#run docker service with name getstartedlab
docker stack deploy -c docker-compose.yml getstartedlab
>Creating network getstartedlab_webnet
>Creating service getstartedlab_web
docker service ls
>ID NAME MODE REPLICAS IMAGE PORTS
>wzjyoehxsy6g getstartedlab_web replicated 5/5 kevinacoder/friendlyhello:latest *:4000->80/tcp