This commit is contained in:
Guillem Hernandez Sola
2018-10-25 17:11:07 +02:00
parent cc5c9fd797
commit 35b4e612ff
2 changed files with 136 additions and 3 deletions

View File

@@ -32,7 +32,7 @@ redis
Create a file called Dockerfile and paste this in:
```shell
FROM python:3.4-alpine
FROM python:3.7-alpine
ADD . /code
WORKDIR /code
RUN pip install -r requirements.txt
@@ -108,7 +108,7 @@ Hello World! I have been seen 3 times.
Bring the app down:
```shell
$ docker-compose down --volumes
vagrant@docker$ docker-compose down --volumes
Stopping stackdemo_web_1 ... done
Stopping stackdemo_redis_1 ... done
@@ -122,7 +122,7 @@ Push the generated image to the registry
To distribute the web apps image across the swarm, it needs to be pushed to the registry you set up earlier. With Compose, this is very simple:
```shell
$ docker-compose push
vagrant@docker$ docker-compose push
Pushing web (127.0.0.1:5000/stackdemo:latest)...
The push refers to a repository [127.0.0.1:5000/stackdemo]
@@ -133,4 +133,121 @@ c9fc143a069a: Pushed
011b303988d2: Pushed
latest: digest: sha256:a81840ebf5ac24b42c1c676cbda3b2cb144580ee347c07e1bc80e35e5ca76507 size: 1372
The stack is now ready to be deployed.
```
### Create the swarm
When you run the command to create a swarm, the Docker Engine starts running in swarm mode.
Run docker swarm init to create a single-node swarm on the current node. The Engine sets up the swarm as follows:
switches the current node into swarm mode.
creates a swarm named default.
designates the current node as a leader manager node for the swarm.
names the node with the machine hostname.
configures the manager to listen on an active network interface on port 2377.
sets the current node to Active availability, meaning it can receive tasks from the scheduler.
starts an internal distributed data store for Engines participating in the swarm to maintain a consistent view of the swarm and all services running on it.
by default, generates a self-signed root CA for the swarm.
by default, generates tokens for worker and manager nodes to join the swarm.
creates an overlay network named ingress for publishing service ports external to the swarm.
The output for docker swarm init provides the connection command to use when you join new worker nodes to the swarm:
```shell
$ docker swarm init
Swarm initialized: current node (dxn1zf6l61qsb1josjja83ngz) is now a manager.
```
To add a worker to this swarm, run the following command:
```shell
docker swarm join \
--token SWMTKN-1-49nj1cmql0jkz5s954yi3oex3nedyz0fb0xx14ie39trti4wxv-8vxv8rssmk743ojnwacrr2e7c \
192.168.99.100:2377
```
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
To retrieve the join command including the join token for worker nodes, run:
$ docker swarm join-token worker
To add a worker to this swarm, run the following command:
```shell
docker swarm join \
--token SWMTKN-1-49nj1cmql0jkz5s954yi3oex3nedyz0fb0xx14ie39trti4wxv-8vxv8rssmk743ojnwacrr2e7c \
192.168.99.100:2377
This node joined a swarm as a worker.
```
NOTE: The IPs could change in your environment
### Deploy the stack to the swarm
Create the stack with docker stack deploy:
```shell
vagrant@docker$ docker stack deploy --compose-file docker-compose.yml stackdemo
Ignoring unsupported options: build
Creating network stackdemo_default
Creating service stackdemo_web
Creating service stackdemo_redis
```
The last argument is a name for the stack. Each network, volume and service name is prefixed with the stack name.
Check that its running with docker stack services stackdemo:
```shell
vagrant@docker$ docker stack services stackdemo
ID NAME MODE REPLICAS IMAGE
orvjk2263y1p stackdemo_redis replicated 1/1 redis:3.2-alpine@sha256:f1ed3708f538b537eb9c2a7dd50dc90a706f7debd7e1196c9264edeea521a86d
s1nf0xy8t1un stackdemo_web replicated 1/1 127.0.0.1:5000/stackdemo@sha256:adb070e0805d04ba2f92c724298370b7a4eb19860222120d43e0f6351ddbc26f
```
Once its running, you should see 1/1 under REPLICAS for both services. This might take some time if you have a multi-node swarm, as images need to be pulled.
As before, you can test the app with curl:
```shell
vagrant@docker$ curl http://localhost:8000
Hello World! I have been seen 1 times.
vagrant@docker$ curl http://localhost:8000
Hello World! I have been seen 2 times.
vagrant@docker$ curl http://localhost:8000
Hello World! I have been seen 3 times.
```
Thanks to Dockers built-in routing mesh, you can access any node in the swarm on port 8000 and get routed to the app:
```shell
vagrant@docker$ curl http://address-of-other-node:8000
Hello World! I have been seen 4 times.
```
Bring the stack down with docker stack rm:
```shell
vagrant@docker$ docker stack rm stackdemo
Removing service stackdemo_web
Removing service stackdemo_redis
Removing network stackdemo_default
```
Bring the registry down with docker service rm:
```shell
vagrant@docker$ docker service rm registry
```
If youre just testing things out on a local machine and want to bring your Docker Engine out of swarm mode, use docker swarm leave:
```shell
vagrant@docker$ docker swarm leave --force
Node left the swarm.
```

16
swarm/Vagrantfile vendored
View File

@@ -4,17 +4,33 @@ Vagrant.configure(2) do |config|
manager.vm.network "private_network", ip: "192.168.0.248"
manager.vm.hostname = "manager.example.com"
manager.vm.provision "shell", path: "docker_install.sh"
manager.vm.network "forwarded_port", guest: 80, host: 80
manager.vm.network "forwarded_port", guest: 443, host: 443
manager.vm.network "forwarded_port", guest: 8000, host: 8000
manager.vm.network "forwarded_port", guest: 5000, host: 5000
manager.vm.network "forwarded_port", guest: 6379, host: 6379
end
config.vm.define "agent1" do |agent1|
agent1.vm.box = "bento/ubuntu-18.04"
agent1.vm.network "private_network", ip: "192.168.0.247"
agent1.vm.hostname = "docker-agent1.example.com"
agent1.vm.provision "shell", path: "docker_install.sh"
agent1.vm.network "forwarded_port", guest: 80, host: 80
agent1.vm.network "forwarded_port", guest: 443, host: 443
agent1.vm.network "forwarded_port", guest: 8000, host: 8000
agent1.vm.network "forwarded_port", guest: 5000, host: 5000
agent1.vm.network "forwarded_port", guest: 6379, host: 6379
end
config.vm.define "agent2" do |agent2|
agent2.vm.box = "bento/ubuntu-18.04"
agent2.vm.network "private_network", ip: "192.168.0.246"
agent2.vm.hostname = "docker-agent2.example.com"
agent2.vm.provision "shell", path: "docker_install.sh"
agent2.vm.network "forwarded_port", guest: 80, host: 80
agent2.vm.network "forwarded_port", guest: 443, host: 443
agent2.vm.network "forwarded_port", guest: 8000, host: 8000
agent2.vm.network "forwarded_port", guest: 5000, host: 5000
agent2.vm.network "forwarded_port", guest: 6379, host: 6379
end
end