1. Docker Stack简介

单机模式下,我们可以使用 Docker Compose 来编排多个服务,而 Docker Swarm 只能实现对单个服务的简单部署。本文的主角 Docker Stack ,通过 Docker Stack 我们只需对已有的 docker-compose.yml 配置文件稍加改造就可以完成 Docker 集群环境下的多服务编排。

2. Docker Stack集群搭建案例

部署一个投票APP,包含如下服务:

  • 5个应用服务:vote、redis、worker,db,result
  • 工具服务:portainer 和 visualizer
    在这里插入图片描述

首先创建一个 docker-compose.yml 文件,使用 Docker Compose v3 语法

version: "3"
services:

  redis:
    image: redis:alpine
    ports:
      - "6379"
    networks:
      - frontend
    deploy:
      replicas: 2
      update_config:
        parallelism: 2
        delay: 10s
      restart_policy:
        condition: on-failure

  db:
    image: postgres:9.4
    volumes:
      - db-data:/var/lib/postgresql/data
    networks:
      - backend
    deploy:
      placement:
        constraints: [node.role == manager]

  vote:
    image: dockersamples/examplevotingapp_vote:before
    ports:
      - 5000:80
    networks:
      - frontend
    depends_on:
      - redis
    deploy:
      replicas: 2
      update_config:
        parallelism: 2
      restart_policy:
        condition: on-failure

  result:
    image: dockersamples/examplevotingapp_result:before
    ports:
      - 5001:80
    networks:
      - backend
    depends_on:
      - db
    deploy:
      replicas: 1
      update_config:
        parallelism: 2
        delay: 10s
      restart_policy:
        condition: on-failure

  worker:
    image: dockersamples/examplevotingapp_worker
    networks:
      - frontend
      - backend
    deploy:
      mode: replicated
      replicas: 1
      labels: [APP=VOTING]
      restart_policy:
        condition: on-failure
        delay: 10s
        max_attempts: 3
        window: 120s
      placement:
        constraints: [node.role == manager]

  visualizer:
    image: dockersamples/visualizer:stable
    ports:
      - "8080:8080"
    stop_grace_period: 1m30s
    volumes:
      - "/var/run/docker.sock:/var/run/docker.sock"
    deploy:
      placement:
        constraints: [node.role == manager]
        
  portainer:
    image: portainer/portainer
    ports:
      - "9000:9000"
    volumes:
      - "/var/run/docker.sock:/var/run/docker.sock"
    deploy:
      replicas: 1
      placement:
        constraints: [node.role == manager]

networks:
  frontend:
  backend:

volumes:
  db-data:

在manager节点部署服务:

[root@swarm-manager example-voting-app]# docker stack deploy example --compose-file=docker-compose.yml 
Creating network example_frontend
Creating network example_default
Creating network example_backend
Creating service example_result
Creating service example_worker
Creating service example_visualizer
Creating service example_portainer
Creating service example_redis
Creating service example_db
Creating service example_vote

部署完成:

[root@swarm-manager example-voting-app]# docker stack services example
ID                  NAME                 MODE                REPLICAS            IMAGE                                          PORTS
cdmrhfag3zje        example_vote         replicated          2/2                 dockersamples/examplevotingapp_vote:before     *:5000->80/tcp
csjcltpwvpzs        example_visualizer   replicated          1/1                 dockersamples/visualizer:stable                *:8080->8080/tcp
ihys1qvpz4i5        example_worker       replicated          1/1                 dockersamples/examplevotingapp_worker:latest   
l0ufhl8n0ozg        example_portainer    replicated          1/1                 portainer/portainer:latest                     *:9000->9000/tcp
m2icguw00ls9        example_db           replicated          1/1                 postgres:9.4                                   
pu2h7rqzpaa0        example_redis        replicated          2/2                 redis:alpine                                   *:30001->6379/tcp
tom14od7paf1        example_result       replicated          1/1                 dockersamples/examplevotingapp_result:before   *:5001->80/tcp
[root@swarm-manager example-voting-app]# docker stack ps example
ID                  NAME                   IMAGE                                          NODE                DESIRED STATE       CURRENT STATE           ERROR                       PORTS
loj1h2dk9l1s        example_vote.1         dockersamples/examplevotingapp_vote:before     swarm-worker3       Running             Running 2 minutes ago                               
kp4n980ldv49        example_db.1           postgres:9.4                                   swarm-manager       Running             Running 2 minutes ago                               
0hf6d80nwwyf        example_worker.1       dockersamples/examplevotingapp_worker:latest   swarm-manager       Running             Running 2 minutes ago                               
rnjnwhl86ro6        example_redis.1        redis:alpine                                   swarm-worker1       Running             Running 2 minutes ago                               
1376pp1lvrly        example_portainer.1    portainer/portainer:latest                     swarm-manager       Running             Running 2 minutes ago                               
xj9a9iu9g2s8        example_worker.1       dockersamples/examplevotingapp_worker:latest   swarm-manager       Shutdown            Failed 2 minutes ago    "task: non-zero exit (1)"   
g5ts3oiibrhn        example_visualizer.1   dockersamples/visualizer:stable                swarm-manager       Running             Running 3 minutes ago                               
kgcttt1xigu2        example_worker.1       dockersamples/examplevotingapp_worker:latest   swarm-manager       Shutdown            Failed 3 minutes ago    "task: non-zero exit (1)"   
jpqdinh0st7b         \_ example_worker.1   dockersamples/examplevotingapp_worker:latest   swarm-manager       Shutdown            Failed 3 minutes ago    "task: non-zero exit (1)"   
ff9bq1vcgbz9        example_result.1       dockersamples/examplevotingapp_result:before   swarm-manager       Running             Running 2 minutes ago                               
q27wp9nna0yc        example_vote.2         dockersamples/examplevotingapp_vote:before     swarm-worker1       Running             Running 2 minutes ago                               
s6hvxlgdz77v        example_redis.2        redis:alpine                                   swarm-worker2       Running             Running 2 minutes ago 

浏览器访问:

vote             ip:5000
result           ip:5001  
portainer        ip:9000     
visualizer 	     ip:8080    

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

3. Docker Stack和Docker Compose区别

  • Docker stack会忽略了“构建”指令,无法使用stack命令构建新镜像,它是需要镜像是预先已经构建好的。 所以docker-compose更适合于开发场景;
  • Docker Compose是一个Python项目,在内部,它使用Docker API规范来操作容器。所以需要安装Docker -compose,以便与Docker一起在您的计算机上使用;
  • Docker Stack功能包含在Docker引擎中。你不需要安装额外的包来使用它,docker stacks 只是swarm mode的一部分。
  • Docker stack不支持基于第2版写的docker-compose.yml ,也就是version版本至少为3。然而Docker Compose对版本为2和3的 文件仍然可以处理;
  • docker stack把docker compose的所有工作都做完了,因此docker stack将占主导地位。同时,对于大多数用户来说,切换到使用docker stack既不困难,也不需要太多的开销。如果您是Docker新手,或正在选择用于新项目的技术,请使用docker stack。

4. Docker Stack常用命令

命令描述
docker stack deploy部署新的堆栈或更新现有堆栈
docker stack ls列出现有堆栈
docker stack ps列出堆栈中的任务
docker stack rm删除一个或多个堆栈
docker stack services列出堆栈中的服务
Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐