部署多 service 应用

如何像docker-compose一样部署多服务应用,这一节我们先看手动的方式

本节课所用的源码文件 https://github.com/xiaopeng163/flask-redis

创建一个mynet的overlay网络

  1. vagrant@swarm-manager:~$ docker network ls
  2. NETWORK ID NAME DRIVER SCOPE
  3. afc8f54c1d07 bridge bridge local
  4. 128fd1cb0fae docker_gwbridge bridge local
  5. 0ea68b0d28b9 host host local
  6. 14fy2l7a4mci ingress overlay swarm
  7. lpirdge00y3j mynet overlay swarm
  8. a8edf1804fb6 none null local
  9. vagrant@swarm-manager:~$

创建一个redis的service

  1. vagrant@swarm-manager:~$ docker service create --network mynet --name redis redis:latest redis-server --requirepass ABC123
  2. qh3nfeth3wc7uoz9ozvzta5ea
  3. overall progress: 1 out of 1 tasks
  4. 1/1: running [==================================================>]
  5. verify: Service converged
  6. vagrant@swarm-manager:~$ docker servce ls
  7. docker: 'servce' is not a docker command.
  8. See 'docker --help'
  9. vagrant@swarm-manager:~$ docker service ls
  10. ID NAME MODE REPLICAS IMAGE PORTS
  11. qh3nfeth3wc7 redis replicated 1/1 redis:latest
  12. vagrant@swarm-manager:~$ docker service ps redis
  13. ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
  14. 111cpkjn4a0k redis.1 redis:latest swarm-worker2 Running Running 19 seconds ago
  15. vagrant@swarm-manager:~$

创建一个flask的service

  1. vagrant@swarm-manager:~$ docker service create --network mynet --name flask --env REDIS_HOST=redis --env REDIS_PASS=ABC123 -p 8080
  2. :5000 xiaopeng163/flask-redis:latest
  3. y7garhvlxah592j5lmqv8a3xj
  4. overall progress: 1 out of 1 tasks
  5. 1/1: running [==================================================>]
  6. verify: Service converged
  7. vagrant@swarm-manager:~$ docker service ls
  8. ID NAME MODE REPLICAS IMAGE PORTS
  9. y7garhvlxah5 flask replicated 1/1 xiaopeng163/flask-redis:latest *:8080->5000/tcp
  10. qh3nfeth3wc7 redis replicated 1/1 redis:latest
  11. vagrant@swarm-manager:~$ docker service ps flask
  12. ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
  13. quptcq7vb48w flask.1 xiaopeng163/flask-redis:latest swarm-worker1 Running Running 15 seconds ago
  14. vagrant@swarm-manager:~$ curl 127.0.0.1:8080
  15. Hello Container World! I have been seen 1 times and my hostname is d4de54036614.
  16. vagrant@swarm-manager:~$ curl 127.0.0.1:8080
  17. Hello Container World! I have been seen 2 times and my hostname is d4de54036614.
  18. vagrant@swarm-manager:~$ curl 127.0.0.1:8080
  19. Hello Container World! I have been seen 3 times and my hostname is d4de54036614.
  20. vagrant@swarm-manager:~$ curl 127.0.0.1:8080
  21. Hello Container World! I have been seen 4 times and my hostname is d4de54036614.
  22. vagrant@swarm-manager:~$