入门-service-第二层

前提

  • dockers version:1.13或者更高(17.03.1-ce)
  • 安装Docker Compose

理解service

在一个分布式应用中,不同的部分都被称为service

services实际上是containers in production。一个service启动一个image,它规定了image的运行方式(使用哪些端口,需要多少个container的复制集才能满足service等)。

docker-compose.yml文件可以很简单的定义、运行、规划services

docker-compose.yml

可以把该文件保存在任何位置。确保image已经上传。

version: "3"
services:
  web:
    # replace username/repo:tag with your name and image details
    image: username/repository:tag
    deploy:
      replicas: 5
      resources:
        limits:
          cpus: "0.1"
          memory: 50M
      restart_policy:
        condition: on-failure
    ports:
      - "80:80"
    networks:
      - webnet
networks:
  webnet:
  • registry拉取image
  • 运行5个image的实例作为服务,称为web,限制每个使用最多10%的CPU和50M的内存
  • 如果停止运行立即重新启动container
  • 映射本地80端口到web的80端口
  • 指导webcontainers通过负载均衡网络webnet共享80端口。(在内部,containers will publish to web’s port 80 at an ephemeral port)
  • 使用默认设置定义webnet网络

运行新的负债均衡的app

docker swarm init

# app name: getstartedlab
docker stack deploy -c docker-compose.yml getstartedlab

# See a list of the five containers you just launched
docker stack ps getstartedlab

Scale(规划) the app

可以修改replicas,保存,重新运行docker stack deploy:

docker stack deploy -c docker-compose.yml getstartedlab

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

摧毁app和swarm

docker stack rm getstartedlab

This removes the app, but our one-node swarm is still up and running (as shown by docker node ls). Take down the swarm with docker swarm leave --force.

docker stack ls              # List all running applications on this Docker host
docker stack deploy -c    # Run the specified Compose file
docker stack services        # List the services associated with an app
docker stack ps    # List the running containers associated with an app
docker stack rm                              # Tear down an application

你可能感兴趣的:(入门-service-第二层)