├── LICENSE ├── README.md ├── docker-compose-go-demo.yml ├── docker-compose-stack.yml ├── setup-stack.sh └── swarm-cluster.sh /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Todsaporn Banjerdkit 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ### Source 2 | - https://technologyconversations.com/2017/01/23/using-docker-stack-and-compose-yaml-files-to-deploy-swarm-services/ 3 | 4 | ### Prerequisites 5 | - Docker 1.13 6 | - Docker Compose 1.10 7 | - Docker Machine 0.9 8 | - [Docker Toolbox](https://www.docker.com/products/docker-toolbox) 9 | 10 | ### Upstream 11 | ```shell 12 | curl -o swarm-cluster.sh https://raw.githubusercontent.com/vfarcic/docker-flow-proxy/master/scripts/swarm-cluster.sh 13 | ``` 14 | 15 | ### Swarm Cluster Setup 16 | ```shell 17 | # Will create 3 nodes, taking sometime. 18 | . swarm-cluster.sh 19 | ``` 20 | 21 | ### Creating Swarm Services Through Docker Stack Commands. 22 | ```shell 23 | # Creating network 24 | . setup-stack.sh 25 | ``` 26 | 27 | ### Cleanup 28 | ```shell 29 | docker-machine rm -f node-1 node-2 node-3 30 | ``` -------------------------------------------------------------------------------- /docker-compose-go-demo.yml: -------------------------------------------------------------------------------- 1 | version: '3' 2 | 3 | services: 4 | 5 | main: 6 | image: vfarcic/go-demo${TAG} 7 | environment: 8 | - DB=db 9 | networks: 10 | - proxy 11 | - default 12 | deploy: 13 | replicas: 3 14 | update_config: 15 | parallelism: 1 16 | delay: 10s 17 | labels: 18 | - com.df.notify=true 19 | - com.df.distribute=true 20 | - com.df.servicePath=/demo 21 | - com.df.port=8080 22 | 23 | db: 24 | image: mongo 25 | networks: 26 | - default 27 | 28 | networks: 29 | default: 30 | external: false 31 | proxy: 32 | external: true 33 | -------------------------------------------------------------------------------- /docker-compose-stack.yml: -------------------------------------------------------------------------------- 1 | version: "3" 2 | 3 | services: 4 | 5 | proxy: 6 | image: vfarcic/docker-flow-proxy 7 | ports: 8 | - 80:80 9 | - 443:443 10 | networks: 11 | - proxy 12 | environment: 13 | - LISTENER_ADDRESS=swarm-listener 14 | - MODE=swarm 15 | deploy: 16 | replicas: 2 17 | 18 | swarm-listener: 19 | image: vfarcic/docker-flow-swarm-listener 20 | networks: 21 | - proxy 22 | volumes: 23 | - /var/run/docker.sock:/var/run/docker.sock 24 | environment: 25 | - DF_NOTIF_CREATE_SERVICE_URL=http://proxy:8080/v1/docker-flow-proxy/reconfigure 26 | - DF_NOTIF_REMOVE_SERVICE_URL=http://proxy:8080/v1/docker-flow-proxy/remove 27 | deploy: 28 | placement: 29 | constraints: [node.role == manager] 30 | 31 | networks: 32 | proxy: 33 | external: true 34 | -------------------------------------------------------------------------------- /setup-stack.sh: -------------------------------------------------------------------------------- 1 | # Just for perf, you can skip this. 2 | START_SEC=$(date +'%s') 3 | 4 | # Creating network. 5 | docker-machine ssh node-1 "docker network create --driver overlay proxy" 6 | 7 | # Grab the source. 8 | docker-machine ssh node-1 "curl -o docker-compose-stack.yml https://raw.githubusercontent.com/vfarcic/docker-flow-proxy/master/docker-compose-stack.yml" 9 | 10 | # Create docker-flow-proxy and docker-flow-swarm-listener services. 11 | docker-machine ssh node-1 "docker stack deploy -c docker-compose-stack.yml proxy" 12 | 13 | # Deploying More Stacks. 14 | docker-machine ssh node-1 "curl -o docker-compose-go-demo.yml https://raw.githubusercontent.com/vfarcic/go-demo/master/docker-compose-stack.yml" 15 | 16 | # Deploy. 17 | docker-machine ssh node-1 "docker stack deploy -c docker-compose-go-demo.yml go-demo" 18 | 19 | # See tasks of the stack. 20 | docker-machine ssh node-1 "docker stack ps proxy" 21 | 22 | # Verify from internal. 23 | docker-machine ssh node-1 "docker stack ps go-demo" 24 | 25 | # See how long does it take. 26 | echo "Done in $(($(date +'%s') - $START_SEC)) seconds" 27 | 28 | # Wait for 10 sec to let server start properly. 29 | echo "Will wait for 10 seconds..." 30 | sleep 10 31 | 32 | # Verify from external. 33 | docker-machine ssh node-1 'curl -i "127.0.0.1/demo/hello"' 34 | 35 | # Hm? 36 | echo "If curl don't work do wait a minite and try curl again." 37 | -------------------------------------------------------------------------------- /swarm-cluster.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | # Just for perf, you can skip this. 4 | START_SEC=$(date +'%s') 5 | 6 | set -e 7 | 8 | for i in 1 2 3; do 9 | docker-machine create -d virtualbox node-$i 10 | done 11 | 12 | eval $(docker-machine env node-1) 13 | 14 | docker swarm init \ 15 | --advertise-addr $(docker-machine ip node-1) \ 16 | --listen-addr $(docker-machine ip node-1):2377 17 | 18 | TOKEN=$(docker swarm join-token -q worker) 19 | 20 | for i in 2 3; do 21 | eval $(docker-machine env node-$i) 22 | 23 | docker swarm join --token $TOKEN $(docker-machine ip node-1):2377 24 | done 25 | 26 | eval $(docker-machine env node-1) 27 | 28 | echo "" 29 | echo ">> The Swarm Cluster is set up!" 30 | 31 | # See how long does it take. 32 | echo "Done in $(($(date +'%s') - $START_SEC)) seconds" --------------------------------------------------------------------------------