sábado, 29 de junio de 2019

Docker Swarm Mode

Creamos tres máquinas virtuales.

Esta será el master
bext@bext-VPCF13WFX:~$ docker-machine create sw-master
Running pre-create checks...
(sw-master) Default Boot2Docker ISO is out-of-date, downloading the latest release...
(sw-master) Latest release for github.com/boot2docker/boot2docker is v18.09.7
(sw-master) Downloading /home/bext/.docker/machine/cache/boot2docker.iso from https://github.com/boot2docker/boot2docker/releases/download/v18.09.7/boot2docker.iso...
(sw-master) 0%....10%....20%....30%....40%....50%....60%....70%....80%....90%....100%
Creating machine...
(sw-master) Copying /home/bext/.docker/machine/cache/boot2docker.iso to /home/bext/.docker/machine/machines/sw-master/boot2docker.iso...
(sw-master) Creating VirtualBox VM...
(sw-master) Creating SSH key...
(sw-master) Starting the VM...
(sw-master) Check network to re-create if needed...
(sw-master) Waiting for an IP...
Waiting for machine to be running, this may take a few minutes...
Detecting operating system of created instance...
Waiting for SSH to be available...
Detecting the provisioner...
Provisioning with boot2docker...
Copying certs to the local machine directory...
Copying certs to the remote machine...
Setting Docker configuration on the remote daemon...
Checking connection to Docker...
Docker is up and running!
To see how to connect your Docker Client to the Docker Engine running on this virtual machine, run: docker-machine env sw-master

Y otros dos más, workers

bext@bext-VPCF13WFX:~$ docker-machine create sw-worker-1
Running pre-create checks...
Creating machine...
(sw-worker-1) Copying /home/bext/.docker/machine/cache/boot2docker.iso to /home/bext/.docker/machine/machines/sw-worker-1/boot2docker.iso...
(sw-worker-1) Creating VirtualBox VM...
(sw-worker-1) Creating SSH key...
(sw-worker-1) Starting the VM...
(sw-worker-1) Check network to re-create if needed...
(sw-worker-1) Waiting for an IP...
Waiting for machine to be running, this may take a few minutes...
Detecting operating system of created instance...
Waiting for SSH to be available...
Detecting the provisioner...
Provisioning with boot2docker...
Copying certs to the local machine directory...
Copying certs to the remote machine...
Setting Docker configuration on the remote daemon...
Checking connection to Docker...
Docker is up and running!
To see how to connect your Docker Client to the Docker Engine running on this virtual machine, run: docker-machine env sw-worker-1
bext@bext-VPCF13WFX:~$ docker-machine create sw-worker-2
Running pre-create checks...
Creating machine...
(sw-worker-2) Copying /home/bext/.docker/machine/cache/boot2docker.iso to /home/bext/.docker/machine/machines/sw-worker-2/boot2docker.iso...
(sw-worker-2) Creating VirtualBox VM...
(sw-worker-2) Creating SSH key...
(sw-worker-2) Starting the VM...
(sw-worker-2) Check network to re-create if needed...
(sw-worker-2) Waiting for an IP...
Waiting for machine to be running, this may take a few minutes...
Detecting operating system of created instance...
Waiting for SSH to be available...
Detecting the provisioner...
Provisioning with boot2docker...
Copying certs to the local machine directory...
Copying certs to the remote machine...
Setting Docker configuration on the remote daemon...
Checking connection to Docker...
Docker is up and running!
To see how to connect your Docker Client to the Docker Engine running on this virtual machine, run: docker-machine env sw-worker-2

Veamos sus detalles
bext@bext-VPCF13WFX:~$ docker-machine ls
NAME          ACTIVE   DRIVER       STATE     URL                         SWARM   DOCKER     ERRORS
sw-master     -        virtualbox   Running   tcp://192.168.99.111:2376           v18.09.7   
sw-worker-1   -        virtualbox   Running   tcp://192.168.99.112:2376           v18.09.7   
sw-worker-2   -        virtualbox   Running   tcp://192.168.99.113:2376           v18.09.7   

Entremos al maestro y hagamoslo maestro del swarm
bext@bext-VPCF13WFX:~$ docker-machine ssh sw-master
   ( '>')
  /) TC (\   Core is distributed with ABSOLUTELY NO WARRANTY.
 (/-_--_-\)           www.tinycorelinux.net

docker@sw-master:~$ docker swar init --advertise-addr 192.168.99.111
unknown flag: --advertise-addr
See 'docker --help'.

Usage: docker [OPTIONS] COMMAND

A self-sufficient runtime for containers

Options:
      --config string      Location of client config files (default "/home/docker/.docker")
  -D, --debug              Enable debug mode
  -H, --host list          Daemon socket(s) to connect to
  -l, --log-level string   Set the logging level
                           ("debug"|"info"|"warn"|"error"|"fatal") (default "info")
      --tls                Use TLS; implied by --tlsverify
      --tlscacert string   Trust certs signed only by this CA (default
                           "/home/docker/.docker/ca.pem")
      --tlscert string     Path to TLS certificate file (default
                           "/home/docker/.docker/cert.pem")
      --tlskey string      Path to TLS key file (default "/home/docker/.docker/key.pem")
      --tlsverify          Use TLS and verify the remote
  -v, --version            Print version information and quit

Management Commands:
  builder     Manage builds
  checkpoint  Manage checkpoints
  config      Manage Docker configs
  container   Manage containers
  engine      Manage the docker engine
  image       Manage images
  manifest    Manage Docker image manifests and manifest lists
  network     Manage networks
  node        Manage Swarm nodes
  plugin      Manage plugins
  secret      Manage Docker secrets
  service     Manage services
  stack       Manage Docker stacks
  swarm       Manage Swarm
  system      Manage Docker
  trust       Manage trust on Docker images
  volume      Manage volumes

Commands:
  attach      Attach local standard input, output, and error streams to a running container
  build       Build an image from a Dockerfile
  commit      Create a new image from a container's changes
  cp          Copy files/folders between a container and the local filesystem
  create      Create a new container
  deploy      Deploy a new stack or update an existing stack
  diff        Inspect changes to files or directories on a container's filesystem
  events      Get real time events from the server
  exec        Run a command in a running container
  export      Export a container's filesystem as a tar archive
  history     Show the history of an image
  images      List images
  import      Import the contents from a tarball to create a filesystem image
  info        Display system-wide information
  inspect     Return low-level information on Docker objects
  kill        Kill one or more running containers
  load        Load an image from a tar archive or STDIN
  login       Log in to a Docker registry
  logout      Log out from a Docker registry
  logs        Fetch the logs of a container
  pause       Pause all processes within one or more containers
  port        List port mappings or a specific mapping for the container
  ps          List containers
  pull        Pull an image or a repository from a registry
  push        Push an image or a repository to a registry
  rename      Rename a container
  restart     Restart one or more containers
  rm          Remove one or more containers
  rmi         Remove one or more images
  run         Run a command in a new container
  save        Save one or more images to a tar archive (streamed to STDOUT by default)
  search      Search the Docker Hub for images
  start       Start one or more stopped containers
  stats       Display a live stream of container(s) resource usage statistics
  stop        Stop one or more running containers
  tag         Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
  top         Display the running processes of a container
  unpause     Unpause all processes within one or more containers
  update      Update configuration of one or more containers
  version     Show the Docker version information
  wait        Block until one or more containers stop, then print their exit codes

Run 'docker COMMAND --help' for more information on a command.

docker@sw-master:~$ docker swarm init --advertise-addr 192.168.99.111
Swarm initialized: current node (pdlp62hc1wqzo5wuubp5ailmx) is now a manager.

To add a worker to this swarm, run the following command:

    docker swarm join --token SWMTKN-1-1rcp2rvr3e263y7htid8htusfqetzpu5d7r57ueoxzkika14pf-damdrsza3bp1h76nfpbtrf6ph 192.168.99.111:2377

To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions. 
 
Veamos sus nodos
docker@sw-master:~$ docker node ls
ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS      ENGINE VERSION
pdlp62hc1wqzo5wuubp5ailmx *   sw-master           Ready               Active              Leader              18.09.7
 
Nos vamos al host OS y vemos sus nodos
docker@sw-master:~$ exit
logout
exit status 127
bext@bext-VPCF13WFX:~$ docker node ls
ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS      ENGINE VERSION
kdaljefl1ech729w83jrdmqr9 *   bext-VPCF13WFX      Ready               Active              Leader              18.09.6

Nos enlazamos al nodo maestro
bext@bext-VPCF13WFX:~$ eval $(docker-machine env sw-master)
bext@bext-VPCF13WFX:~$ docker node ls
ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS      ENGINE VERSION
pdlp62hc1wqzo5wuubp5ailmx *   sw-master           Ready               Active              Leader              18.09.7

Agregamos los workers al maestro
bext@bext-VPCF13WFX:~$ docker-machine ssh sw-worker-1
   ( '>')
  /) TC (\   Core is distributed with ABSOLUTELY NO WARRANTY.
 (/-_--_-\)           www.tinycorelinux.net

docker@sw-worker-1:~$ docker swarm join --token SWMTKN-1-1rcp2rvr3e263y7htid8htusfqetzpu5d7r57ueoxzkika14pf-damdrsza3bp1h76nfpbtrf6ph 192.168.99.111:2377
This node joined a swarm as a worker.
docker@sw-worker-1:~$ exit
logout
bext@bext-VPCF13WFX:~$ docker-machine ssh sw-worker-2
   ( '>')
  /) TC (\   Core is distributed with ABSOLUTELY NO WARRANTY.
 (/-_--_-\)           www.tinycorelinux.net

docker@sw-worker-2:~$ docker swarm join --token SWMTKN-1-1rcp2rvr3e263y7htid8htusfqetzpu5d7r57ueoxzkika14pf-damdrsza3bp1h76nfpbtrf6ph 192.168.99.111:2377
This node joined a swarm as a worker.
docker@sw-worker-2:~$ exit
logout
bext@bext-VPCF13WFX:~$ docker node ls
ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS      ENGINE VERSION
pdlp62hc1wqzo5wuubp5ailmx *   sw-master           Ready               Active              Leader              18.09.7
qfc9sx1tfe3wms1oera75f80b     sw-worker-1         Ready               Active                                  18.09.7
v28filrne0x5fua7r7rxh6zew     sw-worker-2         Ready               Active                                  18.09.7

Instalamos un Vizualizador Web
bext@bext-VPCF13WFX:~$ docker service create --name=viz --publish=8080:8080 \
--constraint=node.role==manager \ 
--mount=type=bind,src=/var/run/docker.sock,dst=/var/run/docker.sock dockersamples/visualizer
1k6akx8sh6yrod7zsi5cvf71a
overall progress: 1 out of 1 tasks 
1/1: running   
verify: Service converged 
bext@bext-VPCF13WFX:~$ docker service ls
ID                  NAME                MODE                REPLICAS            IMAGE                             PORTS
1k6akx8sh6yr        viz                 replicated          1/1                 dockersamples/visualizer:latest   *:8080->8080/tcp
bext@bext-VPCF13WFX:~$ docker service ps viz
ID                  NAME                IMAGE                             NODE                DESIRED STATE       CURRENT STATE                ERROR               PORTS
n1ulgayr7t0k        viz.1               dockersamples/visualizer:latest   sw-master           Running             Running about a minute ago 
 
Lo interesante es que podemos acceder al servicio desde las tres IP del swarm




Benchmark de el balanceador de carga

Instalamos un simple servicio web

bext@bext-VPCF13WFX:~$ docker service create --name=web --publish=80:80 nginx
h5mriknsbuvoj3veols35xqcx
overall progress: 1 out of 1 tasks 
1/1: running   
verify: Service converged 


bext@bext-VPCF13WFX:~$ curl http://192.168.99.111
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
bext@bext-VPCF13WFX:~$ 

Lo escalamos a dos contenedores

bext@bext-VPCF13WFX:~$ docker service scale web=2
web scaled to 2
overall progress: 2 out of 2 tasks 
1/2: running   
2/2: running   
verify: Service converged 
bext@bext-VPCF13WFX:~$ 



 Con  Apache Benchmark medimos su performance para luego degradar el escalamiento a solo contenedor y comparar resultados.

bext@bext-VPCF13WFX:~$ ab -n 10000 -c 50 http://192.168.99.111/
This is ApacheBench, Version 2.3 <$Revision: 1807734 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 192.168.99.111 (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
Completed 7000 requests
Completed 8000 requests
Completed 9000 requests
Completed 10000 requests
Finished 10000 requests


Server Software:        nginx/1.17.0
Server Hostname:        192.168.99.111
Server Port:            80

Document Path:          /
Document Length:        612 bytes

Concurrency Level:      50
Time taken for tests:   7.719 seconds
Complete requests:      10000
Failed requests:        0
Total transferred:      8450000 bytes
HTML transferred:       6120000 bytes
Requests per second:    1295.48 [#/sec] (mean)
Time per request:       38.596 [ms] (mean)
Time per request:       0.772 [ms] (mean, across all concurrent requests)
Transfer rate:          1069.03 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        1   16   8.3     15      82
Processing:     5   22   8.5     19      82
Waiting:        5   22   8.4     19      79
Total:         10   38  13.1     34     132

Percentage of the requests served within a certain time (ms)
  50%     34
  66%     39
  75%     42
  80%     43
  90%     52
  95%     68
  98%     79
  99%     86
 100%    132 (longest request)
bext@bext-VPCF13WFX:~$ docker service scale web=1
web scaled to 1
overall progress: 1 out of 1 tasks 
1/1: running   
verify: Service converged 
 
 bext@bext-VPCF13WFX:~$ ab -n 10000 -c 50 http://192.168.99.111/
This is ApacheBench, Version 2.3 <$Revision: 1807734 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 192.168.99.111 (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
Completed 7000 requests
Completed 8000 requests
Completed 9000 requests
Completed 10000 requests
Finished 10000 requests


Server Software:        nginx/1.17.0
Server Hostname:        192.168.99.111
Server Port:            80

Document Path:          /
Document Length:        612 bytes

Concurrency Level:      50
Time taken for tests:   75.756 seconds
Complete requests:      10000
Failed requests:        0
Total transferred:      8450000 bytes
HTML transferred:       6120000 bytes
Requests per second:    132.00 [#/sec] (mean)
Time per request:       378.778 [ms] (mean)
Time per request:       7.576 [ms] (mean, across all concurrent requests)
Transfer rate:          108.93 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        1  365 508.8     16    3064
Processing:     1   13   4.3     15      33
Waiting:        1   13   4.5     15      33
Total:          3  379 505.8     33    3085

Percentage of the requests served within a certain time (ms)
  50%     33
  66%   1006
  75%   1019
  80%   1023
  90%   1029
  95%   1034
  98%   1039
  99%   1047
 100%   3085 (longest request)

Así podemos observar en el renglón transfer rate que en este caso al correr la prueba en dos contenedores es sustancialmente más rápido.



eot

No hay comentarios:

Publicar un comentario