swarm stack 部署多 service 应用

先在swarm manager节点上安装一下 docker-compose

  1. vagrant@swarm-manager:~$ sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
  2. vagrant@swarm-manager:~$ sudo chmod +x /usr/local/bin/docker-compose

clone我们的代码仓库

  1. vagrant@swarm-manager:~$ git clone https://github.com/xiaopeng163/flask-redis
  2. Cloning into 'flask-redis'...
  3. remote: Enumerating objects: 22, done.
  4. remote: Counting objects: 100% (22/22), done.
  5. remote: Compressing objects: 100% (19/19), done.
  6. remote: Total 22 (delta 9), reused 7 (delta 2), pack-reused 0
  7. Unpacking objects: 100% (22/22), 8.60 KiB | 1.07 MiB/s, done.
  8. vagrant@swarm-manager:~$ cd flask-redis
  9. vagrant@swarm-manager:~/flask-redis$ ls
  10. Dockerfile LICENSE README.md app.py docker-compose.yml
  11. vagrant@swarm-manager:~/flask-redis$

环境清理

  1. vagrant@swarm-manager:~/flask-redis$ docker system prune -a -f

镜像构建和提交, 如果你想做这一步,可以把docker-compose.yml里的 xiaopeng163/flask-redis 改成你的dockerhub id

  1. vagrant@swarm-manager:~/flask-redis$ docker-compose build
  2. vagrant@swarm-manager:~/flask-redis$ docker image ls
  3. REPOSITORY TAG IMAGE ID CREATED SIZE
  4. xiaopeng163/flask-redis latest 5efb4fcbcfc3 6 seconds ago 126MB
  5. python 3.9.5-slim c71955050276 3 weeks ago 115MB

提交镜像到dockerhub

  1. vagrant@swarm-manager:~/flask-redis$ docker login
  2. Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
  3. Username: xiaopeng163
  4. Password:
  5. WARNING! Your password will be stored unencrypted in /home/vagrant/.docker/config.json.
  6. Configure a credential helper to remove this warning. See
  7. https://docs.docker.com/engine/reference/commandline/login/#credentials-store
  8. Login Succeeded
  9. vagrant@swarm-manager:~/flask-redis$ docker-compose push
  10. WARNING: The REDIS_PASSWORD variable is not set. Defaulting to a blank string.
  11. Pushing flask (xiaopeng163/flask-redis:latest)...
  12. The push refers to repository [docker.io/xiaopeng163/flask-redis]
  13. f447d33c161b: Pushed
  14. f7395da2fd9c: Pushed
  15. 5b156295b5a3: Layer already exists
  16. 115e0863702d: Layer already exists
  17. e10857b94a57: Layer already exists
  18. 8d418cbfaf25: Layer already exists
  19. 764055ebc9a7: Layer already exists
  20. latest: digest: sha256:c909100fda2f4160b593b4e0fb692b89046cebb909ae90546627deca9827b676 size: 1788
  21. vagrant@swarm-manager:~/flask-redis$

通过stack启动服务

  1. vagrant@swarm-manager:~/flask-redis$ env REDIS_PASSWORD=ABC123 docker stack deploy --compose-file docker-compose.yml flask-demo
  2. Ignoring unsupported options: build
  3. Creating network flask-demo_default
  4. Creating service flask-demo_flask
  5. Creating service flask-demo_redis-server
  6. vagrant@swarm-manager:~/flask-redis$
  7. vagrant@swarm-manager:~/flask-redis$ docker stack ls
  8. NAME SERVICES ORCHESTRATOR
  9. flask-demo 2 Swarm
  10. vagrant@swarm-manager:~/flask-redis$ docker stack ps flask-demo
  11. ID NAME IMAGE NODE DESIRED STATE CURRENT STATE
  12. ERROR PORTS
  13. lzm6i9inoa8e flask-demo_flask.1 xiaopeng163/flask-redis:latest swarm-manager Running Running 23 seconds ago
  14. ejojb0o5lbu0 flask-demo_redis-server.1 redis:latest swarm-worker2 Running Running 21 seconds ago
  15. vagrant@swarm-manager:~/flask-redis$ docker stack services flask-demo
  16. ID NAME MODE REPLICAS IMAGE PORTS
  17. mpx75z1rrlwn flask-demo_flask replicated 1/1 xiaopeng163/flask-redis:latest *:8080->5000/tcp
  18. z85n16zsldr1 flask-demo_redis-server replicated 1/1 redis:latest
  19. vagrant@swarm-manager:~/flask-redis$ docker service ls
  20. ID NAME MODE REPLICAS IMAGE PORTS
  21. mpx75z1rrlwn flask-demo_flask replicated 1/1 xiaopeng163/flask-redis:latest *:8080->5000/tcp
  22. z85n16zsldr1 flask-demo_redis-server replicated 1/1 redis:latest
  23. vagrant@swarm-manager:~/flask-redis$ curl 127.0.0.1:8080
  24. Hello Container World! I have been seen 1 times and my hostname is 21d63a8bfb57.
  25. vagrant@swarm-manager:~/flask-redis$ curl 127.0.0.1:8080
  26. Hello Container World! I have been seen 2 times and my hostname is 21d63a8bfb57.
  27. vagrant@swarm-manager:~/flask-redis$ curl 127.0.0.1:8080
  28. Hello Container World! I have been seen 3 times and my hostname is 21d63a8bfb57.
  29. vagrant@swarm-manager:~/flask-redis$