Docker swarm building clusters and load balancing

Posted by russellpehrson on Fri, 15 Nov 2019 19:46:00 +0100

Docker swarm

Swarm is a relatively simple tool released by Docker company in early December 2014, which is used to manage Docker clusters. It turns a group of Docker hosts into a single, virtual host. Swarm uses the standard Docker API interface as its front-end access portal. In other words, all kinds of Docker clients (Docker client in Go, Docker_py, Docker, etc.) can directly communicate with swarm. Swarm is developed almost entirely in the Go language.  
Swarm teamon is just a scheduler plus router. Swarm does not run the container itself. It just accepts the requests sent by the docker client and schedules the appropriate nodes to run the container. This means that even if swarm hangs up for some reasons, the nodes in the cluster will run as usual. When swarm resumes running, it will collect and rebuild the cluster information

Structure of Swarm

Experimental environment

System: rhel7
server1 172.25.41.11 
server2 172.25.41.12 
server3 172.25.41.13 
Physical machine 172.25.41.250

I. building clusters

All three hosts need to be installed

[root@server1 ~]# ls
docker-engine-17.03.1.ce-1.el7.centos.x86_64.rpm
docker-engine-selinux-17.03.1.ce-1.el7.centos.noarch.rpm
[root@server1 ~]# yum install -y *
[root@server1 ~]# systemctl start docker
root@server1 ~]# yum install -y bash-*    #After downloading, you need to exit and log in again
[root@foundation41 images]# ssh root@172.25.41.11
[root@server1 ~]# docker swarm init 
Swarm initialized: current node (297d6raqk1k6wv9b9totipqw6) is now a manager.

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

    docker swarm join \
    --token SWMTKN-1-2ygj71qpcyda7k802zu0adgk6ly8z3e80jyd0bcxg3g9ajhnl9-bh2zozew8cg76t0i6jscn9dgb \
    172.25.41.11:2377

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

[root@server2 ~]# docker swarm join \
> --token SWMTKN-1-2ygj71qpcyda7k802zu0adgk6ly8z3e80jyd0bcxg3g9ajhnl9-bh2zozew8cg76t0i6jscn9dgb \
> 172.25.41.11:2377

[root@server3 ~]# docker swarm join \
> --token SWMTKN-1-2ygj71qpcyda7k802zu0adgk6ly8z3e80jyd0bcxg3g9ajhnl9-bh2zozew8cg76t0i6jscn9dgb \
> 172.25.41.11:2377

 

[root@server1 ~]# docker node ls

Three hosts need to add resolution

[root@server1 ~]# vim /etc/hosts
172.25.41.250   westos.org
[root@foundation41 westos.org]# cd /tmp/docker/
[root@foundation41 docker]# pwd
/tmp/docker
[root@foundation41 docker]# docker run -d --restart=always --name registry -v `pwd`/certs:/certs -e REGISTRY_HTTP_ADDR=0.0.0.0:443 -e REGISTRY_HTTP_TLS_CERTIFICATE=/certs/domain.crt  -e REGISTRY_HTTP_TLS_KEY=/certs/domain.key -p 443:443  registry:2
[root@foundation41 docker]# docker ps -a
[root@foundation41 docker]# cd /etc/docker/
[root@foundation41 docker]# scp -r certs.d/  root@172.25.41.11:/etc/docker/
[root@foundation41 docker]# scp -r certs.d/ root@172.25.41.12:/etc/docker/
[root@foundation41 docker]# scp -r certs.d/ root@172.25.41.13:/etc/docker/
[root@foundation41 docker]# docker push westos.org/nginx

[root@server1 ~]# docker pull westos.org/nginx
[root@server1 ~]# docker service create --name nginx --publish 80:80 --replicas 3 westos.org/nginx
#Deploy nginx service in the manager node. The number of services is 3. Expose the specified port as 8080 mapping container 80. Use nginx image
[root@server1 ~]# docker service ls
[root@server1 westos.org]# docker service ps nginx 
[root@server1 westos.org]# docker service scale nginx=6
[root@server1 westos.org]# docker service ps nginx
# Load balancing

Web page access will find load balancing


Physical machine

[root@foundation41 docker]# docker load -i visualizer.tar 
[root@foundation41 docker]# docker images
[root@foundation41 docker]# docker tag dockersamples/visualizer westos.org/visualizer
[root@foundation41 docker]# docker push westos.org/visualizer

[server1]

root@server1 westos.org]# docker service create  --name=viz  --publish=8080:8080/tcp  --constraint=node.role==manager  --mount=type=bind,src=/var/run/docker.sock,dst=/var/run/docker.sock  westos.org/visualizer
[root@server1 westos.org]# docker service ls

Visit: 172.25.41.11:8080

[root@server1 ~]# docker service scale nginx=3
[root@server1 ~]# docker service ps nginx       
[root@server1 ~]# docker ps       
[root@server1 ~]# echo server1 > index.html
[root@server1 ~]# docker container cp index.html nginx.5.alzrk6pwrp5cwkkhwxaeon92c:/usr/share/nginx/html

[server2]

[root@server2 ~]# docker ps
[root@server2 ~]# echo server2 > index.html
[root@server2 ~]# docker container cp index.html nginx.4.fbvrxu0hq4392ghog1fdwjw4t:/usr/share/nginx/html

[server3]

[root@server3 ~]# echo server3 > index.html
[root@server3 ~]# docker ps
[root@server3 ~]# docker container cp index.html nginx.2.85yy62a6253peqjx2vyobnuzc:/usr/share/nginx/html

Test whether load balancing is realized in physical machine

[root@foundation41 docker]# for i in {1..10}; do curl 172.25.41.11;done

Rolling update

Physical machine
root@foundation41 docker]# docker tag rhel7:v1 westos.org/rhel7:v1
[root@foundation41 docker]# docker push westos.org/rhel7:v1 
server1
[root@server1 ~]# docker service scale nginx=30
[root@server1 ~]# docker service update --image westos.org/rhel7:v1 --update-parallelism 3 --update-delay 10s nginx

[root@server1 ~]# docker service ps nginx

Topics: Docker Nginx CentOS RPM