创建docker swarm应用

下面以docker官网上的创建vote投票示例来说明如何创建一个docker swarm的应用。

在进行如下步骤时,你需要保证已经部署并正常运行着一个docker swarm集群。

在这个应用中你将学到

  • 通过创建docker-stack.yml和使用docker stack deploy命令来部署应用
  • 使用visualizer来查看应用的运行时
  • 更新docker-stack.ymlvote镜像重新部署和发布vote 应用
  • 使用Compose Version 3

vote-app-diagram

需要使用到的images

Service 描述 Base image
vote Presents the voting interface via port 5000. Viewable at :5000 Based on a Python image, dockersamples/examplevotingapp_vote
result Displays the voting results via port 5001. Viewable at :5001 Based on a Node.js image, dockersamples/examplevotingapp_result
visulizer A web app that shows a map of the deployment of the various services across the available nodes via port 8080. Viewable at :8080 Based on a Node.js image, dockersamples/visualizer
redis Collects raw voting data and stores it in a key/value queue Based on a redis image, redis:alpine
db A PostgreSQL service which provides permanent storage on a host volume Based on a postgres image, postgres:9.4
worker A background service that transfers votes from the queue to permanent storage Based on a .NET image, dockersamples/examplevotingapp_worker

用到的镜像有:

  • dockersamples/examplevotingapp_vote:before
  • dockersamples/examplevotingapp_worker
  • dockersamples/examplevotingapp_result:before
  • dockersamples/visualizer:stable
  • postgres:9.4
  • redis:alpine

我们将这些images同步到我们的私有镜像仓库sz-pg-oam-docker-hub-001.tendcloud.com中。

镜像名称分别为:

  • sz-pg-oam-docker-hub-001.tendcloud.com/library/examplevotingapp_vote:before
  • sz-pg-oam-docker-hub-001.tendcloud.com/library/examplevotingapp_worker
  • sz-pg-oam-docker-hub-001.tendcloud.com/library/examplevotingapp_result:before
  • sz-pg-oam-docker-hub-001.tendcloud.com/library/visualizer:stable
  • sz-pg-oam-docker-hub-001.tendcloud.com/library/postgres:9.4
  • sz-pg-oam-docker-hub-001.tendcloud.com/library/redis:alpine

使用V3版本的compose文件

v3版本的compose与v2版本的区别

docker-stack.yml配置

  1. version: "3"
  2. services:
  3. redis:
  4. image: sz-pg-oam-docker-hub-001.tendcloud.com/library/redis:alpine
  5. ports:
  6. - "6379"
  7. networks:
  8. - frontend
  9. deploy:
  10. replicas: 2
  11. update_config:
  12. parallelism: 2
  13. delay: 10s
  14. restart_policy:
  15. condition: on-failure
  16. db:
  17. image: sz-pg-oam-docker-hub-001.tendcloud.com/library/postgres:9.4
  18. volumes:
  19. - db-data:/var/lib/postgresql/data
  20. networks:
  21. - backend
  22. deploy:
  23. placement:
  24. constraints: [node.role == manager]
  25. vote:
  26. image: sz-pg-oam-docker-hub-001.tendcloud.com/library/examplevotingapp_vote:before
  27. ports:
  28. - 5000:80
  29. networks:
  30. - frontend
  31. depends_on:
  32. - redis
  33. deploy:
  34. replicas: 2
  35. update_config:
  36. parallelism: 2
  37. restart_policy:
  38. condition: on-failure
  39. result:
  40. image: sz-pg-oam-docker-hub-001.tendcloud.com/library/examplevotingapp_result:before
  41. ports:
  42. - 5001:80
  43. networks:
  44. - backend
  45. depends_on:
  46. - db
  47. deploy:
  48. replicas: 2
  49. update_config:
  50. parallelism: 2
  51. delay: 10s
  52. restart_policy:
  53. condition: on-failure
  54. worker:
  55. image: sz-pg-oam-docker-hub-001.tendcloud.com/library/examplevotingapp_worker
  56. networks:
  57. - frontend
  58. - backend
  59. deploy:
  60. mode: replicated
  61. replicas: 1
  62. labels: [APP=VOTING]
  63. restart_policy:
  64. condition: on-failure
  65. delay: 10s
  66. max_attempts: 3
  67. window: 120s
  68. visualizer:
  69. image: sz-pg-oam-docker-hub-001.tendcloud.com/library/visualizer:stable
  70. ports:
  71. - "8080:8080"
  72. stop_grace_period: 1m30s
  73. volumes:
  74. - "/var/run/docker.sock:/var/run/docker.sock"
  75. deploy:
  76. placement:
  77. constraints: [node.role == manager]
  78. networks:
  79. frontend:
  80. backend:
  81. volumes:
  82. db-data:

部署

使用docker stack deploy命令部署vote应用。

  1. $docker stack deploy -c docker-stack.yml vote
  2. Creating network vote_backend
  3. Creating network vote_frontend
  4. Creating network vote_default
  5. Creating service vote_db
  6. Creating service vote_vote
  7. Creating service vote_result
  8. Creating service vote_worker
  9. Creating service vote_visualizer
  10. Creating service vote_redis

使用docker stack deploy部署的应用中的images必须是已经在镜像仓库中存在的,而不能像之前的docker-compose up一样,可以通过本地构建镜像后启动。

使用docker stack service vote查看应用状态

  1. $docker stack services vote
  2. ID NAME MODE REPLICAS IMAGE
  3. 5bte3o8e0ta9 vote_result replicated 2/2 sz-pg-oam-docker-hub-001.tendcloud.com/library/examplevotingapp_result:before
  4. h65a6zakqgq3 vote_worker replicated 1/1 sz-pg-oam-docker-hub-001.tendcloud.com/library/examplevotingapp_worker:latest
  5. k7xzd0adhh52 vote_db replicated 1/1 sz-pg-oam-docker-hub-001.tendcloud.com/library/postgres:9.4
  6. pvvi5qqcsnag vote_vote replicated 2/2 sz-pg-oam-docker-hub-001.tendcloud.com/library/examplevotingapp_vote:before
  7. z4q2gnvoxtpj vote_redis replicated 2/2 sz-pg-oam-docker-hub-001.tendcloud.com/library/redis:alpine
  8. zgiuxazk4ssc vote_visualizer replicated 1/1 sz-pg-oam-docker-hub-001.tendcloud.com/library/visualizer:stable

使用docker stack lsdocker stack ps vote查看stack的状态

  1. $docker stack ls
  2. NAME SERVICES
  3. vote 6
  4. $docker stack ps vote
  5. ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
  6. tcyy62bs26sp vote_worker.1 sz-pg-oam-docker-hub-001.tendcloud.com/library/examplevotingapp_worker:latest sz-pg-oam-docker-test-003.tendcloud.com Running Running 4 minutes ago
  7. tfa84y1yz00j vote_redis.1 sz-pg-oam-docker-hub-001.tendcloud.com/library/redis:alpine sz-pg-oam-docker-test-002.tendcloud.com Running Running 5 minutes ago
  8. 4yrp8e2pucnu vote_visualizer.1 sz-pg-oam-docker-hub-001.tendcloud.com/library/visualizer:stable sz-pg-oam-docker-test-001.tendcloud.com Running Running 5 minutes ago
  9. zv4dan0n9zo3 vote_worker.1 sz-pg-oam-docker-hub-001.tendcloud.com/library/examplevotingapp_worker:latest sz-pg-oam-docker-test-003.tendcloud.com Shutdown Failed 4 minutes ago "task: non-zero exit (1)"
  10. mhbf683hiugr vote_result.1 sz-pg-oam-docker-hub-001.tendcloud.com/library/examplevotingapp_result:before sz-pg-oam-docker-test-001.tendcloud.com Running Running 5 minutes ago
  11. slf6je49r4v1 vote_vote.1 sz-pg-oam-docker-hub-001.tendcloud.com/library/examplevotingapp_vote:before sz-pg-oam-docker-test-002.tendcloud.com Running Running 5 minutes ago
  12. mqypecrgriyq vote_db.1 sz-pg-oam-docker-hub-001.tendcloud.com/library/postgres:9.4 sz-pg-oam-docker-test-001.tendcloud.com Running Running 4 minutes ago
  13. 6n7856nsvavn vote_redis.2 sz-pg-oam-docker-hub-001.tendcloud.com/library/redis:alpine sz-pg-oam-docker-test-003.tendcloud.com Running Running 5 minutes ago
  14. pcrfnm20jf0r vote_result.2 sz-pg-oam-docker-hub-001.tendcloud.com/library/examplevotingapp_result:before sz-pg-oam-docker-test-002.tendcloud.com Running Running 4 minutes ago
  15. ydxurw1jnft6 vote_vote.2 sz-pg-oam-docker-hub-001.tendcloud.com/library/examplevotingapp_vote:before sz-pg-oam-docker-test-003.tendcloud.com Running Running 5 minutes ago

检查

当vote应用成功部署后,在浏览器中访问visualizer所部属到的主机的8080端口http://sz-pg-oam-docker-hub-001.tendcloud.com:8080可以看到如下画面:

visualizer.jpg

Visualizer用于显示服务和主机的状态。

投票界面

在浏览器中访问examplevotingapp_vote所部属到的主机的5000端口http://sz-pg-oam-docker-hub-001.tendcloud.com:5000可以看到如下画面:

vote_web

给猫投一票。cat

结果界面

在浏览器中访问examplevotingapp_result所部属到的主机的5001端口http://sz-pg-oam-docker-hub-001.tendcloud.com:5001可以看到如下画面.

vote_result

总结

至此整个应用已经完整的部署在docker上了,并验证正常运行。

怎么样,是用docker来部署一个应用是不是很简单?

其实后期的维护、升级、扩展都很简单,后面会详细的说明。