Deploying Applications With Docker-Swarm.
Applications Deployment With Docker Swarm
Docker Swarm orchestrates your Docker Engines across multiple hosts. Simply define your applications as service stacks, and Swarm handles the deployment and management. Think docker-compose for the whole team! But what happens under the hood?
This captures the core idea of Swarm - simplified orchestration through service stacks - without getting bogged down in technical details like file formats. It also retains the connection to docker-compose, but avoids going off on a tangent about configuration similarities.
Lets' get our hands dirty
You can work with your own server to code along but I highly recommend using play-with-docker.com. It is a website managed by a few smart Docker developers. It comes with Docker preinstalled and configured. You can spin up several node servers for a restricted duration.
Creating Our Docker Swarm
docker swarm init --advertise-addr <your node_ip >
Substitute <your node_ip> with the IP address of your node. On play-with-docker website, the address of your IP is on the top page, and if you’re using your own VPS, feel free to use your server’s private IP address as long as it is accessible by other nodes.
docker swarm join-token manager
Copy the command you just generated and run it on both your second and third nodes. This will bring those nodes into the swarm, creating a powerful trio!
领英推荐
Double-check that all three nodes are indeed part of the team. Type docker node ls to get a list of all active members. You should see something like this:
$ docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS
su1bgh1uxqsikf1129tjhg5r8 * node1 Ready Active Leader
t1tgnq38wb0cehsry2pdku10h node3 Ready Active Reachable
wxie5wf65akdug7sfr9uuleui node2 Ready Active
Remember the asterisk by the first node's ID? That means it's our current connection. It's also the leader, with the others ready to step in if needed. Pretty cool, right? Now, time to launch our first app! Let's ship it!
version: '3.1'
services:
web:
image: David/docker-swarm-demo-web
command: gunicorn --bind 0.0.0.0:5000 wsgi:app
deploy:
replicas: 2
secrets:
- db_password
nginx:
image: David/docker-swarm-demo-nginx
ports:
- 8000:80
deploy:
mode: global
redis:
image: redis
deploy:
replicas: 1
secrets:
db_password:
external: true
Quickly create a file named docker-compose.yml on your first node.
Use this command to efficiently add the configuration: echo "<your cmd>" > docker-compose.yml
Before deploying, we need to create a secret called db_password as it's referenced in the configuration.
echo "supersecretpassword" | docker secret create db_password -
Let's instruct Docker to pick up our custom configuration and get things rolling!
docker stack deploy -c docker-compose.yml demo
After running the command, you'll witness Docker bringing the three services (web, nginx, and redis) to life. Due to naming our stack "demo," their actual names become demo_web, demo_nginx, and demo_redis. To view these active services, use the command "docker service ls". It'll display something like:
$ docker service ls
ID NAME MODE REPLICAS IMAGE PORTS
cih6u1t88vx7 demo_web replicated 2/2 lsapan/docker-swarm demo-web:latest
u0p1gd6tykvu demo_nginx global 3/3 lsapan/docker-swarm demo-nginx:latest *:8000->80/
tcp
wa1gz80ker2g demo_redis replicated 1/1 redis:latest
Images deployed! Containers are spinning up. Wait a bit if replicas aren't ready, Docker might still be downloading.