summary
The biggest change in docker version 1.12 is that it integrates docker swarm, and provides a swarm mode under docker engine. Here we mainly talk about docker swarm.
docker engine itself only provides container technology and does not solve container orchestration and communication in cluster environments. docker swarm is a container orchestration management tool. Docker-engine integrates docker swarm after version 1.12 and does not need to be installed separately.
The function of docker swarm, for example, 3 machines have installed docker environments, called 3 docker nodes. So how do you manage these three docker nodes and deploy containers to these three nodes in a load balancing mode similar to load balancing, and let these containers distributed on different nodes communicate with each other. At this time, container orchestration tools are needed. Common orchestration tools include Google's open source kubernetes, apache's mesos, and docker's swarm.
As a Google open source tool, kubernetes has been running in Google's production environment for many years. It is rich in features and stable and reliable. It is currently used by many companies. Docker has built-in swarm mode after version 1.12, integrating container orchestration into docker engine in the core component mode, and drawing on the successful experience of kubernetes.
Install docker
Taking centos7 as an example, the installation document of the official website is attached here. The docker official website is too slow.
Install using yum
1. Update the yum source, sudo yum update;,.
2. Add docker's yum repository
sudo tee /etc// <<-'EOF' [dockerrepo] name=Docker Repository baseurl=/repo/main/centos/7/ enabled=1 gpgcheck=1 gpgkey=/gpg EOF
3. Install docker-engine, sudo yum install docker-engine
4. Start the docker daemon, sudo systemctl start docker
The installation of docker ends here. Let’s talk about the issues that need to be paid attention to when installing docker through proxy in the company’s intranet environment.
1. First, let the machine access the Internet through the proxy
Modify /etc/profile and add export http_proxy=proxy=proxy username:password@proxy address:port
Make the modification take effect, source /etc/profile
2. Configure yum to use proxy
vim /etc/ Add proxy=http://proxy username:password@proxy address:port
After these two steps, you can install docker through the proxy in the intranet.
Configure docker
Open docker remote management port 2375 and configure the docker hub domestic repository.
1. Create a new docker configuration file
vim /etc/sysconfig/docker
Increase
DOCKER_OPTS="-H unix:///var/run/ -H 0.0.0.0:2375 --registry-mirror=domestic warehouse address"
If you need to access through proxy, you need to add
HTTP_PROXY=http://Proxy Username: Password@Proxy Address: Port
2. Load configuration files when starting the docker daemon
vim /lib/systemd/system/
Modify ExecStart configuration to
ExecStart=/usr/bin/dockerd $DOCKER_OPTS
Add configuration file path in [Service]
EnvironmentFile=-/etc/sysconfig/docker
Specify the configuration file loading path to the configuration file /etc/sysconfig/docker created in the first step
3. Restart the docker daemon
sudo systemctl stop docker
sudo systemctl start docker
4. Check whether the configuration is effective
ps -ef|grep docker
If the configuration takes effect, the parameters you just configured will be added to the dockerd process.
Start swarm mode
You only need to initialize the swarm cluster on one docker node, and other nodes join this cluster.
Select a docker node as the leader in swarm mode and run it
docker swarm init --advertise-addr host ip
Follow the prompt information to execute commands on other nodes to join the swarm cluster
Execute docker node ls on the leader node to view node information
Create a cluster network
1. Run on any node in the swarm cluster
docker network ls Check the current network status
Networks with scope local can only work on the local machine, and the network used by the swarm cluster is created below.
2. Run docker network create --driver overlay my-network
Create a cluster network called my-network
Run docker network ls again and you can see the my-network I just created. The scope is swarm. This network can be used in swarm mode and allows containers on multiple nodes to be interconnected.
When creating a service in swarm, you can specify the network used by --network network name. If multiple services use the same scope as swarm network, they can communicate with each other through the service name.
Docker swarm is simple to use
The following is a brief introduction to the use of docker swarm. If you are interested, you can check the official docker documentation for in-depth understanding.
All the commands below are run on the leader node of swarm! !
docker service create --replicas 1 --name tomcat --publish 9090:8080 tomcat:latest
Here a service called tomcat is created. Swarm encapsulates the container as a service, similar to the pod in kubernates. A service is a container combination that can be dispatched by Swarm.
Here --replicas 1 specifies the number of replicas of the service.
--publish 9090:8080 Publish port 8080 in the container to port 9090 of the host
Run docker service ls to view the current service list and you can see that there is a service called tomcat.
Run docker service ps tomcat to see which swarm node the tomcat service is running on and the current running status.
Switch to the node running the tomcat service, run docker ps to see the container ID and port mapping.
Run docker logs -f container ID to view the tomcat run log.
After tomcat is successfully launched, you can access the tomcat homepage in the browser. The address is the IP of the docker node (any node can do it), and the port is 9090, which is the publish specified when creating the service.
A tomcat service has been successfully run above, and this service is deployed here.
Run docker service scale tomcat=2 to expand the tomcat service to two instances.
Run docker service ls to see that the replicas of the tomcat service have become 1/2, and the second instance will become 2/2 after it is successfully launched.
Run docker service ps tomcat to see which docker node the two tomcat service instances run on and their running status.
Swarm will automatically perform load balancing in two services
There are some other commands for swarm, such as docker service rm service name to delete the specified service, as well as grayscale release, etc. Please refer to the official document for specific use, and will not be discussed in detail here.
Issues to be noted in swarm mode
swarm will only schedule containers between docker nodes and will not schedule the mounted volume volume used by containers. When deploying database containers with swarm, you need to pay attention to the data file issues. Or containers loading external configuration files, you also need to pay attention to configuration files.
A simple solution is to have these containers scheduled to the same docker node every time. You need to specify the --constraint parameter when creating a service, such as --constraint '==myhost' . The created service will only be scheduled to the docker node with the host name myhost.
Another solution is to use docker volume plugin, such as flocker. Flocker can be dispatched together with volume during container scheduling. I won’t go into details here, you can refer to flocker’s official website documentation.
at last
Here is just a brief introduction to the installation and configuration of docker and the use of docker swarm. If you want to have a deeper understanding, please refer to the official documentation. Official documentation is the best way to learn. docker is just the beginning of the microservice architecture. If you want to practice microservices, docker is essential.
In the future, we will launch microservice architectures based on docker deployment, use spring cloud to make microservice solutions, deployment of docker-based mysql and mongodb, deployment of docker-based rabbitmq and activemq message middleware, and kafka and elk log collection statistics based on docker deployment, etc.
The above is what the editor introduced to you to easily install docker and run docker swarm mode. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support for my website!