Case study of docker swarm construction and cross-host network interconnection, dockerswarm
1. Open relevant ports
firewall-cmd --zone=public --add-port=2377/tcp --permanentfirewall-cmd --zone=public --add-port=7946/tcp --permanentfirewall-cmd --zone=public --add-port=7946/udp --permanentfirewall-cmd --zone=public --add-port=4789/tcp --permanentfirewall-cmd --zone=public --add-port=4789/udp --permanentfirewall-cmd --reload
The docker command for the cluster is as follows:
Create a swarm Cluster
View docker swarm command instructions
# docker swarm -hFlag shorthand -h has been deprecated, please use --helpUsage: docker swarm COMMANDManage Docker SwarmOptions: --help Print usageCommands: 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 swarmRun 'docker swarm COMMAND --help' for more information on a command.
Create a cluster
# Command Format: docker swarm init -- listen-addr <MANAGER-IP >:< PORT> [root @ centos-web ~] # Docker swarm init -- listen-addr 172.18.30.29: 2377
Swarm initialized: current node (a60d5c3ttymvtozr46uvk17q4) is now a manager.
View Clusters
# docker node lsID HOSTNAME MEMBERSHIP STATUS AVAILABILITY MANAGER STATUSa60d5c3ttymvtozr46uvk17q4 * centos-web Accepted Ready Active Leader
Obtain the token of the cluster command
#docker swarm join-token managerdocker swarm join \ --token ********* \ 172.18.30.29:2377
Add sub-nodes to the Cluster
docker swarm join \ --token ********* \ 172.18.30.29:2377
Delete a cluster subnode
Docker swarm leave -- force (on the node) docker node rm -- force (on the manager)
Create an overlay cross-host network
View Current Network
[root@H-T-29 ~]# docker network lsNETWORK ID NAME DRIVER SCOPE47d05fd82e77 bridge bridge local 76e0302f7929 docker_gwbridge bridge local 81c22e7f1b48 host host local 4enpk54t9oy2 ingress overlay swarm c80502ba134b none null local
Create a new overlay Network
# By default, an overlay network named ingress exists on swarm.
[root@centos-node4 ~]# docker network create --driver overlay docker-net2o223p435s5glx69dw211u5d2 [root@centos-web ~]# docker network lsNETWORK ID NAME DRIVER SCOPEabec77415f48 bridge bridge localaoqs3p835s5g docker-net overlay swarme2fff9d572a6 docker_gwbridge bridge local166bd71f7d0e host host local9gr6bfff1rv9 ingress overlay swarm1d2bfc590294 none null local
Docker-net is the newly created network
Create an application on the new cross-host overlay Network (docker-net)
Deploy applications
docker service create --replicas 2 --publish 2222:22 --name lvs --network=docker-net inits/lvs
-Number of replicas
-Publish service discovery and port ing
-Mount a file
-Name service name
# Inits/lvs images on the premise node Server
Deployment result
docker service lsID NAME REPLICAS IMAGE COMMAND5lgdq3ihiez0 lvs 0/2 inits/lvs[root@centos-node4 ~]# docker service tasks helloworldID NAME SERVICE IMAGE LAST STATE DESIRED STATE NODEeul3bus45qz3b555wekotdmo5 lvs.1 helloworld alpine Running 14 seconds Running centos-node555uhq6xxcv53xlkqv2f0be9b9 lvs.2 helloworld alpine Running 14 seconds Running centos-node4
View the container running status on the two nodes respectively.
#ssh 172.18.30.29
[root@H-T-29 ~]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES389f9eefe455 inits/lvs:latest "/usr/sbin/sshd -D" 15 minutes ago Up 15 minutes 22/tcp lvs.1.aco0uuf1of90tl31skh6a6ndv
#ssh 172.18.30.12[root@H-C-12 ~]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESaf573b0ae945 inits/lvs:latest "/usr/sbin/sshd -D" 15 minutes ago Up 15 minutes 22/tcp lvs.2.0a1s4la7prc3qmfdfdqazitsn
Others
Docker service ls view Cluster list docker service ps lvs view the status of all nodes in the cluster docker service rm lvs Delete the Cluster
Docker service inspect -- pretty lvs cluster attributes
Docker service scale lvs = 4 # scale up the number of cluster nodes
Test whether the networks of two hosts can communicate with each other.
# docker exec -ti lvs.2.0a1s4la7prc3qmfdfdqazitsn sh/ # ping lvs.1.aco0uuf1of90tl31skh6a6ndvPING lvs.1.aco0uuf1of90tl31skh6a6ndv (10.0.9.3): 56 data bytes64 bytes from 10.0.9.3: seq=0 ttl=64 time=0.514 ms64 bytes from 10.0.9.3: seq=1 ttl=64 time=0.508 ms64 bytes from 10.0.9.3: seq=2 ttl=64 time=0.381 ms64 bytes from 10.0.9.3: seq=3 ttl=64 time=0.408 ms^C--- lvs.1.aco0uuf1of90tl31skh6a6ndv ping statistics ---4 packets transmitted, 4 packets received, 0% packet lossround-trip min/avg/max = 0.381/0.452/0.514 ms
Similarly, go to another host to test whether the network is normal.
Or port 22 has been exposed, directly
Ssh 172.18.30.29-p 2222 ifconfig172.18.30.29 internal ipping 172.18.30.12 internal ipssh 172.18.30.12-p 2222 ifconfig172.18.30.12 internal ipping 172.18.30.29 internal ip
Load Balancing provided by dokcer swarm
Create a group of services
docker service create --replicas 2 --name nginx -p 80:80 --network=docker-net inits/nginx
Test Service
Browser access: http: // 172.18.30.29
Refresh the browser and the system will poll and access services under the cluster node