In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
This paper describes the creation and management of Docker cluster in detail. Share with you for your reference, the details are as follows:
Write an application in the "Docker simple installation and Application introduction tutorial" and turn it into a service, in the "Docker distributed Application tutorial", make the application expand 5x in the production process, and define how it should run. Now deploy this application to the cluster and run it on multiple machines, making multi-container, multi-machine applications possible by connecting multiple machines to the Dockerized cluster.
Swarm (cluster) is a set of machines that run Docker and join a cluster, in which case you will continue to run the previous Docker commands, but now they will be executed on the cluster by swarm manager (cluster manager). The machines in the cluster can be physical or virtual, and when they join the cluster, they are called nodes. The cluster manager can use a variety of strategies to run containers, such as emptiest node (the emptest node), which populates the least used machines with containers. Or global (global), which ensures that each machine can only get one instance of the specified container. You can instruct the cluster manager to use these policies in the composition file, just like the one you have already used.
The cluster manager is the only machine in the cluster that can execute commands or authorize other machines to join the cluster as workers (workers). Workers are just there to provide capacity and have no right to tell any other machine what can and cannot be done. So far, you have used Docker in single-host mode on your local machine, but Docker can also switch to cluster mode, which is why you use clustering. When you immediately enable cluster mode to make the current machine a cluster manager, Docker will run the commands executed on the cluster you are managing, not just on the current machine.
Create a cluster
A cluster consists of multiple nodes, which can be physical or virtual machines. It is simple to run docker swarm init to enable cluster mode and make your current machine a cluster manager, and then run docker swarm join on other machines to join the cluster as workers.
Next, to quickly create a cluster using a virtual machine, you need a hypervisor that can create a virtual machine (VMs) and install the Oracle VirtualBox application on the machine. If you have a Windows 10 system and you have Hyper-V installed, you don't need to install VirtualBox, you should use Hyper-V instead.
Now, use docker-machine to create several virtual machines and use the VirtualBox driver:
$docker-machine create-- driver virtualbox myvm1 $docker-machine create-- driver virtualbox myvm2
You have now created two virtual machines named myvm1 and myvm2, and use the following command to list the machines and obtain their IP addresses:
$docker-machine ls
The first machine will act as an administrator, execute management orders and certify workers to join the group, and the second machine will become a worker. You can use docker-machine ssh to send commands to the virtual machine, and execute docker swarm init to make myvm1 the cluster manager:
$docker-machine ssh myvm1 "docker swarm init-advertise-addr"
After docker swarm init is executed, a preconfigured docker swarm join command is included in the response, which you can run on any node you want to add. Copy this command and send it through docker-machine ssh to a virtual machine named myvm2, allowing myvm2 to join the new cluster as a worker:
$docker-machine ssh myvm2 "docker swarm join-token: 2377"
When joining myvm2 to the cluster, choose 2377 as port 2376 is a Docker daemon port, do not use this port, or you may encounter an error. Run docker node ls on the manager to view the nodes in the cluster:
$docker-machine ssh myvm1 "docker node ls"
Now that the cluster has been created, if you want to delete the cluster, you can run it on each node using docker swarm leave.
Deploy applications on a cluster
Now just repeat the process used in the Docker distributed Application tutorial to deploy the new cluster, just keep in mind that only cluster managers like myvm1 can execute Docker commands, and workers are just working.
So far, you have wrapped the Docker command in docker-machine ssh to communicate with the virtual machine, and another option is to run docker-machine env to get and run a currently configured command to communicate with the Docker daemon on the virtual machine. This approach is better because it allows the application to be deployed "remotely" using a local docker-compose.yml file without having to copy it anywhere.
Execute the docker-machine env myvm1 command, copy the command provided on the last line of the output, then paste and run the command to configure the terminal to talk to the cluster manager myvm1:
$docker-machine env myvm1 $eval $(docker-machine env myvm1)
Run docker-machine ls to verify that myvm1 is now the active machine, with an asterisk next to the active state:
$docker-machine ls
You can now use the permissions of myvm1 as the cluster manager to deploy the application by using the docker stack deploy command and a local copy of docker-compose.yml. By configuring to connect to myvm1 through the docker-machine command, you can still access the files on the local host and ensure that the application is deployed on myvm1 by running the following command in the same directory as the docker-compose.yml files:
$docker stack deploy-c docker-compose.yml getstartedlab
In this way, the application is deployed on a cluster, and you can now use the Docker command to see that the service and associated containers have been allocated between myvm1 and myvm2:
$docker stack ps getstartedlab
Visit the cluster
Applications can now be accessed from the IP address of myvm1 or myvm2, and the network is shared and load balanced between them. Run docker-machine ls to get the IP address of the virtual machine and access any of them in a browser, or use the curl command.
You will see five different container ID, all of which loop randomly, demonstrating load balancing. The reason the two IP addresses work is that the nodes in the cluster participate in the ingress routing network, which ensures that a service deployed on a port in the cluster always reserves that port for itself, regardless of which node is actually running the container. The following is a schematic diagram of a routed network that publishes a service called my-web on port 8080 on a three-node cluster:
You can scale the application by changing the docker-compose.yml file, edit the code to change the application's behavior, then rebuild, and then push the new image, just run docker stack deploy again to deploy the changes. You can use the docker swarm join command to join any physical or virtual machine to this cluster and add capacity to the cluster, after which you simply run the docker stack deploy deployment, and the application will take advantage of the new resources.
Clean up and restart
You can use docker stack rm to clean up the stack, for example:
$docker stack rm getstartedlab
You can use the following command to cancel the docker-machine environment variable in the current terminal:
$eval $(docker-machine env-u)
This disconnects the terminal from the virtual machine created by docker-machine and allows you to continue to work in the same terminal. If you shut down the local host, the Docker machine will stop running, and you can check the status of the machine by running docker-machine ls:
$docker-machine ls
To restart a stopped machine, run:
$docker-machine start
If you want to delete the cluster, you can use the docker-machine ssh myvm2 "docker swarm leave" command, or use the docker-machine ssh myvm1 "docker swarm leave-- force" to force the deletion.
I hope what is described in this article will be helpful to the use of docker containers.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.