Copyright belongs to the author, any form of reprint please contact the author to obtain authorization and indicate the source.
Docker swarm profile
Docker Swarm and Docker Compose are both official Docker container composing projects, but Docker Compose is a tool for creating multiple containers on a single server or host. Docker Swarm can create container cluster services on multiple servers or hosts. For the deployment of micro-services, Docker Swarm is obviously more suitable.
Docker Swarm Docker Swarm has been included in Docker engine since version 1.12.0, and has built-in service discovery tools, we do not need to like before, Then configure Etcd or Consul for service discovery configuration.
Docker Swarm architecture is as follows: there are multiple workers under one Manager (each container is actually run)
How to create a swarm
Swarm node allocation is random by default, we didn’t know at first
docker swarm --help # View helpDocker swarm init - advertise - addr = 198.168.205.10Docker Swarm Manager (docker Swarm Manager
docker node ls
Copy the code
- On the first machine, run the command to set it as the master node
- The second machine executes the command in the penultimate paragraph above
- Then go back to the first Manager machine and look at the current node
- The remaining machines, in turn, execute the same commands
- Check the first Manager machine again and you will find that there are already 3 Docker Swarm Nodes
A simple Docker Swarm cluster has been created
Create maintenance and horizontal scaling
docker service create --help
docker service ls # view all
docker service scale # Horizontal scaling
docker service scale [serviceName] = [num] Set the number of service (container) containers
docker service ps [serviceNmae] View the current service details
docker ps
Copy the code
- For example, run a command
docker service create --name demo busybox sh -c"while true; do sleep 3600; done"
- Execute the command
docker service ls
See, REPLICAS represents horizontal scaling
- Execute the command
docker service ps demo
NODE showing node3 indicates that demo Service is on our third machine
- Execute the command
docker service scale=5
In this case, REPLICAS is 5/5. What does 5/5 mean? The first 5 is how many are ready, and the second 5 is how many scheuld instances are there
- Execute the command
docker service ps demo
You can see all deployments of the Servie
Docker Swarm Manange after Docker service rm demo is executed, all containers of the service demo created above are destroyed. You can run docker service ps demo, docker ps again to see details.
Deployment – wordpress in swRAM cluster
docker network create -d overlay demo Create a Docker network called Demo
docker network ls # check
docker exec [containerID] ip a Check the container IP
Copy the code
- Execute the command
docker service create --name mysql --env MYSQL_ROOT_PASSWORD=root --env MYSQL_DATABASE=wordpress --network demo --mount type=volume,source=mysql-data,destination=/var/lib/mysql mysq
- Check the service mysql deployment
3. Run the command
docker service create --name wordpress -p 80:80 --env WORDPRESS_DB_PASSWORD=root --env WORDPRESS_DB_HOST=mysql --network demo wordpress
- Execute the command
docker service ps wordpress
.docker ps
Check the wordpress deployment
Swarm is the underlying mechanism that synchronizes the creation of a network because it communicates between multiple nodes.
Swarm Communicates with cluster services
-
Docker network create -d overlay demo Create a Docker network named demo
-
Run the docker service create –name whoami -p 88:8000 –network demo -d jwilder/whoami command
-
Run the docker service ls command, docker service ps whoami check, you can see that the service is on the current machine
-
Run the curl 127.0.0.1:8000 command to return the host name
-
Run the docker service create –name client -d –network demo busybox sh -c “while true; do sleep 4600; done”
-
Run the docker service ls command, docker service ps client check, you can see that the service is on another machine
-
View and access the Service Client
-
You can ping the whoami host address, but the host address is 10.0.0.7, obviously this is not a real IP address
-
Enter the service whoami machine and execute docker service Scale whoami=2
-
Nslookup nslookup nslookup nslookup nslookup nslookup nslookup nslookup nslookup nslookup
Execute on the machine where service Whoami is deployed
docker exec [containerID] ip a
View the corresponding container IP address to know the real IP address. The VIP (virtual IP address) does not change frequently, and the machine IP address may change
If you run the curl 127.0.0.1:8000 command repeatedly, the host name may change each time. This is because the load balancing function is provided.
Internal Load Balancing figure:
DNS+VIP+iptables+LVS
Two manifestations of Routing Mesh
- Internal — Access between containers via overlay network (via VIP virtual IP)
- Ingress — If the service has a binding interface, the service can be accessed via the corresponding interface of any Swarm node
The above operations use these two methods respectively
Ingress load balancing
- Load balancing for external access
- Swarm nodes are exposed to service ports
- Internal load balancing is done through IPVS
- Use the previous example service whoami. Nodes are on swarm-manager and swarm- Manager2 respectively
- You can see that a different host name is returned each time, load balancing
- Swarm-manager1 Can be accessed without the service whoami
Swarm-manager1 Run sudo iptables -nl -t NAT to view forwarding rules
ip a
brctl show
docker network ls
docker network inspect docker_gwbridge
sudo ls /var/run/docker/netns
sudo nsenter --net=/var/run/docker/netns/ingress_sbox
iptables -nL -t mangle
yum install ipvsadm
ipvsadm -l