Use docker 1.12 to build multi host docker swarm cluster

  • 2020-06-19 12:05:57
  • OfStack

The & # 65279; swarm is docker's own container cluster management tool. This article introduces the use of docker 1.12 to build multi-host docker swarm cluster, to share

To prepare

Prepare at least two centos 7 hosts (new minimum installation, can be installed using virtual machine) Open port 2377 tcp, 7946 4789 tcp udp This paper USES 192.168.99.101(hostname: ES15en-node4) as swarm manager 192.168.99.102(hostname: ES20en-node5) as swarm agent1

Install docker engine 1.12
Execute the following command on each machine


# sudo tee /etc/yum.repos.d/docker.repo <<-'EOF'
[dockerrepo]
name=Docker Repository 
baseurl=https://yum.dockerproject.org/repo/main/centos/7/ 
enabled=1 
gpgcheck=1 
gpgkey=https://yum.dockerproject.org/gpg 
EOF

# sudo yum install docker-engine
# sudo systemctl enable docker
# sudo systemctl start docker

Check the Docker version after installation


[root@centos-node4 ~]# docker version 
Client: 
 Version:   1.12.0
 API version: 1.24
 Go version:  go1.6.3
 Git commit:  8eab29e
 Built:    
 OS/Arch:   linux/amd64

Server: 
 Version:   1.12.0
 API version: 1.24
 Go version:  go1.6.3
 Git commit:  8eab29e
 Built:    
 OS/Arch:   linux/amd64

Open port related commands


firewall-cmd --zone=public --add-port=2377/tcp --permanent 
firewall-cmd --zone=public --add-port=7946/tcp --permanent 
firewall-cmd --zone=public --add-port=7946/udp --permanent 
firewall-cmd --zone=public --add-port=4789/tcp --permanent 
firewall-cmd --zone=public --add-port=4789/udp --permanent 
firewall-cmd --reload 

Details of the new docker swarm command

The docker command for clusters is as follows:

docker swarm: Cluster management, subcommands are init, join, ES52en-ES53en, leave, update docker node: Node management, subcommands are demote, inspect,ls, promote, rm, ps, update docker service: Service management, subcommands are create, inspect, ps, ls,rm, scale, update docker stack/deploy: Experimental feature for multi-application deployment

Create the swarm cluster

See the docker swarm command description


[root@centos-node4 ~]# docker swarm -h
Flag shorthand -h has been deprecated, please use --help

Usage:  docker swarm COMMAND

Manage Docker Swarm

Options: 
   --help  Print usage

Commands: 
 init    Initialize a swarm
 join    Join a swarm as a node and/or manager
 join-token Manage join tokens
 update   Update the swarm
 leave    Leave a swarm

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

Initialize the swarm cluster on swarm manager(ES90en-node4:192.168.99.101)

with --listen-addr Specify ip and port to listen on


# The command format : docker swarm init --listen-addr <MANAGER-IP>:<PORT>
[root@centos-node4 ~]# docker swarm init --listen-addr 192.168.99.101:2377
Swarm initialized: current node (a60d5c3ttymvtozr46uvk17q4) is now a manager. 

View the results


[root@centos-node4 ~]# docker node ls
ID              HOSTNAME   MEMBERSHIP STATUS AVAILABILITY MANAGER STATUS 
a60d5c3ttymvtozr46uvk17q4 * centos-node4 Accepted  Ready  Active    Leader 

Add ES106en-agent1 (ES108en-node5:192.168.99.102) to the swarm cluster

On swarm-agent1:


# The command format : docker swarm join <MANAGER-IP>:<PORT>
[root@centos-node5 ~]# docker swarm join 192.168.99.101:2377
This node joined a Swarm as a worker. 

View the results in swarm manager


[root@centos-node4 ~]# docker node ls
ID              HOSTNAME   MEMBERSHIP STATUS AVAILABILITY MANAGER STATUS 
0ypcw58hjlcvr0xqbtizmau62  centos-node5 Accepted  Ready  Active 
a60d5c3ttymvtozr46uvk17q4 * centos-node4 Accepted  Ready  Active    Leader 

Create an overlay cross-host network

View the existing network


[root@centos-node4 ~]# docker network ls
NETWORK ID     NAME        DRIVER       SCOPE 
abec77415f48    bridge       bridge       local 
e2fff9d572a6    docker_gwbridge   bridge       local 
166bd71f7d0e    host        host        local 
9gr6bfff1rv9    ingress       overlay       swarm 
1d2bfc590294    none        null        local 

As you can see, there is already an overlay network named ingress by default on swarm, which is used in swarm by default. This article will create a new one

Create a new overlay network


 [root@centos-node4 ~]# docker network create --driver overlay docker-net
aoqs3p835s5glx69hi46ou2dw 
 [root@centos-node4 ~]# docker network ls
NETWORK ID     NAME        DRIVER       SCOPE 
abec77415f48    bridge       bridge       local 
aoqs3p835s5g    docker-net     overlay       swarm 
e2fff9d572a6    docker_gwbridge   bridge       local 
166bd71f7d0e    host        host        local 
9gr6bfff1rv9    ingress       overlay       swarm 
1d2bfc590294    none        null        local 

A new network (ES146en-ES147en) has been created

Create applications on the new cross-host overlay network (docker-net)

The deployment of

Start two instances on the docker-net network with the alpine image and marshal them into a set of services


[root@centos-node4 ~]# docker version 
Client: 
 Version:   1.12.0
 API version: 1.24
 Go version:  go1.6.3
 Git commit:  8eab29e
 Built:    
 OS/Arch:   linux/amd64

Server: 
 Version:   1.12.0
 API version: 1.24
 Go version:  go1.6.3
 Git commit:  8eab29e
 Built:    
 OS/Arch:   linux/amd64
0

View deployment results


[root@centos-node4 ~]# docker version 
Client: 
 Version:   1.12.0
 API version: 1.24
 Go version:  go1.6.3
 Git commit:  8eab29e
 Built:    
 OS/Arch:   linux/amd64

Server: 
 Version:   1.12.0
 API version: 1.24
 Go version:  go1.6.3
 Git commit:  8eab29e
 Built:    
 OS/Arch:   linux/amd64
1

You can see two instances running on two nodes

Test whether the network of two hosts can intercommunicate

View the names of the two instances separately


[root@centos-node4 ~]# docker version 
Client: 
 Version:   1.12.0
 API version: 1.24
 Go version:  go1.6.3
 Git commit:  8eab29e
 Built:    
 OS/Arch:   linux/amd64

Server: 
 Version:   1.12.0
 API version: 1.24
 Go version:  go1.6.3
 Git commit:  8eab29e
 Built:    
 OS/Arch:   linux/amd64
2

Tested on swarm-manager


[root@centos-node4 ~]# docker exec -ti helloworld.2.55uhq6xxcv53xlkqv2f0be9b9 sh
/ # ping helloworld.1.eul3bus45qz3b555wekotdmo5
PING helloworld.1.eul3bus45qz3b555wekotdmo5 (10.0.9.3): 56 data bytes 
64 bytes from 10.0.9.3: seq=0 ttl=64 time=0.514 ms 
64 bytes from 10.0.9.3: seq=1 ttl=64 time=0.508 ms 
64 bytes from 10.0.9.3: seq=2 ttl=64 time=0.381 ms 
64 bytes from 10.0.9.3: seq=3 ttl=64 time=0.408 ms 
^C
--- helloworld.1.eul3bus45qz3b555wekotdmo5 ping statistics ---
4 packets transmitted, 4 packets received, 0% packet loss 
round-trip min/avg/max = 0.381/0.452/0.514 ms

Test on swarm-agent1


[root@centos-node4 ~]# docker version 
Client: 
 Version:   1.12.0
 API version: 1.24
 Go version:  go1.6.3
 Git commit:  8eab29e
 Built:    
 OS/Arch:   linux/amd64

Server: 
 Version:   1.12.0
 API version: 1.24
 Go version:  go1.6.3
 Git commit:  8eab29e
 Built:    
 OS/Arch:   linux/amd64
4

The new version of docker swarm is very easy to manage and can quickly set up a cluster across hosts and deploy applications

dokcer swarm comes with load balancing

Create a set of services

docker service create --replicas 2 --name whoami -p 8080:80 daocloud.io/nginx:alpine  

Access the service (can be executed more than once)


[root@centos-node4 ~]# docker version 
Client: 
 Version:   1.12.0
 API version: 1.24
 Go version:  go1.6.3
 Git commit:  8eab29e
 Built:    
 OS/Arch:   linux/amd64

Server: 
 Version:   1.12.0
 API version: 1.24
 Go version:  go1.6.3
 Git commit:  8eab29e
 Built:    
 OS/Arch:   linux/amd64
5

Then use docker logs to view the access log of nginx in the container, showing that both containers have access records


Related articles: