What is Docker Swarm?

  • Compare the Docker

The Docker can understand as we introduce before is one of our service independent operation of the container, so in practice, our systems may be a micro service application, system according to the business is divided into several modules, can understand a module corresponding to a set of running the same mirror Docker container, this is a single Docker, Swarm is the orchestration tool that manages routing, load balancing, service discovery, container scaling, logs and configuration files among all containers in the system.

  • Compare the Docker – Compose

Docker-compose is similar to our daily housekeeper in that it only manages all the containers on a host and can start multiple containers at once. Docker-swarm can manage multiple containers across hosts, and when a container state is abnormal, it can quickly start containers on other machines, to ensure high availability of service!

  • Contrast Kubernetes

Kubernetes itself has the same role positioning as Docker Swarm, that is to say, they are responsible for the same part of the container field, they are a cross-host container management platform, of course, there are some different characteristics. K8s is a container management platform developed by Google based on its own years of experience in operation and maintenance. Docker Swarm was developed by Docker. Kubernetes encapsulates a layer of concepts on top of Docker, so Swarm is more powerful than Docker, but more complex to operate, and easier to use.

Docker is a container, docker-compose is a single-host scheduling tool, docker-swarm is a multi-host scheduling tool!

swarm
  • Swarm architecture composition
    • Docker Api is based on Docker to manage containers
    • Swarm Manage CLi is the Swarm management Api
    • Leadership is the HA provided by Swarm
    • Scheduler is Swarm’s scheduling policy and filtering rule management
    • The Discovery Service manages the registration and Discovery of services for each container
chart
  • How does Swarm work? Docker Docker Docker Docker Docker Docker Docker Docker Docker Docker Docker Docker Docker Docker Docker Docker Docker Docker But only one management node can be the Leader. The work node is the task execution node.
    • Manager nodes:
      1. Handle cluster management tasks
      2. Maintaining Cluster Status
      3. The scheduler
      4. Swarm provides an external callable API interface
    • Worker node: Docker container that actually performs our service
Perform figure

Docker Swarm command

  • Initializing a Cluster
$docker swarm init --advertise-addr 192.168.99.121

Swarm initialized: current node (bvz81updecsj6wjz393c09vti) is now a manager.



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



    docker swarm join \

    --token SWMTKN-1-3pu6hszjas19xyp7ghgosyx9k8atbfcr8p2is99znpy26u2lkl-1awxwuwd3z9j1z3puu7rcgdbx \

    172.17.0.2:2377



To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.

Copy the code
  • To join the cluster
# to join the worker

$ docker swarm join-token worker





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

    docker swarm join \

    --token SWMTKN-1-3pu6hszjas19xyp7ghgosyx9k8atbfcr8p2is99znpy26u2lkl-1awxwuwd3z9j1z3puu7rcgdbx \

    172.17.0.2:2377

# to join manager

$ docker swarm join-token manager



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



    docker swarm join \

    --token SWMTKN-1-3pu6hszjas19xyp7ghgosyx9k8atbfcr8p2is99znpy26u2lkl-7p73s1dx5in4tatdymyhg9hu2 \

    172.17.0.2:2377

Copy the code
  • Leave the cluster
# --force / -f Force leave

$ docker swarm leave -f

Node left the default swarm.

Copy the code
  • Query cluster node information
docker@manager:~$ docker node ls                                                                                                                                                          

ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS      ENGINE VERSION

Alz0od3rbkg552mkicfuezhkn * manager Ready Active Leader 19.03.12

Ready Active 19.03.12 jxf5u2hnurrgz90vnd9ay3y7p node - 1

Ready Active 19.03.12 lldvm2slseyodo0t7ath5tzne node - 2



Copy the code
  • Swarm Created a service in the cluster
Docker service create --name redis redis:3.0.6



dmu1ept4cxcfe8k8lhtux3ro3



$docker service create --mode global --name redis2 redis:3.0.6



a8q9dasaafudfs8q8w32udass



$ docker service ls



ID            NAME    MODE        REPLICAS  IMAGE

Dmu1ept4cxcf Redis Replicated 1/1 Redis :3.0.6

A8q9dasaafud redis2 Global 1/1 Redis :3.0.6

Copy the code
  • Example View swarm cluster service information
$ docker service inspect redis

Copy the code
  • View service logs
$ docker service logs -f redis



Copy the code
  • View start all services
$ docker service ls 

ID            NAME      MODE            REPLICAS             IMAGE

c8wgl7q4ndfd  frontend  replicated      5/5                  nginx:alpine

Dmu1ept4cxcf Redis Replicated 3/3 Redis :3.0.6

Iwe3278osahj Mongo Global 7/7 Mongo :3.3

hh08h9uu8uwr  job       replicated-job  1/1 (3/5 completed)  nginx:latest



Copy the code
  • View the specific container in which the service is started
$ docker service ps redis



ID             NAME      IMAGE        NODE      DESIRED STATE  CURRENT STATE          ERROR  PORTS

0qihejybwf1x   redis.1   redis:3.0.5  manager1  Running        Running 8 seconds

Bk658fpbex0d redis.2 redis:3.0.5 worker2 Running Running 9 seconds

5LS5S5FLDAqg redis.3 redis:3.0.5 worker1 Running 9 seconds

Copy the code
  • Remove the service
$ docker service rm redis



redis



$ docker service ls



ID  NAME  MODE  REPLICAS  IMAGE

Copy the code
  • Swarm-cluster service (you can dynamically add service providers to a swarm node cluster.)docker service create --mode global ... : Start the service 🈯️ set --mode global cannot use this command. Global creates a service and starts one on each node
Expand the frontend service by 50 nodes

$ docker service scale frontend=50



frontend scaled to 50

Copy the code
  • Specify parameters to update specific referenceshttps://docs.docker.com/engine/reference/commandline/service_update/

Swarm combat drill

Create three VMS using dcoker-machine

1. Create three hosts

docker-machine create --driver=virtualbox  manager

docker-machine create --driver=virtualbox  node-1

docker-machine create --driver=virtualbox  node-2



[root@docker ~]# docker-machine ls

NAME      ACTIVE   DRIVER       STATE     URL                         SWARM   DOCKER      ERRORS

Manager - virtualbox Running TCP: / / 192.168.99.100:2376 v19.03.12

Node - 1 - virtualbox Running TCP: / / 192.168.99.103:2376 v19.03.12

Node - 2 - virtualbox Running TCP: / / 192.168.99.102:2376 v19.03.12

Copy the code

2. After creating hosts, initialize the Swarm Manager node

[root@docker ~]# docker-machine ssh manager

   ( '>')

  /) TC (\   Core is distributed with ABSOLUTELY NO WARRANTY.

 (/-_--_-\)           www.tinycorelinux.net



--advertise-addr Specifies the IP address of the manager

docker@manager:~$docker swarm init --advertise-addr 192.168.99.100

Swarm initialized: current node (alz0od3rbkg552mkicfuezhkn) is now a manager.



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



    docker swarm join --token SWMTKN-1-4mocuep5w9jhd05bww7h0rzwhd1ayd1o3i2lncwkk2cw1rv9d2-9ed4ny56zt7ygw69dok39id8w 192.168.99.100:2377



To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.



Copy the code

So that’s how you initialize the manager isn’t that easy?

3. Switch to Node-1 and Node-2 and add them to the cluster



Swarm swarm # node-1 joined swarm

[root@docker ~]# docker-machine ssh node-1

   ( '>')

  /) TC (\   Core is distributed with ABSOLUTELY NO WARRANTY.

 (/-_--_-\)           www.tinycorelinux.net



docker@node-1:~$ docker swarm join --token SWMTKN mocuep5w9jhd05bww7h0rzwhd1ayd1o3i2lncwkk2cw1rv9d2-9-1-4 ed4ny56zt7ygw69dok39id8w 192.168.99.100:2377

This node joined a swarm as a worker.





#node-2 joined the swarm

[root@docker ~]# docker-machine ssh node-2

   ( '>')

  /) TC (\   Core is distributed with ABSOLUTELY NO WARRANTY.

 (/-_--_-\)           www.tinycorelinux.net



docker@node-2:~$ docker swarm join --token SWMTKN mocuep5w9jhd05bww7h0rzwhd1ayd1o3i2lncwkk2cw1rv9d2-9-1-4 ed4ny56zt7ygw69dok39id8w 192.168.99.100:2377

This node joined a swarm as a worker



Copy the code
  1. Check the cluster node status in manager
docker@manager:~$ docker node ls                                                                                                                                                          

ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS      ENGINE VERSION

Alz0od3rbkg552mkicfuezhkn * manager Ready Active Leader 19.03.12

Ready Active 19.03.12 jxf5u2hnurrgz90vnd9ay3y7p node - 1

Ready Active 19.03.12 lldvm2slseyodo0t7ath5tzne node - 2

Copy the code
  1. Create an Nginx service that contains three copies
--replicas specifies the number of replicas

docker@manager:~$ docker service create  --name test-nginx --publish 8080:80  --replicas=3 nginx

ya3q60llnj62w141aaekggyl2

overall progress: 3 out of 3 tasks 

1/3: running   [==================================================>] 

2/3: running   [==================================================>] 

3/3: running   [==================================================>] 

verify: Service converged 

docker@manager:~$ docker service ls                                                                                                                                                       

ID                  NAME                MODE                REPLICAS            IMAGE               PORTS

ya3q60llnj62        test-nginx          replicated          3/3                 nginx:latest        *:8080->80/tcp

Copy the code

Nginx services can be accessed randomly from any nginx service as long as the manager IP address is 8080.

# send multiple requests to nginx service

The curl 192.168.99.100:8080

# check log

docker@manager:~$ docker service logs -f ya3q60llnj62                                                                                                                                     

test-nginx.1.h50ridnlpsim@node-2    | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration

test-nginx.1.h50ridnlpsim@node-2    | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/

test-nginx.1.h50ridnlpsim@node-2    | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh

test-nginx.1.h50ridnlpsim@node-2    | 10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf

test-nginx.1.h50ridnlpsim@node-2    | 10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf

test-nginx.1.h50ridnlpsim@node-2    | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh

test-nginx.1.h50ridnlpsim@node-2    | /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh

test-nginx.1.h50ridnlpsim@node-2    | /docker-entrypoint.sh: Configuration complete; ready for start up

test- nginx. H50ridnlpsim @ node - 2 | 10.0.0.2 - [25 / Mar 2021:14:59:30 + 0000]"The GET/HTTP / 1.1"200, 612,"-" "Curl / 7.29.0" "-"

test- nginx. H50ridnlpsim @ node - 2 | 10.0.0.2 - [25 / Mar 2021:14:59:33 + 0000]"The GET/HTTP / 1.1"200, 612,"-" "Curl / 7.29.0" "-"

test-nginx.3.mw75ab00bfs1@node-1    | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration

test-nginx.3.mw75ab00bfs1@node-1    | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/

test-nginx.3.mw75ab00bfs1@node-1    | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh

test-nginx.3.mw75ab00bfs1@node-1    | 10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf

test-nginx.3.mw75ab00bfs1@node-1    | 10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf

test-nginx.3.mw75ab00bfs1@node-1    | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh

test-nginx.3.mw75ab00bfs1@node-1    | /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh

test-nginx.3.mw75ab00bfs1@node-1    | /docker-entrypoint.sh: Configuration complete; ready for start up

test- nginx. | 3. Mw75ab00bfs1 @ node - 1 10.0.0.2 - [25 / Mar 2021:14:59:28 + 0000]"The GET/HTTP / 1.1"200, 612,"-" "Curl / 7.29.0" "-"

test- nginx. | 3. Mw75ab00bfs1 @ node - 1 10.0.0.2 - [25 / Mar 2021:14:59:31 + 0000]"The GET/HTTP / 1.1"200, 612,"-" "Curl / 7.29.0" "-"

test- nginx. | 3. Mw75ab00bfs1 @ node - 1 10.0.0.2 - [25 / Mar 2021:14:59:34 + 0000]"The GET/HTTP / 1.1"200, 612,"-" "Curl / 7.29.0" "-"

test-nginx.2.drsv2s4cnf9y@manager    | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration

test-nginx.2.drsv2s4cnf9y@manager    | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/

test-nginx.2.drsv2s4cnf9y@manager    | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh

test-nginx.2.drsv2s4cnf9y@manager    | 10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf

test-nginx.2.drsv2s4cnf9y@manager    | 10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf

test-nginx.2.drsv2s4cnf9y@manager    | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh

test-nginx.2.drsv2s4cnf9y@manager    | /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh

test-nginx.2.drsv2s4cnf9y@manager    | /docker-entrypoint.sh: Configuration complete; ready for start up

test- nginx. 2. Drsv2s4cnf9y @ manager | 10.0.0.2 - [25 / Mar 2021:14:59:29 + 0000]"The GET/HTTP / 1.1"200, 612,"-" "Curl / 7.29.0" "-"

test- nginx. 2. Drsv2s4cnf9y @ manager | 10.0.0.2 - [25 / Mar 2021:14:59:32 + 0000]"The GET/HTTP / 1.1"200, 612,"-" "Curl / 7.29.0" "-"

Copy the code

At this point all requests are loaded onto the three back-end Nginx services.

The end of the

How swarm can replace the functions of microservices!

Qq group: 869215239 Search: coding on the way to focus on the public number!